@vue-skuilder/db 0.1.20 → 0.1.21
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/CLAUDE.md +2 -2
- package/dist/{classroomDB-CZdMBiTU.d.ts → contentSource-BP9hznNV.d.ts} +150 -196
- package/dist/{classroomDB-PxDZTky3.d.cts → contentSource-DsJadoBU.d.cts} +150 -196
- package/dist/core/index.d.cts +3 -3
- package/dist/core/index.d.ts +3 -3
- package/dist/core/index.js +615 -1758
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.mjs +579 -1727
- package/dist/core/index.mjs.map +1 -1
- package/dist/{dataLayerProvider-D8o6ZnKW.d.ts → dataLayerProvider-CHYrQ5pB.d.cts} +1 -1
- package/dist/{dataLayerProvider-D0MoZMjH.d.cts → dataLayerProvider-MDTxXq2l.d.ts} +1 -1
- package/dist/impl/couch/index.d.cts +6 -22
- package/dist/impl/couch/index.d.ts +6 -22
- package/dist/impl/couch/index.js +598 -1769
- package/dist/impl/couch/index.js.map +1 -1
- package/dist/impl/couch/index.mjs +579 -1755
- package/dist/impl/couch/index.mjs.map +1 -1
- package/dist/impl/static/index.d.cts +22 -6
- package/dist/impl/static/index.d.ts +22 -6
- package/dist/impl/static/index.js +617 -1629
- package/dist/impl/static/index.js.map +1 -1
- package/dist/impl/static/index.mjs +607 -1624
- package/dist/impl/static/index.mjs.map +1 -1
- package/dist/index.d.cts +64 -56
- package/dist/index.d.ts +64 -56
- package/dist/index.js +1000 -2161
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +970 -2127
- package/dist/index.mjs.map +1 -1
- package/dist/pouch/index.js +3 -0
- package/dist/pouch/index.js.map +1 -1
- package/dist/pouch/index.mjs +3 -0
- package/dist/pouch/index.mjs.map +1 -1
- package/docs/navigators-architecture.md +2 -9
- package/package.json +3 -3
- package/src/core/interfaces/classroomDB.ts +5 -13
- package/src/core/interfaces/contentSource.ts +6 -66
- package/src/core/interfaces/courseDB.ts +2 -7
- package/src/core/navigators/Pipeline.ts +24 -53
- package/src/core/navigators/PipelineAssembler.ts +1 -1
- package/src/core/navigators/defaults.ts +84 -0
- package/src/core/navigators/{hierarchyDefinition.ts → filters/hierarchyDefinition.ts} +11 -25
- package/src/core/navigators/{interferenceMitigator.ts → filters/interferenceMitigator.ts} +10 -24
- package/src/core/navigators/{relativePriority.ts → filters/relativePriority.ts} +10 -24
- package/src/core/navigators/filters/userTagPreference.ts +1 -16
- package/src/core/navigators/{CompositeGenerator.ts → generators/CompositeGenerator.ts} +15 -64
- package/src/core/navigators/{elo.ts → generators/elo.ts} +13 -63
- package/src/core/navigators/{srs.ts → generators/srs.ts} +11 -40
- package/src/core/navigators/generators/types.ts +1 -1
- package/src/core/navigators/index.ts +36 -91
- package/src/impl/couch/classroomDB.ts +100 -103
- package/src/impl/couch/courseDB.ts +5 -81
- package/src/impl/couch/pouchdb-setup.ts +7 -0
- package/src/impl/static/StaticDataUnpacker.ts +50 -1
- package/src/impl/static/courseDB.ts +76 -37
- package/src/study/SessionController.ts +122 -202
- package/src/study/SourceMixer.ts +65 -0
- package/src/study/TagFilteredContentSource.ts +49 -92
- package/src/study/index.ts +1 -0
- package/src/study/services/CardHydrationService.ts +165 -81
- package/src/util/dataDirectory.ts +1 -1
- package/src/util/index.ts +0 -1
- package/tests/core/navigators/CompositeGenerator.test.ts +44 -168
- package/tests/core/navigators/Pipeline.test.ts +5 -72
- package/tests/core/navigators/PipelineAssembler.test.ts +8 -58
- package/tests/core/navigators/navigators.test.ts +118 -151
- package/src/core/navigators/hardcodedOrder.ts +0 -163
- package/src/util/tuiLogger.ts +0 -139
- /package/src/core/navigators/{inferredPreference.ts → filters/inferredPreferenceStub.ts} +0 -0
- /package/src/core/navigators/{userGoal.ts → filters/userGoalStub.ts} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/impl/common/SyncStrategy.ts","../../../src/core/interfaces/adminDB.ts","../../../src/core/interfaces/classroomDB.ts","../../../src/util/logger.ts","../../../src/impl/couch/pouchdb-setup.ts","../../../src/util/Loggable.ts","../../../src/impl/couch/updateQueue.ts","../../../src/core/types/types-legacy.ts","../../../src/impl/couch/clientCache.ts","../../../src/impl/couch/courseAPI.ts","../../../src/impl/couch/courseLookupDB.ts","../../../src/core/navigators/CompositeGenerator.ts","../../../src/core/navigators/Pipeline.ts","../../../src/core/navigators/PipelineAssembler.ts","../../../src/core/navigators/elo.ts","../../../src/core/navigators/filters/eloDistance.ts","../../../src/core/navigators/filters/userTagPreference.ts","../../../src/core/navigators/filters/index.ts","../../../src/core/navigators/filters/types.ts","../../../src/core/navigators/generators/index.ts","../../../src/core/navigators/generators/types.ts","../../../src/core/navigators/hardcodedOrder.ts","../../../src/core/navigators/hierarchyDefinition.ts","../../../src/core/navigators/inferredPreference.ts","../../../src/core/navigators/interferenceMitigator.ts","../../../src/core/navigators/relativePriority.ts","../../../src/core/navigators/srs.ts","../../../src/core/navigators/userGoal.ts","../../../src/core/navigators/index.ts","../../../src/impl/couch/courseDB.ts","../../../src/impl/couch/classroomDB.ts","../../../src/study/TagFilteredContentSource.ts","../../../src/core/interfaces/contentSource.ts","../../../src/core/interfaces/courseDB.ts","../../../src/core/interfaces/dataLayerProvider.ts","../../../src/core/interfaces/userDB.ts","../../../src/core/interfaces/index.ts","../../../src/core/types/user.ts","../../../src/core/types/strategyState.ts","../../../src/core/util/index.ts","../../../src/core/bulkImport/cardProcessor.ts","../../../src/core/bulkImport/types.ts","../../../src/core/bulkImport/index.ts","../../../src/core/index.ts","../../../src/util/tuiLogger.ts","../../../src/util/dataDirectory.ts","../../../src/impl/common/userDBHelpers.ts","../../../src/impl/couch/user-course-relDB.ts","../../../src/impl/common/BaseUserDB.ts","../../../src/impl/common/index.ts","../../../src/factory.ts","../../../src/impl/couch/adminDB.ts","../../../src/impl/couch/auth.ts","../../../src/impl/couch/CouchDBSyncStrategy.ts","../../../src/impl/couch/index.ts"],"sourcesContent":["// packages/db/src/impl/common/SyncStrategy.ts\n\nimport type { AccountCreationResult, AuthenticationResult } from './types';\n\n/**\n * Strategy interface for handling user data synchronization\n * Different implementations handle remote sync vs local-only storage\n */\nexport interface SyncStrategy {\n /**\n * Set up the remote database for a user\n * @param username The username to set up remote DB for\n * @returns PouchDB database instance (may be same as local for no-op)\n */\n setupRemoteDB(username: string): PouchDB.Database;\n\n /**\n * Get the database to use for write operations (local-first approach)\n * @param username The username to get write DB for\n * @returns PouchDB database instance for write operations\n */\n getWriteDB?(username: string): PouchDB.Database;\n\n /**\n * Start synchronization between local and remote databases\n * @param localDB The local PouchDB instance\n * @param remoteDB The remote PouchDB instance\n */\n startSync(localDB: PouchDB.Database, remoteDB: PouchDB.Database): void;\n\n /**\n * Stop synchronization (optional - for cleanup)\n */\n stopSync?(): void;\n\n /**\n * Whether this strategy supports account creation\n */\n canCreateAccount(): boolean;\n\n /**\n * Whether this strategy supports authentication\n */\n canAuthenticate(): boolean;\n\n /**\n * Create a new user account (if supported)\n * @param username The username for the new account\n * @param password The password for the new account\n */\n createAccount?(username: string, password: string): Promise<AccountCreationResult>;\n\n /**\n * Authenticate a user (if supported)\n * @param username The username to authenticate\n * @param password The password to authenticate with\n */\n authenticate?(username: string, password: string): Promise<AuthenticationResult>;\n\n /**\n * Log out the current user (if supported)\n */\n logout?(): Promise<AuthenticationResult>;\n\n /**\n * Get the current logged-in username\n * Returns the username if logged in, or a default guest username\n */\n getCurrentUsername(): Promise<string>;\n}\n\n/**\n * Base class for sync strategies with common functionality\n */\nexport abstract class BaseSyncStrategy implements SyncStrategy {\n abstract setupRemoteDB(username: string): PouchDB.Database;\n abstract startSync(localDB: PouchDB.Database, remoteDB: PouchDB.Database): void;\n abstract canCreateAccount(): boolean;\n abstract canAuthenticate(): boolean;\n abstract getCurrentUsername(): Promise<string>;\n\n stopSync?(): void {\n // Default no-op implementation\n }\n\n async createAccount(_username: string, _password: string): Promise<AccountCreationResult> {\n throw new Error('Account creation not supported by this sync strategy');\n }\n\n async authenticate(_username: string, _password: string): Promise<AuthenticationResult> {\n throw new Error('Authentication not supported by this sync strategy');\n }\n\n async logout(): Promise<AuthenticationResult> {\n throw new Error('Logout not supported by this sync strategy');\n }\n}\n","import { ClassroomConfig, CourseConfig } from '@vue-skuilder/common';\n\n/**\n * Admin functionality\n */\nexport interface AdminDBInterface {\n /**\n * Get all users\n */\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n getUsers(): Promise<PouchDB.Core.Document<{}>[]>;\n\n /**\n * Get all courses\n */\n getCourses(): Promise<CourseConfig[]>;\n\n /**\n * Remove a course\n */\n removeCourse(id: string): Promise<PouchDB.Core.Response>;\n\n /**\n * Get all classrooms\n */\n getClassrooms(): Promise<(ClassroomConfig & { _id: string })[]>;\n}\n","import { ClassroomConfig } from '@vue-skuilder/common';\nimport { ScheduledCard } from '../types/user';\nimport { StudySessionNewItem, StudySessionReviewItem } from './contentSource';\n\n/**\n * Classroom management\n */\nexport interface ClassroomDBInterface {\n /**\n * Get classroom config\n */\n getConfig(): ClassroomConfig;\n\n /**\n * Get assigned content\n */\n getAssignedContent(): Promise<AssignedContent[]>;\n}\n\nexport interface TeacherClassroomDBInterface extends ClassroomDBInterface {\n /**\n * For teacher interfaces: assign content\n */\n assignContent?(content: AssignedContent): Promise<boolean>;\n\n /**\n * For teacher interfaces: remove content\n */\n removeContent?(content: AssignedContent): Promise<void>;\n}\n\nexport interface StudentClassroomDBInterface extends ClassroomDBInterface {\n /**\n * For student interfaces: get pending reviews\n */\n getPendingReviews?(): Promise<(StudySessionReviewItem & ScheduledCard)[]>;\n\n /**\n * For student interfaces: get new cards\n */\n getNewCards?(limit?: number): Promise<StudySessionNewItem[]>;\n}\n\nexport type AssignedContent = AssignedCourse | AssignedTag | AssignedCard;\n\nexport interface AssignedTag extends ContentBase {\n type: 'tag';\n tagID: string;\n}\nexport interface AssignedCourse extends ContentBase {\n type: 'course';\n}\nexport interface AssignedCard extends ContentBase {\n type: 'card';\n cardID: string;\n}\n\ninterface ContentBase {\n type: 'course' | 'tag' | 'card';\n /**\n * Username of the assigning teacher.\n */\n assignedBy: string;\n /**\n * Date the content was assigned.\n */\n assignedOn: moment.Moment;\n /**\n * A 'due' date for this assigned content, for scheduling content\n * in advance. Content will not be actively pushed to students until\n * this date.\n */\n activeOn: moment.Moment;\n courseID: string;\n}\n","/**\n * Simple logging utility for @vue-skuilder/db package\n *\n * This utility provides environment-aware logging with ESLint suppressions\n * to resolve console statement violations while maintaining logging functionality.\n */\n\nconst isDevelopment = typeof process !== 'undefined' && process.env.NODE_ENV === 'development';\n\nexport const logger = {\n /**\n * Debug-level logging - only shown in development\n */\n debug: (message: string, ...args: any[]): void => {\n if (isDevelopment) {\n // eslint-disable-next-line no-console\n console.debug(`[DB:DEBUG] ${message}`, ...args);\n }\n },\n\n /**\n * Info-level logging - general information\n */\n info: (message: string, ...args: any[]): void => {\n // eslint-disable-next-line no-console\n console.info(`[DB:INFO] ${message}`, ...args);\n },\n\n /**\n * Warning-level logging - potential issues\n */\n warn: (message: string, ...args: any[]): void => {\n // eslint-disable-next-line no-console\n console.warn(`[DB:WARN] ${message}`, ...args);\n },\n\n /**\n * Error-level logging - serious problems\n */\n error: (message: string, ...args: any[]): void => {\n // eslint-disable-next-line no-console\n console.error(`[DB:ERROR] ${message}`, ...args);\n },\n\n /**\n * Log function for backward compatibility with existing log() usage\n */\n log: (message: string, ...args: any[]): void => {\n if (isDevelopment) {\n // eslint-disable-next-line no-console\n console.log(`[DB:LOG] ${message}`, ...args);\n }\n },\n};\n","import PouchDB from 'pouchdb';\nimport PouchDBFind from 'pouchdb-find';\nimport PouchDBAuth from '@nilock2/pouchdb-authentication';\n\n// Register plugins\nPouchDB.plugin(PouchDBFind);\nPouchDB.plugin(PouchDBAuth);\n\n// Configure PouchDB globally\nPouchDB.defaults({\n // ajax: {\n // timeout: 60000,\n // },\n});\n\nexport default PouchDB;\n","export abstract class Loggable {\n protected abstract readonly _className: string;\n protected log(...args: unknown[]): void {\n // eslint-disable-next-line no-console\n console.log(`LOG-${this._className}@${new Date()}:`, ...args);\n }\n protected error(...args: unknown[]): void {\n // eslint-disable-next-line no-console\n console.error(`ERROR-${this._className}@${new Date()}:`, ...args);\n }\n}\n","import { Loggable } from '../../util/Loggable';\nimport { logger } from '../../util/logger';\n\nexport type Update<T> = Partial<T> | ((x: T) => T);\n\nexport default class UpdateQueue extends Loggable {\n _className: string = 'UpdateQueue';\n private pendingUpdates: {\n [index: string]: Update<unknown>[];\n } = {};\n private inprogressUpdates: {\n [index: string]: boolean;\n } = {};\n\n private readDB: PouchDB.Database; // Database for read operations\n private writeDB: PouchDB.Database; // Database for write operations (local-first)\n\n /**\n * Queues an update for a document and applies it with conflict resolution.\n *\n * @param id - Document ID to update\n * @param update - Partial object or function that transforms the document\n * @returns Promise resolving to the updated document\n *\n * @throws {PouchError} with status 404 if document doesn't exist\n *\n * @remarks\n * **Error Handling Pattern:**\n * - This method does NOT create documents if they don't exist\n * - Callers are responsible for handling 404 errors and creating documents\n * - This design maintains separation of concerns (UpdateQueue handles conflicts, callers handle lifecycle)\n *\n * @example\n * ```typescript\n * try {\n * await updateQueue.update(docId, (doc) => ({ ...doc, field: newValue }));\n * } catch (e) {\n * if ((e as PouchError).status === 404) {\n * // Create the document with initial values\n * await db.put({ _id: docId, field: newValue, ...initialFields });\n * }\n * }\n * ```\n */\n public update<T extends PouchDB.Core.Document<object>>(\n id: PouchDB.Core.DocumentId,\n update: Update<T>\n ) {\n logger.debug(`Update requested on doc: ${id}`);\n if (this.pendingUpdates[id]) {\n this.pendingUpdates[id].push(update);\n } else {\n this.pendingUpdates[id] = [update];\n }\n return this.applyUpdates<T>(id);\n }\n\n constructor(readDB: PouchDB.Database, writeDB?: PouchDB.Database) {\n super();\n // PouchDB.debug.enable('*');\n this.readDB = readDB;\n this.writeDB = writeDB || readDB; // Default to readDB if writeDB not provided\n logger.debug(`UpdateQ initialized...`);\n void this.readDB.info().then((i) => {\n logger.debug(`db info: ${JSON.stringify(i)}`);\n });\n }\n\n private async applyUpdates<T extends PouchDB.Core.Document<object>>(\n id: string\n ): Promise<T & PouchDB.Core.GetMeta & PouchDB.Core.RevisionIdMeta> {\n logger.debug(`Applying updates on doc: ${id}`);\n if (this.inprogressUpdates[id]) {\n // Poll instead of recursing to avoid infinite recursion\n while (this.inprogressUpdates[id]) {\n await new Promise(resolve => setTimeout(resolve, Math.random() * 50));\n }\n return this.applyUpdates<T>(id);\n } else {\n if (this.pendingUpdates[id] && this.pendingUpdates[id].length > 0) {\n this.inprogressUpdates[id] = true;\n\n const MAX_RETRIES = 5;\n for (let i = 0; i < MAX_RETRIES; i++) {\n try {\n const doc = await this.readDB.get<T>(id);\n\n // Create a new doc object to apply updates to for this attempt\n let updatedDoc = { ...doc };\n\n // Note: This loop is not fully safe if updates are functions that depend on a specific doc state\n // that might change between retries. But for simple object merges, it's okay.\n const updatesToApply = [...this.pendingUpdates[id]];\n for (const update of updatesToApply) {\n if (typeof update === 'function') {\n updatedDoc = { ...updatedDoc, ...update(updatedDoc) };\n } else {\n updatedDoc = {\n ...updatedDoc,\n ...update,\n };\n }\n }\n\n await this.writeDB.put<T>(updatedDoc);\n\n // Success! Remove the updates we just applied.\n this.pendingUpdates[id].splice(0, updatesToApply.length);\n\n if (this.pendingUpdates[id].length === 0) {\n this.inprogressUpdates[id] = false;\n delete this.inprogressUpdates[id];\n } else {\n // More updates came in, run again.\n return this.applyUpdates<T>(id);\n }\n return updatedDoc as any; // success, exit loop and function\n } catch (e: any) {\n if (e.name === 'conflict' && i < MAX_RETRIES - 1) {\n logger.warn(`Conflict on update for doc ${id}, retry #${i + 1}`);\n await new Promise((res) => setTimeout(res, 50 * Math.random()));\n // continue to next iteration of the loop\n } else if (e.name === 'not_found' && i === 0) {\n // Document not present - throw to caller for initialization\n logger.warn(`Update failed for ${id} - does not exist. Throwing to caller.`);\n delete this.inprogressUpdates[id];\n throw e; // Let caller handle\n } else {\n // Max retries reached or a non-conflict error\n delete this.inprogressUpdates[id];\n if (this.pendingUpdates[id]) {\n delete this.pendingUpdates[id];\n }\n logger.error(`Error on attemped update (retry ${i}): ${JSON.stringify(e)}`);\n throw e; // Let caller handle\n }\n }\n }\n // This should be unreachable, but it satisfies the compiler that a value is always returned or an error thrown.\n throw new Error(`UpdateQueue failed for doc ${id} after ${MAX_RETRIES} retries.`);\n } else {\n throw new Error(`Empty Updates Queue Triggered`);\n }\n }\n }\n}\n","import { CourseElo, Answer, Evaluation } from '@vue-skuilder/common';\nimport { Moment } from 'moment';\nimport { logger } from '../../util/logger';\n\nexport const GuestUsername: string = 'sk-guest-';\n\nexport const log = (message: string): void => {\n logger.log(message);\n};\n\nexport enum DocType {\n DISPLAYABLE_DATA = 'DISPLAYABLE_DATA',\n CARD = 'CARD',\n DATASHAPE = 'DATASHAPE',\n QUESTIONTYPE = 'QUESTION',\n VIEW = 'VIEW',\n PEDAGOGY = 'PEDAGOGY',\n CARDRECORD = 'CARDRECORD',\n SCHEDULED_CARD = 'SCHEDULED_CARD',\n TAG = 'TAG',\n NAVIGATION_STRATEGY = 'NAVIGATION_STRATEGY',\n STRATEGY_STATE = 'STRATEGY_STATE',\n}\n\nexport interface QualifiedCardID {\n courseID: string;\n cardID: string;\n}\n\n/**\n * Interface for all data on course content and pedagogy stored\n * in the c/pouch database.\n */\nexport interface SkuilderCourseData {\n course: string;\n docType: DocType;\n}\n\nexport interface Tag extends SkuilderCourseData {\n docType: DocType.TAG;\n name: string;\n snippet: string; // 200 char description of the tag\n wiki: string; // 3000 char md-friendly description\n taggedCards: PouchDB.Core.DocumentId[];\n author: string;\n}\nexport interface TagStub {\n name: string;\n snippet: string;\n count: number; // the number of cards that have this tag applied\n}\n\nexport interface CardData extends SkuilderCourseData {\n docType: DocType.CARD;\n id_displayable_data: PouchDB.Core.DocumentId[];\n id_view: PouchDB.Core.DocumentId;\n elo: CourseElo;\n author: string;\n}\n\n/** A list of populated courses in the DB */\nexport interface CourseListData extends PouchDB.Core.Response {\n courses: string[];\n}\n\n/**\n * The data used to hydrate viewable components (questions, info, etc)\n */\nexport interface DisplayableData extends SkuilderCourseData {\n docType: DocType.DISPLAYABLE_DATA;\n author?: string;\n id_datashape: PouchDB.Core.DocumentId;\n data: Field[];\n _attachments?: { [index: string]: PouchDB.Core.FullAttachment };\n}\n\nexport interface Field {\n data: unknown;\n name: string;\n}\n\nexport interface DataShapeData extends SkuilderCourseData {\n docType: DocType.DATASHAPE;\n _id: PouchDB.Core.DocumentId;\n questionTypes: PouchDB.Core.DocumentId[];\n}\n\nexport interface QuestionData extends SkuilderCourseData {\n docType: DocType.QUESTIONTYPE;\n _id: PouchDB.Core.DocumentId;\n viewList: string[];\n dataShapeList: PouchDB.Core.DocumentId[];\n}\n\nexport const DocTypePrefixes = {\n [DocType.CARD]: 'c',\n [DocType.DISPLAYABLE_DATA]: 'dd',\n [DocType.TAG]: 'TAG',\n [DocType.CARDRECORD]: 'cardH',\n [DocType.SCHEDULED_CARD]: 'card_review_',\n // Add other doctypes here as they get prefixed IDs\n [DocType.DATASHAPE]: 'DATASHAPE',\n [DocType.QUESTIONTYPE]: 'QUESTION',\n [DocType.VIEW]: 'VIEW',\n [DocType.PEDAGOGY]: 'PEDAGOGY',\n [DocType.NAVIGATION_STRATEGY]: 'NAVIGATION_STRATEGY',\n [DocType.STRATEGY_STATE]: 'STRATEGY_STATE',\n} as const;\n\nexport interface CardHistory<T extends CardRecord> {\n _id: PouchDB.Core.DocumentId;\n /**\n * The CouchDB id of the card\n */\n cardID: PouchDB.Core.DocumentId;\n\n /**\n * The ID of the course\n */\n courseID: string;\n\n /**\n * The to-date largest interval between successful\n * card reviews. `0` indicates no successful reviews.\n */\n bestInterval: number;\n\n /**\n * The number of times that a card has been\n * failed in review\n */\n lapses: number;\n\n /**\n * The number of consecutive successful impressions\n * on this card\n */\n streak: number;\n\n records: T[];\n}\n\nexport interface CardRecord {\n /**\n * The CouchDB id of the card\n */\n cardID: string;\n /**\n * The ID of the course\n */\n courseID: string;\n /**\n * Number of milliseconds that the user spent before dismissing\n * the card (ie, \"I've read this\" or \"here is my answer\")\n *\n * //TODO: this (sometimes?) wants to be replaced with a rich\n * recording of user activity in working the question\n */\n timeSpent: number;\n /**\n * The date-time that the card was rendered. timeStamp + timeSpent will give the\n * time of user submission.\n */\n timeStamp: Moment;\n}\n\nexport interface QuestionRecord extends CardRecord, Evaluation {\n userAnswer: Answer;\n /**\n * The number of incorrect user submissions prededing this submisstion.\n *\n * eg, if a user is asked 7*6=__, submitting 46, 48, 42 will result in three\n * records being created having 0, 1, and 2 as their recorded 'priorAttempts' values\n */\n priorAttemps: number;\n}\n","// todo: something good here instead\n\nconst CLIENT_CACHE: {\n [k: string]: unknown;\n} = {};\n\nexport async function GET_CACHED<K>(k: string, f?: (x: string) => Promise<K>): Promise<K> {\n if (CLIENT_CACHE[k]) {\n // console.log('returning a cached item');\n return CLIENT_CACHE[k] as K;\n }\n\n CLIENT_CACHE[k] = f ? await f(k) : await GET_ITEM(k);\n return GET_CACHED(k);\n}\n\nasync function GET_ITEM(k: string): Promise<unknown> {\n throw new Error(`No implementation found for GET_CACHED(${k})`);\n}\n","import pouch from './pouchdb-setup';\nimport { createPouchDBConfig } from '.';\nimport { ENV } from '@db/factory';\n// import { DataShape } from '../..base-course/Interfaces/DataShape';\nimport { NameSpacer, ShapeDescriptor } from '@vue-skuilder/common';\nimport { CourseConfig, DataShape } from '@vue-skuilder/common';\nimport { CourseElo, blankCourseElo, toCourseElo } from '@vue-skuilder/common';\nimport { CourseDB, createTag } from './courseDB';\nimport { CardData, DisplayableData, DocType, Tag, DocTypePrefixes } from '../../core/types/types-legacy';\nimport { prepareNote55 } from '@vue-skuilder/common';\nimport { BaseUser } from '../common';\nimport { logger } from '@db/util/logger';\nimport { v4 as uuidv4 } from 'uuid';\n\n/**\n *\n * @param courseID id of the course (quilt) being added to\n * @param codeCourse\n * @param shape\n * @param data the datashape data - data required for this shape\n * @param author\n * @param uploads optional additional media uploads: img0, img1, ..., aud0, aud1,...\n * @returns\n */\nexport async function addNote55(\n courseID: string,\n codeCourse: string,\n shape: DataShape,\n data: unknown,\n author: string,\n tags: string[],\n uploads?: { [x: string]: PouchDB.Core.FullAttachment },\n elo: CourseElo = blankCourseElo()\n): Promise<PouchDB.Core.Response> {\n const db = getCourseDB(courseID);\n const payload = prepareNote55(courseID, codeCourse, shape, data, author, tags, uploads);\n const _id = `${DocTypePrefixes[DocType.DISPLAYABLE_DATA]}-${uuidv4()}`;\n const result = await db.put<DisplayableData>({ ...payload, _id });\n\n const dataShapeId = NameSpacer.getDataShapeString({\n course: codeCourse,\n dataShape: shape.name,\n });\n\n if (result.ok) {\n try {\n // create cards\n await createCards(courseID, dataShapeId, result.id, tags, elo, author);\n } catch (error) {\n // Handle CouchDB errors which often have a 'reason' property\n let errorMessage = 'Unknown error';\n if (error instanceof Error) {\n errorMessage = error.message;\n } else if (error && typeof error === 'object' && 'reason' in error) {\n errorMessage = error.reason as string;\n } else if (error && typeof error === 'object' && 'message' in error) {\n errorMessage = error.message as string;\n } else {\n errorMessage = String(error);\n }\n\n logger.error(`[addNote55] Failed to create cards for note ${result.id}: ${errorMessage}`);\n // Add info to result to indicate card creation failed\n (result as any).cardCreationFailed = true;\n (result as any).cardCreationError = errorMessage;\n }\n } else {\n logger.error(`[addNote55] Error adding note. Result: ${JSON.stringify(result)}`);\n }\n\n return result;\n}\n\nasync function createCards(\n courseID: string,\n datashapeID: PouchDB.Core.DocumentId,\n noteID: PouchDB.Core.DocumentId,\n tags: string[],\n elo: CourseElo = blankCourseElo(),\n author: string\n): Promise<void> {\n const cfg = await getCredentialledCourseConfig(courseID);\n const dsDescriptor = NameSpacer.getDataShapeDescriptor(datashapeID);\n let questionViewTypes: string[] = [];\n\n for (const ds of cfg.dataShapes) {\n if (ds.name === datashapeID) {\n questionViewTypes = ds.questionTypes;\n }\n }\n\n if (questionViewTypes.length === 0) {\n const errorMsg = `No questionViewTypes found for datashapeID: ${datashapeID} in course config. Cards cannot be created.`;\n logger.error(errorMsg);\n throw new Error(errorMsg);\n }\n\n for (const questionView of questionViewTypes) {\n await createCard(questionView, courseID, dsDescriptor, noteID, tags, elo, author);\n }\n}\n\nasync function createCard(\n questionViewName: string,\n courseID: string,\n dsDescriptor: ShapeDescriptor,\n noteID: string,\n tags: string[],\n elo: CourseElo = blankCourseElo(),\n author: string\n): Promise<void> {\n const qDescriptor = NameSpacer.getQuestionDescriptor(questionViewName);\n const cfg = await getCredentialledCourseConfig(courseID);\n\n for (const rQ of cfg.questionTypes) {\n if (rQ.name === questionViewName) {\n for (const view of rQ.viewList) {\n await addCard(\n courseID,\n dsDescriptor.course,\n [noteID],\n NameSpacer.getViewString({\n course: qDescriptor.course,\n questionType: qDescriptor.questionType,\n view,\n }),\n elo,\n tags,\n author\n );\n }\n }\n }\n}\n\n/**\n *\n * Adds a card to the DB. This function is called\n * as a side effect of adding either a View or\n * DisplayableData item.\n * @param course The name of the course that the card belongs to\n * @param id_displayable_data C/PouchDB ID of the data used to hydrate the view\n * @param id_view C/PouchDB ID of the view used to display the card\n *\n * @package\n */\nasync function addCard(\n courseID: string,\n course: string,\n id_displayable_data: PouchDB.Core.DocumentId[],\n id_view: PouchDB.Core.DocumentId,\n elo: CourseElo,\n tags: string[],\n author: string\n): Promise<PouchDB.Core.Response> {\n const db = getCourseDB(courseID);\n const _id = `${DocTypePrefixes[DocType.CARD]}-${uuidv4()}`;\n const card = await db.put<CardData>({\n _id,\n course,\n id_displayable_data,\n id_view,\n docType: DocType.CARD,\n elo: elo || toCourseElo(990 + Math.round(20 * Math.random())),\n author,\n });\n for (const tag of tags) {\n logger.info(`adding tag: ${tag} to card ${card.id}`);\n await addTagToCard(courseID, card.id, tag, author, false);\n }\n return card;\n}\n\nexport async function getCredentialledCourseConfig(courseID: string): Promise<CourseConfig> {\n try {\n const db = getCourseDB(courseID);\n const ret = await db.get<CourseConfig>('CourseConfig');\n ret.courseID = courseID;\n logger.info(`Returning course config: ${JSON.stringify(ret)}`);\n return ret;\n } catch (e) {\n logger.error(`Error fetching config for ${courseID}:`, e);\n throw e;\n }\n}\n\n/**\n Assciates a tag with a card.\n\n NB: DB stores tags as separate documents, with a list of card IDs.\n Consider renaming to `addCardToTag` to reflect this.\n\n NB: tags are created if they don't already exist\n\n @param updateELO whether to update the ELO of the card with the new tag. Default true.\n @package\n*/\nexport async function addTagToCard(\n courseID: string,\n cardID: string,\n tagID: string,\n author: string,\n updateELO: boolean = true\n): Promise<PouchDB.Core.Response> {\n // todo: possible future perf. hit if tags have large #s of taggedCards.\n // In this case, should be converted to a server-request\n const prefixedTagID = getTagID(tagID);\n const courseDB = getCourseDB(courseID);\n const courseApi = new CourseDB(courseID, async () => {\n const dummySyncStrategy = {\n setupRemoteDB: () => null as any,\n startSync: () => {},\n canCreateAccount: () => false,\n canAuthenticate: () => false,\n getCurrentUsername: async () => 'DummyUser',\n };\n return BaseUser.Dummy(dummySyncStrategy);\n });\n try {\n logger.info(`Applying tag ${tagID} to card ${courseID + '-' + cardID}...`);\n const tag = await courseDB.get<Tag>(prefixedTagID);\n if (!tag.taggedCards.includes(cardID)) {\n tag.taggedCards.push(cardID);\n\n if (updateELO) {\n try {\n const eloData = await courseApi.getCardEloData([cardID]);\n const elo = eloData[0];\n elo.tags[tagID] = {\n count: 0,\n score: elo.global.score, // todo: or 1000?\n };\n await updateCardElo(courseID, cardID, elo);\n } catch (error) {\n logger.error('Failed to update ELO data for card:', cardID, error);\n }\n }\n\n return courseDB.put<Tag>(tag);\n } else throw new AlreadyTaggedErr(`Card ${cardID} is already tagged with ${tagID}`);\n } catch (e) {\n if (e instanceof AlreadyTaggedErr) {\n throw e;\n }\n\n await createTag(courseID, tagID, author);\n return addTagToCard(courseID, cardID, tagID, author, updateELO);\n }\n}\n\nasync function updateCardElo(courseID: string, cardID: string, elo: CourseElo) {\n if (elo) {\n // checking against null, undefined, NaN\n const cDB = getCourseDB(courseID);\n const card = await cDB.get<CardData>(cardID);\n logger.debug(`Replacing ${JSON.stringify(card.elo)} with ${JSON.stringify(elo)}`);\n card.elo = elo;\n return cDB.put(card); // race conditions - is it important? probably not (net-zero effect)\n }\n}\n\nclass AlreadyTaggedErr extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'AlreadyTaggedErr';\n }\n}\n\nexport function getTagID(tagName: string): string {\n const tagPrefix = DocType.TAG.valueOf() + '-';\n if (tagName.indexOf(tagPrefix) === 0) {\n return tagName;\n } else {\n return tagPrefix + tagName;\n }\n}\n\nexport function getCourseDB(courseID: string): PouchDB.Database {\n const dbName = `coursedb-${courseID}`;\n return new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,\n createPouchDBConfig()\n );\n}\n","import pouch from './pouchdb-setup';\nimport { ENV } from '@db/factory';\nimport { logger } from '../../util/logger';\n\nconst courseLookupDBTitle = 'coursedb-lookup';\n\ninterface CourseLookupDoc {\n _id: string;\n _rev: string;\n name: string;\n disambiguator?: string;\n}\n\nlogger.debug(`COURSELOOKUP FILE RUNNING`);\n\n/**\n * A Lookup table of existant courses. Each docID in this DB correspondes to a\n * course database whose name is `coursedb-{docID}`\n */\nexport default class CourseLookup {\n // [ ] this db should be read only for public, admin-only for write\n // Cache for the PouchDB instance\n private static _dbInstance: PouchDB.Database | null = null;\n\n /**\n * Static getter for the PouchDB database instance.\n * Connects using ENV variables and caches the instance.\n * Throws an error if required ENV variables are not set.\n */\n private static get _db(): PouchDB.Database {\n // Return cached instance if available\n if (this._dbInstance) {\n return this._dbInstance;\n }\n\n // --- Check required environment variables ---\n if (ENV.COUCHDB_SERVER_URL === 'NOT_SET' || !ENV.COUCHDB_SERVER_URL) {\n throw new Error(\n 'CourseLookup.db: COUCHDB_SERVER_URL is not set. Ensure initializeDataLayer has been called with valid configuration.'\n );\n }\n if (ENV.COUCHDB_SERVER_PROTOCOL === 'NOT_SET' || !ENV.COUCHDB_SERVER_PROTOCOL) {\n throw new Error(\n 'CourseLookup.db: COUCHDB_SERVER_PROTOCOL is not set. Ensure initializeDataLayer has been called with valid configuration.'\n );\n }\n\n // --- Construct connection options ---\n const dbUrl = `${ENV.COUCHDB_SERVER_PROTOCOL}://${ENV.COUCHDB_SERVER_URL}/${courseLookupDBTitle}`;\n const options: PouchDB.Configuration.RemoteDatabaseConfiguration = {\n // fetch: (url, opts) => { // Optional: Add for debugging network requests\n // console.log('PouchDB fetch:', url, opts);\n // return pouch.fetch(url, opts);\n // }\n };\n\n // Add authentication if both username and password are provided\n if (ENV.COUCHDB_USERNAME && ENV.COUCHDB_PASSWORD) {\n options.auth = {\n username: ENV.COUCHDB_USERNAME,\n password: ENV.COUCHDB_PASSWORD,\n };\n logger.info(`CourseLookup: Connecting to ${dbUrl} with authentication.`);\n } else {\n logger.info(`CourseLookup: Connecting to ${dbUrl} without authentication.`);\n }\n\n // --- Create and cache the PouchDB instance ---\n try {\n this._dbInstance = new pouch(dbUrl, options);\n logger.info(`CourseLookup: Database instance created for ${courseLookupDBTitle}.`);\n return this._dbInstance;\n } catch (error) {\n logger.error(`CourseLookup: Failed to create PouchDB instance for ${dbUrl}`, error);\n // Reset cache attempt on failure\n this._dbInstance = null;\n // Re-throw the error to indicate connection failure\n throw new Error(\n `CourseLookup: Failed to initialize database connection: ${\n error instanceof Error ? error.message : String(error)\n }`\n );\n }\n }\n\n /**\n * Adds a new course to the lookup database, and returns the courseID\n * @param courseName\n * @returns\n */\n static async add(courseName: string): Promise<string> {\n const resp = await CourseLookup._db.post({\n name: courseName,\n });\n\n return resp.id;\n }\n\n /**\n * Adds a new course to the lookup database with a specific courseID\n * @param courseId The specific course ID to use\n * @param courseName The course name\n * @param disambiguator Optional disambiguator\n * @returns Promise<void>\n */\n static async addWithId(\n courseId: string,\n courseName: string,\n disambiguator?: string\n ): Promise<void> {\n const doc: Omit<CourseLookupDoc, '_rev'> = {\n _id: courseId,\n name: courseName,\n };\n\n if (disambiguator) {\n doc.disambiguator = disambiguator;\n }\n\n await CourseLookup._db.put(doc);\n }\n\n /**\n * Removes a course from the index\n * @param courseID\n */\n static async delete(courseID: string): Promise<PouchDB.Core.Response> {\n const doc = await CourseLookup._db.get(courseID);\n return await CourseLookup._db.remove(doc);\n }\n\n // [ ] rename to allCourses()\n static async allCourseWare(): Promise<CourseLookupDoc[]> {\n const resp = await CourseLookup._db.allDocs<CourseLookupDoc>({\n include_docs: true,\n });\n\n return resp.rows.map((row) => row.doc!);\n }\n\n static async updateDisambiguator(\n courseID: string,\n disambiguator?: string\n ): Promise<PouchDB.Core.Response> {\n const doc = await CourseLookup._db.get<CourseLookupDoc>(courseID);\n doc.disambiguator = disambiguator;\n return await CourseLookup._db.put(doc);\n }\n\n static async isCourse(courseID: string): Promise<boolean> {\n try {\n await CourseLookup._db.get(courseID);\n return true;\n } catch (error) {\n logger.info(`Courselookup failed:`, error);\n return false;\n }\n }\n}\n","import { ContentNavigator } from './index';\nimport type { WeightedCard } from './index';\nimport type { ContentNavigationStrategyData } from '../types/contentNavigationStrategy';\nimport type { CourseDBInterface } from '../interfaces/courseDB';\nimport type { UserDBInterface } from '../interfaces/userDB';\nimport type { StudySessionNewItem, StudySessionReviewItem } from '../interfaces/contentSource';\nimport type { ScheduledCard } from '../types/user';\nimport type { CardGenerator, GeneratorContext } from './generators/types';\nimport { logger } from '../../util/logger';\n\n// ============================================================================\n// COMPOSITE GENERATOR\n// ============================================================================\n//\n// Composes multiple generator strategies into a single generator.\n//\n// Use case: When a course has multiple generators (e.g., ELO + SRS), this\n// class merges their outputs into a unified candidate list.\n//\n// Aggregation strategy:\n// - Cards appearing in multiple generators get a frequency boost\n// - Score = average(scores) * (1 + 0.1 * (appearances - 1))\n// - This rewards cards that multiple generators agree on\n//\n// ============================================================================\n\n/**\n * Aggregation modes for combining scores from multiple generators.\n */\nexport enum AggregationMode {\n /** Use the maximum score from any generator */\n MAX = 'max',\n /** Average all scores */\n AVERAGE = 'average',\n /** Average with frequency boost: avg * (1 + 0.1 * (n-1)) */\n FREQUENCY_BOOST = 'frequencyBoost',\n}\n\nconst DEFAULT_AGGREGATION_MODE = AggregationMode.FREQUENCY_BOOST;\nconst FREQUENCY_BOOST_FACTOR = 0.1;\n\n/**\n * Composes multiple generators into a single generator.\n *\n * Implements CardGenerator for use in Pipeline architecture.\n * Also extends ContentNavigator for backward compatibility.\n *\n * Fetches candidates from all generators, deduplicates by cardId,\n * and aggregates scores based on the configured mode.\n */\nexport default class CompositeGenerator extends ContentNavigator implements CardGenerator {\n /** Human-readable name for CardGenerator interface */\n name: string = 'Composite Generator';\n\n private generators: CardGenerator[];\n private aggregationMode: AggregationMode;\n\n constructor(\n generators: CardGenerator[],\n aggregationMode: AggregationMode = DEFAULT_AGGREGATION_MODE\n ) {\n super();\n this.generators = generators;\n this.aggregationMode = aggregationMode;\n\n if (generators.length === 0) {\n throw new Error('CompositeGenerator requires at least one generator');\n }\n\n logger.debug(\n `[CompositeGenerator] Created with ${generators.length} generators, mode: ${aggregationMode}`\n );\n }\n\n /**\n * Creates a CompositeGenerator from strategy data.\n *\n * This is a convenience factory for use by PipelineAssembler.\n */\n static async fromStrategies(\n user: UserDBInterface,\n course: CourseDBInterface,\n strategies: ContentNavigationStrategyData[],\n aggregationMode: AggregationMode = DEFAULT_AGGREGATION_MODE\n ): Promise<CompositeGenerator> {\n const generators = await Promise.all(\n strategies.map((s) => ContentNavigator.create(user, course, s))\n );\n // Cast is safe because we know these are generators\n return new CompositeGenerator(generators as unknown as CardGenerator[], aggregationMode);\n }\n\n /**\n * Get weighted cards from all generators, merge and deduplicate.\n *\n * Cards appearing in multiple generators receive a score boost.\n * Provenance tracks which generators produced each card and how scores were aggregated.\n *\n * This method supports both the legacy signature (limit only) and the\n * CardGenerator interface signature (limit, context).\n *\n * @param limit - Maximum number of cards to return\n * @param context - Optional GeneratorContext passed to child generators\n */\n async getWeightedCards(limit: number, context?: GeneratorContext): Promise<WeightedCard[]> {\n // Fetch from all generators in parallel\n const results = await Promise.all(\n this.generators.map((g) => g.getWeightedCards(limit, context))\n );\n\n // Group by cardId\n const byCardId = new Map<string, WeightedCard[]>();\n for (const cards of results) {\n for (const card of cards) {\n const existing = byCardId.get(card.cardId) || [];\n existing.push(card);\n byCardId.set(card.cardId, existing);\n }\n }\n\n // Aggregate scores\n const merged: WeightedCard[] = [];\n for (const [, cards] of byCardId) {\n const aggregatedScore = this.aggregateScores(cards);\n const finalScore = Math.min(1.0, aggregatedScore); // Clamp to [0, 1]\n\n // Merge provenance from all generators that produced this card\n const mergedProvenance = cards.flatMap((c) => c.provenance);\n\n // Determine action based on whether score changed\n const initialScore = cards[0].score;\n const action =\n finalScore > initialScore ? 'boosted' : finalScore < initialScore ? 'penalized' : 'passed';\n\n // Build reason explaining the aggregation\n const reason = this.buildAggregationReason(cards, finalScore);\n\n // Append composite provenance entry\n merged.push({\n ...cards[0],\n score: finalScore,\n provenance: [\n ...mergedProvenance,\n {\n strategy: 'composite',\n strategyName: 'Composite Generator',\n strategyId: 'COMPOSITE_GENERATOR',\n action,\n score: finalScore,\n reason,\n },\n ],\n });\n }\n\n // Sort by score descending and limit\n return merged.sort((a, b) => b.score - a.score).slice(0, limit);\n }\n\n /**\n * Build human-readable reason for score aggregation.\n */\n private buildAggregationReason(cards: WeightedCard[], finalScore: number): string {\n const count = cards.length;\n const scores = cards.map((c) => c.score.toFixed(2)).join(', ');\n\n if (count === 1) {\n return `Single generator, score ${finalScore.toFixed(2)}`;\n }\n\n const strategies = cards.map((c) => c.provenance[0]?.strategy || 'unknown').join(', ');\n\n switch (this.aggregationMode) {\n case AggregationMode.MAX:\n return `Max of ${count} generators (${strategies}): scores [${scores}] → ${finalScore.toFixed(2)}`;\n\n case AggregationMode.AVERAGE:\n return `Average of ${count} generators (${strategies}): scores [${scores}] → ${finalScore.toFixed(2)}`;\n\n case AggregationMode.FREQUENCY_BOOST: {\n const avg = cards.reduce((sum, c) => sum + c.score, 0) / count;\n const boost = 1 + FREQUENCY_BOOST_FACTOR * (count - 1);\n return `Frequency boost from ${count} generators (${strategies}): avg ${avg.toFixed(2)} × ${boost.toFixed(2)} → ${finalScore.toFixed(2)}`;\n }\n\n default:\n return `Aggregated from ${count} generators: ${finalScore.toFixed(2)}`;\n }\n }\n\n /**\n * Aggregate scores from multiple generators for the same card.\n */\n private aggregateScores(cards: WeightedCard[]): number {\n const scores = cards.map((c) => c.score);\n\n switch (this.aggregationMode) {\n case AggregationMode.MAX:\n return Math.max(...scores);\n\n case AggregationMode.AVERAGE:\n return scores.reduce((sum, s) => sum + s, 0) / scores.length;\n\n case AggregationMode.FREQUENCY_BOOST: {\n const avg = scores.reduce((sum, s) => sum + s, 0) / scores.length;\n const frequencyBoost = 1 + FREQUENCY_BOOST_FACTOR * (cards.length - 1);\n return avg * frequencyBoost;\n }\n\n default:\n return scores[0];\n }\n }\n\n /**\n * Get new cards from all generators, merged and deduplicated.\n */\n async getNewCards(n?: number): Promise<StudySessionNewItem[]> {\n // For legacy method, need to filter to generators that have getNewCards\n const legacyGenerators = this.generators.filter(\n (g): g is CardGenerator & ContentNavigator => g instanceof ContentNavigator\n );\n\n const results = await Promise.all(legacyGenerators.map((g) => g.getNewCards(n)));\n\n // Deduplicate by cardID\n const seen = new Set<string>();\n const merged: StudySessionNewItem[] = [];\n\n for (const cards of results) {\n for (const card of cards) {\n if (!seen.has(card.cardID)) {\n seen.add(card.cardID);\n merged.push(card);\n }\n }\n }\n\n return n ? merged.slice(0, n) : merged;\n }\n\n /**\n * Get pending reviews from all generators, merged and deduplicated.\n */\n async getPendingReviews(): Promise<(StudySessionReviewItem & ScheduledCard)[]> {\n // For legacy method, need to filter to generators that have getPendingReviews\n const legacyGenerators = this.generators.filter(\n (g): g is CardGenerator & ContentNavigator => g instanceof ContentNavigator\n );\n\n const results = await Promise.all(legacyGenerators.map((g) => g.getPendingReviews()));\n\n // Deduplicate by cardID\n const seen = new Set<string>();\n const merged: (StudySessionReviewItem & ScheduledCard)[] = [];\n\n for (const reviews of results) {\n for (const review of reviews) {\n if (!seen.has(review.cardID)) {\n seen.add(review.cardID);\n merged.push(review);\n }\n }\n }\n\n return merged;\n }\n}\n","import { toCourseElo } from '@vue-skuilder/common';\nimport type { CourseDBInterface } from '../interfaces/courseDB';\nimport type { UserDBInterface } from '../interfaces/userDB';\nimport type { ScheduledCard } from '../types/user';\nimport { ContentNavigator } from './index';\nimport type { WeightedCard } from './index';\nimport type { CardFilter, FilterContext } from './filters/types';\nimport type { CardGenerator, GeneratorContext } from './generators/types';\nimport type { StudySessionNewItem, StudySessionReviewItem } from '../interfaces/contentSource';\nimport { logger } from '../../util/logger';\n\n// ============================================================================\n// PIPELINE LOGGING HELPERS\n// ============================================================================\n//\n// Focused logging functions that can be toggled by commenting single lines.\n// Use these to inspect pipeline behavior in development/production.\n//\n\n/**\n * Log pipeline configuration on construction.\n * Shows generator and filter chain structure.\n */\nfunction logPipelineConfig(generator: CardGenerator, filters: CardFilter[]): void {\n const filterList = filters.length > 0\n ? '\\n - ' + filters.map(f => f.name).join('\\n - ')\n : ' none';\n\n logger.info(\n `[Pipeline] Configuration:\\n` +\n ` Generator: ${generator.name}\\n` +\n ` Filters:${filterList}`\n );\n}\n\n/**\n * Log tag hydration results.\n * Shows effectiveness of batch query (how many cards/tags were hydrated).\n */\nfunction logTagHydration(cards: WeightedCard[], tagsByCard: Map<string, string[]>): void {\n const totalTags = Array.from(tagsByCard.values()).reduce((sum, tags) => sum + tags.length, 0);\n const cardsWithTags = Array.from(tagsByCard.values()).filter(tags => tags.length > 0).length;\n\n logger.debug(\n `[Pipeline] Tag hydration: ${cards.length} cards, ` +\n `${cardsWithTags} have tags (${totalTags} total tags) - single batch query`\n );\n}\n\n/**\n * Log pipeline execution summary.\n * Shows complete flow from generator through filters to final results.\n */\nfunction logExecutionSummary(\n generatorName: string,\n generatedCount: number,\n filterCount: number,\n finalCount: number,\n topScores: number[]\n): void {\n const scoreDisplay = topScores.length > 0\n ? topScores.map(s => s.toFixed(2)).join(', ')\n : 'none';\n\n logger.info(\n `[Pipeline] Execution: ${generatorName} produced ${generatedCount} → ` +\n `${filterCount} filters → ${finalCount} results (top scores: ${scoreDisplay})`\n );\n}\n\n/**\n * Log provenance trails for cards.\n * Shows the complete scoring history for each card through the pipeline.\n * Useful for debugging why cards scored the way they did.\n */\nfunction logCardProvenance(cards: WeightedCard[], maxCards: number = 3): void {\n const cardsToLog = cards.slice(0, maxCards);\n\n logger.debug(`[Pipeline] Provenance for top ${cardsToLog.length} cards:`);\n\n for (const card of cardsToLog) {\n logger.debug(`[Pipeline] ${card.cardId} (final score: ${card.score.toFixed(3)}):`);\n\n for (const entry of card.provenance) {\n const scoreChange = entry.score.toFixed(3);\n const action = entry.action.padEnd(9); // Align columns\n logger.debug(\n `[Pipeline] ${action} ${scoreChange} - ${entry.strategyName}: ${entry.reason}`\n );\n }\n }\n}\n\n// ============================================================================\n// PIPELINE\n// ============================================================================\n//\n// Executes a navigation pipeline: generator → filters → sorted results.\n//\n// Architecture:\n// cards = generator.getWeightedCards(limit, context)\n// cards = filter1.transform(cards, context)\n// cards = filter2.transform(cards, context)\n// cards = filter3.transform(cards, context)\n// return sorted(cards).slice(0, limit)\n//\n// Benefits:\n// - Clear separation: generators produce, filters transform\n// - No nested instantiation complexity\n// - Filters don't need to know about each other\n// - Shared context built once, passed to all stages\n//\n// ============================================================================\n\n/**\n * A navigation pipeline that runs a generator and applies filters sequentially.\n *\n * Implements StudyContentSource for backward compatibility with SessionController.\n *\n * ## Usage\n *\n * ```typescript\n * const pipeline = new Pipeline(\n * compositeGenerator, // or single generator\n * [eloDistanceFilter, interferenceFilter],\n * user,\n * course\n * );\n *\n * const cards = await pipeline.getWeightedCards(20);\n * ```\n */\nexport class Pipeline extends ContentNavigator {\n private generator: CardGenerator;\n private filters: CardFilter[];\n\n /**\n * Create a new pipeline.\n *\n * @param generator - The generator (or CompositeGenerator) that produces candidates\n * @param filters - Filters to apply sequentially (order doesn't matter for multipliers)\n * @param user - User database interface\n * @param course - Course database interface\n */\n constructor(\n generator: CardGenerator,\n filters: CardFilter[],\n user: UserDBInterface,\n course: CourseDBInterface\n ) {\n super();\n this.generator = generator;\n this.filters = filters;\n this.user = user;\n this.course = course;\n\n // Toggle pipeline configuration logging:\n logPipelineConfig(generator, filters);\n }\n\n /**\n * Get weighted cards by running generator and applying filters.\n *\n * 1. Build shared context (user ELO, etc.)\n * 2. Get candidates from generator (passing context)\n * 3. Batch hydrate tags for all candidates\n * 4. Apply each filter sequentially\n * 5. Remove zero-score cards\n * 6. Sort by score descending\n * 7. Return top N\n *\n * @param limit - Maximum number of cards to return\n * @returns Cards sorted by score descending\n */\n async getWeightedCards(limit: number): Promise<WeightedCard[]> {\n // Build shared context once\n const context = await this.buildContext();\n\n // Over-fetch from generator to account for filtering\n const overFetchMultiplier = 2 + this.filters.length * 0.5;\n const fetchLimit = Math.ceil(limit * overFetchMultiplier);\n\n logger.debug(\n `[Pipeline] Fetching ${fetchLimit} candidates from generator '${this.generator.name}'`\n );\n\n // Get candidates from generator, passing context\n let cards = await this.generator.getWeightedCards(fetchLimit, context);\n const generatedCount = cards.length;\n\n logger.debug(`[Pipeline] Generator returned ${generatedCount} candidates`);\n\n // Batch hydrate tags before filters run\n cards = await this.hydrateTags(cards);\n\n // Apply filters sequentially\n for (const filter of this.filters) {\n const beforeCount = cards.length;\n cards = await filter.transform(cards, context);\n logger.debug(`[Pipeline] Filter '${filter.name}': ${beforeCount} → ${cards.length} cards`);\n }\n\n // Remove zero-score cards (hard filtered)\n cards = cards.filter((c) => c.score > 0);\n\n // Sort by score descending\n cards.sort((a, b) => b.score - a.score);\n\n // Return top N\n const result = cards.slice(0, limit);\n\n // Toggle execution summary logging:\n const topScores = result.slice(0, 3).map(c => c.score);\n logExecutionSummary(this.generator.name, generatedCount, this.filters.length, result.length, topScores);\n\n // Toggle provenance logging (shows scoring history for top cards):\n logCardProvenance(result, 3);\n\n return result;\n }\n\n /**\n * Batch hydrate tags for all cards.\n *\n * Fetches tags for all cards in a single database query and attaches them\n * to the WeightedCard objects. Filters can then use card.tags instead of\n * making individual getAppliedTags() calls.\n *\n * @param cards - Cards to hydrate\n * @returns Cards with tags populated\n */\n private async hydrateTags(cards: WeightedCard[]): Promise<WeightedCard[]> {\n if (cards.length === 0) {\n return cards;\n }\n\n const cardIds = cards.map((c) => c.cardId);\n const tagsByCard = await this.course!.getAppliedTagsBatch(cardIds);\n\n // Toggle tag hydration logging:\n logTagHydration(cards, tagsByCard);\n\n return cards.map((card) => ({\n ...card,\n tags: tagsByCard.get(card.cardId) ?? [],\n }));\n }\n\n /**\n * Build shared context for generator and filters.\n *\n * Called once per getWeightedCards() invocation.\n * Contains data that the generator and multiple filters might need.\n *\n * The context satisfies both GeneratorContext and FilterContext interfaces.\n */\n private async buildContext(): Promise<GeneratorContext & FilterContext> {\n let userElo = 1000; // Default ELO\n\n try {\n const courseReg = await this.user!.getCourseRegDoc(this.course!.getCourseID());\n const courseElo = toCourseElo(courseReg.elo);\n userElo = courseElo.global.score;\n } catch (e) {\n logger.debug(`[Pipeline] Could not get user ELO, using default: ${e}`);\n }\n\n return {\n user: this.user!,\n course: this.course!,\n userElo,\n };\n }\n\n // ===========================================================================\n // Legacy StudyContentSource methods\n // ===========================================================================\n //\n // These delegate to the generator for backward compatibility.\n // Eventually SessionController will use getWeightedCards() exclusively.\n //\n\n /**\n * Get new cards via legacy API.\n * Delegates to the generator if it supports the legacy interface.\n */\n async getNewCards(n?: number): Promise<StudySessionNewItem[]> {\n // Check if generator has legacy method (ContentNavigator-based generators do)\n if ('getNewCards' in this.generator && typeof this.generator.getNewCards === 'function') {\n return (this.generator as ContentNavigator).getNewCards(n);\n }\n // Pure CardGenerator without legacy support - return empty\n return [];\n }\n\n /**\n * Get pending reviews via legacy API.\n * Delegates to the generator if it supports the legacy interface.\n */\n async getPendingReviews(): Promise<(StudySessionReviewItem & ScheduledCard)[]> {\n // Check if generator has legacy method (ContentNavigator-based generators do)\n if (\n 'getPendingReviews' in this.generator &&\n typeof this.generator.getPendingReviews === 'function'\n ) {\n return (this.generator as ContentNavigator).getPendingReviews();\n }\n // Pure CardGenerator without legacy support - return empty\n return [];\n }\n\n /**\n * Get the course ID for this pipeline.\n */\n getCourseID(): string {\n return this.course!.getCourseID();\n }\n}\n","import type { ContentNavigationStrategyData } from '../types/contentNavigationStrategy';\nimport { ContentNavigator, isGenerator, isFilter, Navigators } from './index';\nimport type { CardFilter } from './filters/types';\nimport type { CardGenerator } from './generators/types';\nimport { Pipeline } from './Pipeline';\nimport { DocType } from '../types/types-legacy';\nimport { logger } from '../../util/logger';\nimport type { CourseDBInterface } from '../interfaces/courseDB';\nimport type { UserDBInterface } from '../interfaces/userDB';\nimport CompositeGenerator from './CompositeGenerator';\n\n// ============================================================================\n// PIPELINE ASSEMBLER\n// ============================================================================\n//\n// Assembles navigation strategies into a Pipeline instance.\n//\n// This class is DB-agnostic: it receives strategy documents and returns an\n// assembled, ready-to-use Pipeline. This separation enables:\n// 1. Use with different DB implementations (Couch, Static, etc.)\n// 2. Future dynamic/evolutionary strategy selection\n// 3. Easy unit testing without DB mocking\n//\n// Pipeline assembly:\n// 1. Separate strategies into generators and filters by role\n// 2. Instantiate generator(s) - wrap multiple in CompositeGenerator\n// 3. Instantiate filters\n// 4. Return Pipeline(generator, filters)\n//\n// ============================================================================\n\n/**\n * Input for pipeline assembly.\n */\nexport interface PipelineAssemblerInput {\n /** All strategy documents to assemble into a pipeline */\n strategies: ContentNavigationStrategyData[];\n /** User database interface (required for instantiation) */\n user: UserDBInterface;\n /** Course database interface (required for instantiation) */\n course: CourseDBInterface;\n}\n\n/**\n * Result of pipeline assembly.\n */\nexport interface PipelineAssemblyResult {\n /** The assembled pipeline, or null if assembly failed */\n pipeline: Pipeline | null;\n /** Generator strategies found (for informational purposes) */\n generatorStrategies: ContentNavigationStrategyData[];\n /** Filter strategies found (for informational purposes) */\n filterStrategies: ContentNavigationStrategyData[];\n /** Warnings encountered during assembly (logged but non-fatal) */\n warnings: string[];\n}\n\n/**\n * Assembles navigation strategies into a Pipeline.\n *\n * Instantiates generators and filters from strategy documents and\n * composes them into a ready-to-use Pipeline instance.\n */\nexport class PipelineAssembler {\n /**\n * Assembles a navigation pipeline from strategy documents.\n *\n * 1. Separates into generators and filters by role\n * 2. Validates at least one generator exists (or creates default ELO)\n * 3. Instantiates generators - wraps multiple in CompositeGenerator\n * 4. Instantiates filters\n * 5. Returns Pipeline(generator, filters)\n *\n * @param input - Strategy documents plus user/course interfaces\n * @returns Assembled pipeline and any warnings\n */\n async assemble(input: PipelineAssemblerInput): Promise<PipelineAssemblyResult> {\n const { strategies, user, course } = input;\n const warnings: string[] = [];\n\n if (strategies.length === 0) {\n return {\n pipeline: null,\n generatorStrategies: [],\n filterStrategies: [],\n warnings,\n };\n }\n\n // Separate generators from filters\n const generatorStrategies: ContentNavigationStrategyData[] = [];\n const filterStrategies: ContentNavigationStrategyData[] = [];\n\n for (const s of strategies) {\n if (isGenerator(s.implementingClass)) {\n generatorStrategies.push(s);\n } else if (isFilter(s.implementingClass)) {\n filterStrategies.push(s);\n } else {\n // Unknown strategy type — skip with warning\n warnings.push(`Unknown strategy type '${s.implementingClass}', skipping: ${s.name}`);\n }\n }\n\n // If no generator but filters exist, use default ELO generator\n if (generatorStrategies.length === 0) {\n if (filterStrategies.length > 0) {\n logger.debug(\n '[PipelineAssembler] No generator found, using default ELO with configured filters'\n );\n generatorStrategies.push(this.makeDefaultEloStrategy(course.getCourseID()));\n } else {\n warnings.push('No generator strategy found');\n return {\n pipeline: null,\n generatorStrategies: [],\n filterStrategies: [],\n warnings,\n };\n }\n }\n\n // Instantiate generators\n let generator: CardGenerator;\n\n if (generatorStrategies.length === 1) {\n // Single generator\n const nav = await ContentNavigator.create(user, course, generatorStrategies[0]);\n generator = nav as unknown as CardGenerator;\n logger.debug(`[PipelineAssembler] Using single generator: ${generatorStrategies[0].name}`);\n } else {\n // Multiple generators - wrap in CompositeGenerator\n logger.debug(\n `[PipelineAssembler] Using CompositeGenerator for ${generatorStrategies.length} generators: ${generatorStrategies.map((g) => g.name).join(', ')}`\n );\n generator = await CompositeGenerator.fromStrategies(user, course, generatorStrategies);\n }\n\n // Instantiate filters\n const filters: CardFilter[] = [];\n\n // Sort filters alphabetically for deterministic ordering\n const sortedFilterStrategies = [...filterStrategies].sort((a, b) =>\n a.name.localeCompare(b.name)\n );\n\n for (const filterStrategy of sortedFilterStrategies) {\n try {\n const nav = await ContentNavigator.create(user, course, filterStrategy);\n // The navigator implements CardFilter\n if ('transform' in nav && typeof nav.transform === 'function') {\n filters.push(nav as unknown as CardFilter);\n logger.debug(`[PipelineAssembler] Added filter: ${filterStrategy.name}`);\n } else {\n warnings.push(\n `Filter '${filterStrategy.name}' does not implement CardFilter.transform(), skipping`\n );\n }\n } catch (e) {\n warnings.push(`Failed to instantiate filter '${filterStrategy.name}': ${e}`);\n }\n }\n\n // Build pipeline\n const pipeline = new Pipeline(generator, filters, user, course);\n\n logger.debug(\n `[PipelineAssembler] Assembled pipeline with ${generatorStrategies.length} generator(s) and ${filters.length} filter(s)`\n );\n\n return {\n pipeline,\n generatorStrategies,\n filterStrategies: sortedFilterStrategies,\n warnings,\n };\n }\n\n /**\n * Creates a default ELO generator strategy.\n * Used when filters are configured but no generator is specified.\n */\n private makeDefaultEloStrategy(courseId: string): ContentNavigationStrategyData {\n return {\n _id: 'NAVIGATION_STRATEGY-ELO-default',\n course: courseId,\n docType: DocType.NAVIGATION_STRATEGY,\n name: 'ELO (default)',\n description: 'Default ELO-based generator',\n implementingClass: Navigators.ELO,\n serializedData: '',\n };\n }\n}\n","import type { ScheduledCard } from '../types/user';\nimport type { CourseDBInterface } from '../interfaces/courseDB';\nimport type { UserDBInterface } from '../interfaces/userDB';\nimport { ContentNavigator } from './index';\nimport type { WeightedCard } from './index';\nimport type { CourseElo } from '@vue-skuilder/common';\nimport { toCourseElo } from '@vue-skuilder/common';\nimport type { StudySessionReviewItem, StudySessionNewItem, QualifiedCardID } from '..';\nimport type { CardGenerator, GeneratorContext } from './generators/types';\n\n// ============================================================================\n// ELO NAVIGATOR\n// ============================================================================\n//\n// A generator strategy that selects new cards based on ELO proximity.\n//\n// Cards closer to the user's skill level (ELO) receive higher scores.\n// This ensures learners see content matched to their current ability.\n//\n// NOTE: This generator only handles NEW cards. Reviews are handled by\n// SRSNavigator. Use CompositeGenerator to combine both.\n//\n// ============================================================================\n\n/**\n * A navigation strategy that scores new cards by ELO proximity.\n *\n * Implements CardGenerator for use in Pipeline architecture.\n * Also extends ContentNavigator for backward compatibility with legacy code.\n *\n * Higher scores indicate better ELO match:\n * - Cards at user's ELO level score highest\n * - Score decreases with ELO distance\n *\n * Only returns new cards - use SRSNavigator for reviews.\n */\nexport default class ELONavigator extends ContentNavigator implements CardGenerator {\n /** Human-readable name for CardGenerator interface */\n name: string;\n\n constructor(\n user: UserDBInterface,\n course: CourseDBInterface,\n strategyData?: { name: string; _id: string }\n // The ELO strategy is non-parameterized.\n //\n // It instead relies on existing meta data from the course and user with respect to\n // ELO scores - it uses those to select cards matched to user skill level.\n ) {\n super(user, course, strategyData as any);\n this.name = strategyData?.name || 'ELO';\n }\n\n async getPendingReviews(): Promise<(StudySessionReviewItem & ScheduledCard)[]> {\n type ratedReview = ScheduledCard & CourseElo;\n\n const reviews = await this.user.getPendingReviews(this.course.getCourseID()); // todo: this adds a db round trip - should be server side\n const elo = await this.course.getCardEloData(reviews.map((r) => r.cardId));\n\n const ratedReviews = reviews.map((r, i) => {\n const ratedR: ratedReview = {\n ...r,\n ...elo[i],\n };\n return ratedR;\n });\n\n ratedReviews.sort((a, b) => {\n return a.global.score - b.global.score;\n });\n\n return ratedReviews.map((r) => {\n return {\n ...r,\n contentSourceType: 'course',\n contentSourceID: this.course.getCourseID(),\n cardID: r.cardId,\n courseID: r.courseId,\n qualifiedID: `${r.courseId}-${r.cardId}`,\n reviewID: r._id,\n status: 'review',\n };\n });\n }\n\n async getNewCards(limit: number = 99): Promise<StudySessionNewItem[]> {\n const activeCards = await this.user.getActiveCards();\n return (\n await this.course.getCardsCenteredAtELO(\n { limit: limit, elo: 'user' },\n (c: QualifiedCardID) => {\n if (activeCards.some((ac) => c.cardID === ac.cardID)) {\n return false;\n } else {\n return true;\n }\n }\n )\n ).map((c) => {\n return {\n ...c,\n status: 'new',\n };\n });\n }\n\n /**\n * Get new cards with suitability scores based on ELO distance.\n *\n * Cards closer to user's ELO get higher scores.\n * Score formula: max(0, 1 - distance / 500)\n *\n * NOTE: This generator only handles NEW cards. Reviews are handled by\n * SRSNavigator. Use CompositeGenerator to combine both.\n *\n * This method supports both the legacy signature (limit only) and the\n * CardGenerator interface signature (limit, context).\n *\n * @param limit - Maximum number of cards to return\n * @param context - Optional GeneratorContext (used when called via Pipeline)\n */\n async getWeightedCards(limit: number, context?: GeneratorContext): Promise<WeightedCard[]> {\n // Determine user ELO - from context if available, otherwise fetch\n let userGlobalElo: number;\n if (context?.userElo !== undefined) {\n userGlobalElo = context.userElo;\n } else {\n const courseReg = await this.user.getCourseRegDoc(this.course.getCourseID());\n const userElo = toCourseElo(courseReg.elo);\n userGlobalElo = userElo.global.score;\n }\n\n // Get new cards (existing logic)\n const newCards = await this.getNewCards(limit);\n\n // Get ELO data for all cards in one batch\n const cardIds = newCards.map((c) => c.cardID);\n const cardEloData = await this.course.getCardEloData(cardIds);\n\n // Score new cards by ELO distance\n const scored: WeightedCard[] = newCards.map((c, i) => {\n const cardElo = cardEloData[i]?.global?.score ?? 1000;\n const distance = Math.abs(cardElo - userGlobalElo);\n const score = Math.max(0, 1 - distance / 500);\n\n return {\n cardId: c.cardID,\n courseId: c.courseID,\n score,\n provenance: [\n {\n strategy: 'elo',\n strategyName: this.strategyName || this.name,\n strategyId: this.strategyId || 'NAVIGATION_STRATEGY-ELO-default',\n action: 'generated',\n score,\n reason: `ELO distance ${Math.round(distance)} (card: ${Math.round(cardElo)}, user: ${Math.round(userGlobalElo)}), new card`,\n },\n ],\n };\n });\n\n // Sort by score descending\n scored.sort((a, b) => b.score - a.score);\n\n return scored.slice(0, limit);\n }\n}\n","import type { WeightedCard } from '../index';\nimport type { CardFilter, FilterContext } from './types';\n\n// ============================================================================\n// ELO DISTANCE FILTER\n// ============================================================================\n//\n// Penalizes cards that are far from the user's current ELO using a smooth curve.\n//\n// This filter addresses cross-strategy coordination:\n// - SRS generates reviews based on scheduling\n// - But some scheduled cards may be \"below\" the user's current level\n// - Or \"above\" (shouldn't happen often, but possible)\n//\n// By applying ELO distance penalties, we can:\n// - Deprioritize reviews the user has \"moved beyond\"\n// - Deprioritize cards that are too hard for current skill level\n//\n// The penalty curve is smooth (no discontinuities) using a Gaussian-like decay.\n//\n// ============================================================================\n\n/**\n * Configuration for the ELO distance filter.\n */\nexport interface EloDistanceConfig {\n /**\n * The ELO distance at which the multiplier is ~0.6 (one standard deviation).\n * Default: 200 ELO points.\n *\n * - At distance 0: multiplier ≈ 1.0\n * - At distance = halfLife: multiplier ≈ 0.6\n * - At distance = 2 * halfLife: multiplier ≈ 0.37\n * - At distance = 3 * halfLife: multiplier ≈ 0.22\n */\n halfLife?: number;\n\n /**\n * Minimum multiplier (floor) to prevent scores from going too low.\n * Default: 0.3\n */\n minMultiplier?: number;\n\n /**\n * Maximum multiplier (ceiling). Usually 1.0 (no boost for close cards).\n * Default: 1.0\n */\n maxMultiplier?: number;\n}\n\nconst DEFAULT_HALF_LIFE = 200;\nconst DEFAULT_MIN_MULTIPLIER = 0.3;\nconst DEFAULT_MAX_MULTIPLIER = 1.0;\n\n/**\n * Compute the multiplier for a given ELO distance using Gaussian decay.\n *\n * Formula: minMultiplier + (maxMultiplier - minMultiplier) * exp(-(distance/halfLife)^2)\n *\n * This produces a smooth bell curve centered at distance=0:\n * - At distance 0: multiplier = maxMultiplier (1.0)\n * - As distance increases: multiplier smoothly decays toward minMultiplier\n * - No discontinuities or sudden jumps\n */\nfunction computeMultiplier(\n distance: number,\n halfLife: number,\n minMultiplier: number,\n maxMultiplier: number\n): number {\n // Gaussian decay: exp(-(d/h)^2)\n const normalizedDistance = distance / halfLife;\n const decay = Math.exp(-(normalizedDistance * normalizedDistance));\n\n // Scale between min and max\n return minMultiplier + (maxMultiplier - minMultiplier) * decay;\n}\n\n/**\n * Create an ELO distance filter.\n *\n * Penalizes cards that are far from the user's current ELO level\n * using a smooth Gaussian decay curve. No discontinuities.\n *\n * @param config - Optional configuration for the decay curve\n * @returns A CardFilter that applies ELO distance penalties\n */\nexport function createEloDistanceFilter(config?: EloDistanceConfig): CardFilter {\n const halfLife = config?.halfLife ?? DEFAULT_HALF_LIFE;\n const minMultiplier = config?.minMultiplier ?? DEFAULT_MIN_MULTIPLIER;\n const maxMultiplier = config?.maxMultiplier ?? DEFAULT_MAX_MULTIPLIER;\n\n return {\n name: 'ELO Distance Filter',\n\n async transform(cards: WeightedCard[], context: FilterContext): Promise<WeightedCard[]> {\n const { course, userElo } = context;\n\n // Batch fetch ELO data for all cards\n const cardIds = cards.map((c) => c.cardId);\n const cardElos = await course.getCardEloData(cardIds);\n\n return cards.map((card, i) => {\n const cardElo = cardElos[i]?.global?.score ?? 1000;\n const distance = Math.abs(cardElo - userElo);\n const multiplier = computeMultiplier(distance, halfLife, minMultiplier, maxMultiplier);\n const newScore = card.score * multiplier;\n\n const action = multiplier < maxMultiplier - 0.01 ? 'penalized' : 'passed';\n\n return {\n ...card,\n score: newScore,\n provenance: [\n ...card.provenance,\n {\n strategy: 'eloDistance',\n strategyName: 'ELO Distance Filter',\n strategyId: 'ELO_DISTANCE_FILTER',\n action,\n score: newScore,\n reason: `ELO distance ${Math.round(distance)} (card: ${Math.round(cardElo)}, user: ${Math.round(userElo)}) → ${multiplier.toFixed(2)}x`,\n },\n ],\n };\n });\n },\n };\n}\n\n// Export defaults for testing\nexport { DEFAULT_HALF_LIFE, DEFAULT_MIN_MULTIPLIER, DEFAULT_MAX_MULTIPLIER };\n","import type { ScheduledCard } from '../../types/user';\nimport type { CourseDBInterface } from '../../interfaces/courseDB';\nimport type { UserDBInterface } from '../../interfaces/userDB';\nimport { ContentNavigator } from '../index';\nimport type { WeightedCard } from '../index';\nimport type { ContentNavigationStrategyData } from '../../types/contentNavigationStrategy';\nimport type { StudySessionReviewItem, StudySessionNewItem } from '../..';\nimport type { CardFilter, FilterContext } from './types';\n\n// ============================================================================\n// USER TAG PREFERENCE FILTER\n// ============================================================================\n//\n// Allows users to personalize their learning experience by specifying:\n// - Tags to boost/penalize (score multiplied by boost factor)\n//\n// User preferences are stored in STRATEGY_STATE documents in the user's\n// database, enabling persistence across sessions and sync across devices.\n//\n// Use cases:\n// - Goal-based learning: \"I want to learn piano by ear, skip sight-reading\"\n// - Selective focus: \"I only want to practice chess endgames\"\n// - Accessibility: \"Skip text-heavy cards, prefer visual content\"\n// - Difficulty customization: \"Skip beginner content I already know\"\n//\n// ============================================================================\n\n/**\n * User's tag preference state, stored in STRATEGY_STATE document.\n *\n * This interface defines what gets persisted to the user's database.\n * UI components write to this structure, and the filter reads from it.\n *\n * ## Preferences vs Goals\n *\n * Preferences are **path constraints** — they affect HOW the user learns,\n * not WHAT they're trying to learn. Examples:\n * - \"Skip text-heavy cards\" (accessibility)\n * - \"Prefer visual content\"\n *\n * For **goal-based** filtering (defining WHAT to learn), see the separate\n * UserGoalNavigator (stub). Goals affect progress tracking and completion\n * criteria; preferences only affect card selection.\n *\n * ## Slider Semantics\n *\n * Each tag maps to a multiplier value in the `boost` record:\n * - `0` = banish/exclude (card score = 0)\n * - `0.5` = penalize by 50%\n * - `1.0` = neutral/no effect (default when tag added)\n * - `2.0` = 2x preference boost\n * - Higher values = stronger preference\n *\n * If multiple tags on a card have preferences, the maximum multiplier wins.\n */\nexport interface UserTagPreferenceState {\n /**\n * Tag-specific multipliers.\n * Maps tag name to score multiplier (0 = exclude, 1 = neutral, >1 = boost).\n */\n boost: Record<string, number>;\n\n /**\n * ISO timestamp of last update.\n * Use `moment.utc(updatedAt)` to parse into a Moment object.\n */\n updatedAt: string;\n}\n\n/**\n * A filter that applies user-configured tag preferences.\n *\n * Reads preferences from STRATEGY_STATE document in user's database.\n * If no preferences exist, passes through unchanged (no-op).\n *\n * Implements CardFilter for use in Pipeline architecture.\n * Also extends ContentNavigator for compatibility with dynamic loading.\n */\nexport default class UserTagPreferenceFilter extends ContentNavigator implements CardFilter {\n private _strategyData: ContentNavigationStrategyData;\n\n /** Human-readable name for CardFilter interface */\n name: string;\n\n constructor(\n user: UserDBInterface,\n course: CourseDBInterface,\n strategyData: ContentNavigationStrategyData\n ) {\n super(user, course, strategyData);\n this._strategyData = strategyData;\n this.name = strategyData.name || 'User Tag Preferences';\n }\n\n /**\n * Compute multiplier for a card based on its tags and user preferences.\n * Returns the maximum multiplier among all matching tags, or 1.0 if no matches.\n */\n private computeMultiplier(cardTags: string[], boostMap: Record<string, number>): number {\n const multipliers = cardTags\n .map((tag) => boostMap[tag])\n .filter((val) => val !== undefined);\n\n if (multipliers.length === 0) {\n return 1.0;\n }\n\n // Use max multiplier among matching tags\n return Math.max(...multipliers);\n }\n\n /**\n * Build human-readable reason for the filter's decision.\n */\n private buildReason(\n cardTags: string[],\n boostMap: Record<string, number>,\n multiplier: number\n ): string {\n // Find which tag(s) contributed to the multiplier\n const matchingTags = cardTags.filter((tag) => boostMap[tag] === multiplier);\n\n if (multiplier === 0) {\n return `Excluded by user preference: ${matchingTags.join(', ')} (${multiplier}x)`;\n }\n\n if (multiplier < 1.0) {\n return `Penalized by user preference: ${matchingTags.join(', ')} (${multiplier.toFixed(2)}x)`;\n }\n\n if (multiplier > 1.0) {\n return `Boosted by user preference: ${matchingTags.join(', ')} (${multiplier.toFixed(2)}x)`;\n }\n\n return 'No matching user preferences';\n }\n\n /**\n * CardFilter.transform implementation.\n *\n * Apply user tag preferences:\n * 1. Read preferences from strategy state\n * 2. If no preferences, pass through unchanged\n * 3. For each card:\n * - Look up tag in boost record\n * - If tag found: apply multiplier (0 = exclude, 1 = neutral, >1 = boost)\n * - If multiple tags match: use max multiplier\n * - Append provenance with clear reason\n */\n async transform(cards: WeightedCard[], _context: FilterContext): Promise<WeightedCard[]> {\n // Read user preferences from strategy state\n const prefs = await this.getStrategyState<UserTagPreferenceState>();\n \n\n // No preferences configured → pass through unchanged\n if (!prefs || Object.keys(prefs.boost).length === 0) {\n return cards.map((card) => ({\n ...card,\n provenance: [\n ...card.provenance,\n {\n strategy: 'userTagPreference',\n strategyName: this.strategyName || this.name,\n strategyId: this.strategyId || this._strategyData._id,\n action: 'passed' as const,\n score: card.score,\n reason: 'No user tag preferences configured',\n },\n ],\n }));\n }\n\n // Process each card\n const adjusted: WeightedCard[] = await Promise.all(\n cards.map(async (card) => {\n const cardTags = card.tags ?? [];\n\n // Compute multiplier based on card tags and user preferences\n const multiplier = this.computeMultiplier(cardTags, prefs.boost);\n const finalScore = Math.min(1, card.score * multiplier);\n\n // Determine action for provenance\n let action: 'passed' | 'boosted' | 'penalized';\n if (multiplier === 0 || multiplier < 1.0) {\n action = 'penalized';\n } else if (multiplier > 1.0) {\n action = 'boosted';\n } else {\n action = 'passed';\n }\n\n return {\n ...card,\n score: finalScore,\n provenance: [\n ...card.provenance,\n {\n strategy: 'userTagPreference',\n strategyName: this.strategyName || this.name,\n strategyId: this.strategyId || this._strategyData._id,\n action,\n score: finalScore,\n reason: this.buildReason(cardTags, prefs.boost, multiplier),\n },\n ],\n };\n })\n );\n\n return adjusted;\n }\n\n /**\n * Legacy getWeightedCards - throws as filters should not be used as generators.\n */\n async getWeightedCards(_limit: number): Promise<WeightedCard[]> {\n throw new Error(\n 'UserTagPreferenceFilter is a filter and should not be used as a generator. ' +\n 'Use Pipeline with a generator and this filter via transform().'\n );\n }\n\n // Legacy methods - stub implementations since filters don't generate cards\n\n async getNewCards(_n?: number): Promise<StudySessionNewItem[]> {\n return [];\n }\n\n async getPendingReviews(): Promise<(StudySessionReviewItem & ScheduledCard)[]> {\n return [];\n }\n}\n","// Filter types and interfaces\nexport type { CardFilter, FilterContext, CardFilterFactory } from './types';\n\n// Filter implementations\nexport { createEloDistanceFilter } from './eloDistance';\nexport type { EloDistanceConfig } from './eloDistance';\n\nexport { default as UserTagPreferenceFilter } from './userTagPreference';\nexport type { UserTagPreferenceState } from './userTagPreference';\n","import type { WeightedCard } from '../index';\nimport type { CourseDBInterface } from '../../interfaces/courseDB';\nimport type { UserDBInterface } from '../../interfaces/userDB';\n\n// ============================================================================\n// CARD FILTER INTERFACE\n// ============================================================================\n//\n// Filters are pure transforms on a list of WeightedCards.\n// They replace the delegate-wrapping pattern with a simpler model:\n//\n// cards = Generator.getWeightedCards()\n// cards = Filter1.transform(cards, context)\n// cards = Filter2.transform(cards, context)\n//\n// Benefits:\n// - No nested instantiation\n// - Filters don't need to know about delegates\n// - Easy to add/remove/reorder filters\n// - Natural place to hydrate shared data before filter pass\n//\n// All filters should be score multipliers (including score: 0 for exclusion).\n// This means filter order doesn't affect final scores.\n//\n// ============================================================================\n\n/**\n * Shared context available to all filters in a pipeline.\n *\n * Built once per getWeightedCards() call and passed to each filter.\n * This avoids repeated lookups for common data like user ELO.\n */\nexport interface FilterContext {\n /** User database interface */\n user: UserDBInterface;\n\n /** Course database interface */\n course: CourseDBInterface;\n\n /** User's global ELO score for this course */\n userElo: number;\n\n // Future extensions:\n // - hydrated tags for all cards (batch lookup)\n // - user's tag-level ELO data\n // - course config\n}\n\n/**\n * A filter that transforms a list of weighted cards.\n *\n * Filters are pure transforms - they receive cards and context,\n * and return a modified list of cards. No delegate wrapping,\n * no side effects beyond provenance tracking.\n *\n * ## Implementation Guidelines\n *\n * 1. **Append provenance**: Every filter should add a StrategyContribution\n * entry documenting its decision for each card.\n *\n * 2. **Use multipliers**: Adjust scores by multiplying, not replacing.\n * This ensures filter order doesn't matter.\n *\n * 3. **Score 0 for exclusion**: To exclude a card, set score to 0.\n * Don't filter it out - let provenance show why it was excluded.\n *\n * 4. **Don't sort**: The Pipeline handles final sorting.\n * Filters just transform scores.\n *\n * ## Example Implementation\n *\n * ```typescript\n * const myFilter: CardFilter = {\n * name: 'My Filter',\n * async transform(cards, context) {\n * return cards.map(card => {\n * const multiplier = computeMultiplier(card, context);\n * const newScore = card.score * multiplier;\n * return {\n * ...card,\n * score: newScore,\n * provenance: [...card.provenance, {\n * strategy: 'myFilter',\n * strategyName: 'My Filter',\n * strategyId: 'MY_FILTER',\n * action: multiplier < 1 ? 'penalized' : 'passed',\n * score: newScore,\n * reason: 'Explanation of decision'\n * }]\n * };\n * });\n * }\n * };\n * ```\n */\nexport interface CardFilter {\n /** Human-readable name for this filter */\n name: string;\n\n /**\n * Transform a list of weighted cards.\n *\n * @param cards - Cards to transform (already scored by generator)\n * @param context - Shared context (user, course, userElo, etc.)\n * @returns Transformed cards with updated scores and provenance\n */\n transform(cards: WeightedCard[], context: FilterContext): Promise<WeightedCard[]>;\n}\n\n/**\n * Factory function type for creating filters from configuration.\n *\n * Used by PipelineAssembler to instantiate filters from strategy documents.\n */\nexport type CardFilterFactory<TConfig = unknown> = (config: TConfig) => CardFilter;\n","// Generator types and interfaces\nexport type { CardGenerator, GeneratorContext, CardGeneratorFactory } from './types';\n","import type { WeightedCard } from '../index';\nimport type { CourseDBInterface } from '../../interfaces/courseDB';\nimport type { UserDBInterface } from '../../interfaces/userDB';\n\n// ============================================================================\n// CARD GENERATOR INTERFACE\n// ============================================================================\n//\n// Generators produce candidate cards with initial scores.\n// They are the \"source\" stage of a navigation pipeline.\n//\n// Examples: ELO (skill proximity), SRS (review scheduling), HardcodedOrder\n//\n// Generators differ from filters:\n// - Generators: produce candidates from DB queries, assign initial scores\n// - Filters: transform existing candidates, adjust scores with multipliers\n//\n// The Pipeline class orchestrates: Generator → Filter₁ → Filter₂ → ... → Results\n//\n// ============================================================================\n\n/**\n * Context available to generators when producing candidates.\n *\n * Built once per getWeightedCards() call by the Pipeline.\n */\nexport interface GeneratorContext {\n /** User database interface */\n user: UserDBInterface;\n\n /** Course database interface */\n course: CourseDBInterface;\n\n /** User's global ELO score for this course */\n userElo: number;\n\n // Future extensions:\n // - user's tag-level ELO data\n // - course config\n // - session state (cards already seen this session)\n}\n\n/**\n * A generator that produces candidate cards with initial scores.\n *\n * Generators are the \"source\" stage of a navigation pipeline.\n * They query the database for eligible cards and assign initial\n * suitability scores based on their strategy (ELO proximity,\n * review urgency, fixed order, etc.).\n *\n * ## Implementation Guidelines\n *\n * 1. **Create provenance**: Each card should have a provenance entry\n * with action='generated' documenting why it was selected.\n *\n * 2. **Score semantics**: Higher scores = more suitable for presentation.\n * Scores should be in [0, 1] range for composability.\n *\n * 3. **Limit handling**: Respect the limit parameter, but may over-fetch\n * internally if needed for scoring accuracy.\n *\n * 4. **Sort before returning**: Return cards sorted by score descending.\n *\n * ## Example Implementation\n *\n * ```typescript\n * const myGenerator: CardGenerator = {\n * name: 'My Generator',\n * async getWeightedCards(limit, context) {\n * const candidates = await fetchCandidates(context.course, limit);\n * return candidates.map(c => ({\n * cardId: c.id,\n * courseId: context.course.getCourseID(),\n * score: computeScore(c, context),\n * provenance: [{\n * strategy: 'myGenerator',\n * strategyName: 'My Generator',\n * strategyId: 'MY_GENERATOR',\n * action: 'generated',\n * score: computeScore(c, context),\n * reason: 'Explanation of selection'\n * }]\n * }));\n * }\n * };\n * ```\n */\nexport interface CardGenerator {\n /** Human-readable name for this generator */\n name: string;\n\n /**\n * Produce candidate cards with initial scores.\n *\n * @param limit - Maximum number of cards to return\n * @param context - Shared context (user, course, userElo, etc.)\n * @returns Cards sorted by score descending, with provenance\n */\n getWeightedCards(limit: number, context: GeneratorContext): Promise<WeightedCard[]>;\n}\n\n/**\n * Factory function type for creating generators from configuration.\n *\n * Used by PipelineAssembler to instantiate generators from strategy documents.\n */\nexport type CardGeneratorFactory<TConfig = unknown> = (config: TConfig) => CardGenerator;\n","import type {\n CourseDBInterface,\n QualifiedCardID,\n StudySessionNewItem,\n StudySessionReviewItem,\n UserDBInterface,\n} from '..';\nimport type { ContentNavigationStrategyData } from '../types/contentNavigationStrategy';\nimport type { ScheduledCard } from '../types/user';\nimport { ContentNavigator } from './index';\nimport type { WeightedCard } from './index';\nimport type { CardGenerator, GeneratorContext } from './generators/types';\nimport { logger } from '../../util/logger';\n\n// ============================================================================\n// HARDCODED ORDER NAVIGATOR\n// ============================================================================\n//\n// A generator strategy that presents cards in a fixed, author-defined order.\n//\n// Use case: When course authors want explicit control over content sequencing,\n// e.g., teaching letters in a specific pedagogical order.\n//\n// The order is defined in serializedData as a JSON array of card IDs.\n// Earlier positions in the array get higher scores.\n//\n// ============================================================================\n\n/**\n * A navigation strategy that presents cards in a fixed order.\n *\n * Implements CardGenerator for use in Pipeline architecture.\n * Also extends ContentNavigator for backward compatibility with legacy code.\n *\n * Scoring:\n * - Earlier cards in the sequence get higher scores\n * - Reviews get score 1.0 (highest priority)\n * - New cards scored by position: 1.0 - (position / total) * 0.5\n */\nexport default class HardcodedOrderNavigator extends ContentNavigator implements CardGenerator {\n /** Human-readable name for CardGenerator interface */\n name: string;\n\n private orderedCardIds: string[] = [];\n\n constructor(\n user: UserDBInterface,\n course: CourseDBInterface,\n strategyData: ContentNavigationStrategyData\n ) {\n super(user, course, strategyData);\n this.name = strategyData.name || 'Hardcoded Order';\n\n if (strategyData.serializedData) {\n try {\n this.orderedCardIds = JSON.parse(strategyData.serializedData);\n } catch (e) {\n logger.error('Failed to parse serializedData for HardcodedOrderNavigator', e);\n }\n }\n }\n\n async getPendingReviews(): Promise<(StudySessionReviewItem & ScheduledCard)[]> {\n const reviews = await this.user.getPendingReviews(this.course.getCourseID());\n return reviews.map((r) => {\n return {\n ...r,\n contentSourceType: 'course',\n contentSourceID: this.course.getCourseID(),\n cardID: r.cardId,\n courseID: r.courseId,\n reviewID: r._id,\n status: 'review',\n };\n });\n }\n\n async getNewCards(limit: number = 99): Promise<StudySessionNewItem[]> {\n const activeCardIds = (await this.user.getActiveCards()).map((c: QualifiedCardID) => c.cardID);\n\n const newCardIds = this.orderedCardIds.filter((cardId) => !activeCardIds.includes(cardId));\n\n const cardsToReturn = newCardIds.slice(0, limit);\n\n return cardsToReturn.map((cardId) => {\n return {\n cardID: cardId,\n courseID: this.course.getCourseID(),\n contentSourceType: 'course',\n contentSourceID: this.course.getCourseID(),\n status: 'new',\n };\n });\n }\n\n /**\n * Get cards in hardcoded order with scores based on position.\n *\n * Earlier cards in the sequence get higher scores.\n * Score formula: 1.0 - (position / totalCards) * 0.5\n * This ensures scores range from 1.0 (first card) to 0.5+ (last card).\n *\n * This method supports both the legacy signature (limit only) and the\n * CardGenerator interface signature (limit, context).\n *\n * @param limit - Maximum number of cards to return\n * @param _context - Optional GeneratorContext (currently unused, but required for interface)\n */\n async getWeightedCards(limit: number, _context?: GeneratorContext): Promise<WeightedCard[]> {\n const activeCardIds = (await this.user.getActiveCards()).map((c: QualifiedCardID) => c.cardID);\n const reviews = await this.getPendingReviews();\n\n // Filter out already-active cards\n const newCardIds = this.orderedCardIds.filter((cardId) => !activeCardIds.includes(cardId));\n\n const totalCards = newCardIds.length;\n\n // Score new cards by position in sequence\n const scoredNew: WeightedCard[] = newCardIds.slice(0, limit).map((cardId, index) => {\n const position = index + 1;\n const score = Math.max(0.5, 1.0 - (index / totalCards) * 0.5);\n\n return {\n cardId,\n courseId: this.course.getCourseID(),\n score,\n provenance: [\n {\n strategy: 'hardcodedOrder',\n strategyName: this.strategyName || this.name,\n strategyId: this.strategyId || 'NAVIGATION_STRATEGY-hardcoded',\n action: 'generated',\n score,\n reason: `Position ${position} of ${totalCards} in fixed sequence, new card`,\n },\n ],\n };\n });\n\n // Score reviews at 1.0 (highest priority)\n const scoredReviews: WeightedCard[] = reviews.map((r) => ({\n cardId: r.cardID,\n courseId: r.courseID,\n score: 1.0,\n provenance: [\n {\n strategy: 'hardcodedOrder',\n strategyName: this.strategyName || this.name,\n strategyId: this.strategyId || 'NAVIGATION_STRATEGY-hardcoded',\n action: 'generated',\n score: 1.0,\n reason: 'Scheduled review, highest priority',\n },\n ],\n }));\n\n // Combine (reviews already sorted at top due to score=1.0)\n const all = [...scoredReviews, ...scoredNew];\n all.sort((a, b) => b.score - a.score);\n\n return all.slice(0, limit);\n }\n}\n","import type { ScheduledCard } from '../types/user';\nimport type { CourseDBInterface } from '../interfaces/courseDB';\nimport type { UserDBInterface } from '../interfaces/userDB';\nimport { ContentNavigator } from './index';\nimport type { WeightedCard } from './index';\nimport type { ContentNavigationStrategyData } from '../types/contentNavigationStrategy';\nimport type { StudySessionReviewItem, StudySessionNewItem } from '..';\nimport type { CardFilter, FilterContext } from './filters/types';\nimport { toCourseElo } from '@vue-skuilder/common';\n\n/**\n * A single prerequisite requirement for a tag.\n * Each prerequisite refers to one tag with its own mastery threshold.\n */\ninterface TagPrerequisite {\n /** The tag that must be mastered */\n tag: string;\n /** Thresholds for considering this prerequisite tag \"mastered\" */\n masteryThreshold?: {\n /** Minimum ELO score for mastery. If not set, uses avgElo comparison */\n minElo?: number;\n /** Minimum interaction count (default: 3) */\n minCount?: number;\n };\n}\n\n/**\n * Configuration for the HierarchyDefinition strategy\n */\nexport interface HierarchyConfig {\n /** Map of tag ID to its list of prerequisites (each with individual thresholds) */\n prerequisites: {\n [tagId: string]: TagPrerequisite[];\n };\n}\n\nconst DEFAULT_MIN_COUNT = 3;\n\n/**\n * A filter strategy that gates cards based on prerequisite mastery.\n *\n * Cards are locked until the user masters all prerequisite tags.\n * Locked cards receive score: 0 (hard filter).\n *\n * Mastery is determined by:\n * - User's ELO for the tag exceeds threshold (or avgElo if not specified)\n * - User has minimum interaction count with the tag\n *\n * Tags with no prerequisites are always unlocked.\n *\n * Implements CardFilter for use in Pipeline architecture.\n * Also extends ContentNavigator for backward compatibility.\n */\nexport default class HierarchyDefinitionNavigator extends ContentNavigator implements CardFilter {\n private config: HierarchyConfig;\n private _strategyData: ContentNavigationStrategyData;\n\n /** Human-readable name for CardFilter interface */\n name: string;\n\n constructor(\n user: UserDBInterface,\n course: CourseDBInterface,\n _strategyData: ContentNavigationStrategyData\n ) {\n super(user, course, _strategyData);\n this._strategyData = _strategyData;\n this.config = this.parseConfig(_strategyData.serializedData);\n this.name = _strategyData.name || 'Hierarchy Definition';\n }\n\n private parseConfig(serializedData: string): HierarchyConfig {\n try {\n const parsed = JSON.parse(serializedData);\n return {\n prerequisites: parsed.prerequisites || {},\n };\n } catch {\n // Return safe defaults if parsing fails\n return {\n prerequisites: {},\n };\n }\n }\n\n /**\n * Check if a specific prerequisite is satisfied\n */\n private isPrerequisiteMet(\n prereq: TagPrerequisite,\n userTagElo: { score: number; count: number } | undefined,\n userGlobalElo: number\n ): boolean {\n if (!userTagElo) return false;\n\n const minCount = prereq.masteryThreshold?.minCount ?? DEFAULT_MIN_COUNT;\n if (userTagElo.count < minCount) return false;\n\n if (prereq.masteryThreshold?.minElo !== undefined) {\n return userTagElo.score >= prereq.masteryThreshold.minElo;\n } else {\n // Default: user ELO for tag > global user ELO (proxy for \"above average\")\n return userTagElo.score >= userGlobalElo;\n }\n }\n\n /**\n * Get the set of tags the user has mastered.\n * A tag is \"mastered\" if it appears as a prerequisite somewhere and meets its threshold.\n */\n private async getMasteredTags(context: FilterContext): Promise<Set<string>> {\n const mastered = new Set<string>();\n\n try {\n const courseReg = await context.user.getCourseRegDoc(context.course.getCourseID());\n const userElo = toCourseElo(courseReg.elo);\n\n // Collect all unique prerequisite tags and check mastery for each\n for (const prereqs of Object.values(this.config.prerequisites)) {\n for (const prereq of prereqs) {\n const tagElo = userElo.tags[prereq.tag];\n if (this.isPrerequisiteMet(prereq, tagElo, userElo.global.score)) {\n mastered.add(prereq.tag);\n }\n }\n }\n } catch {\n // If we can't get user data, return empty set (no tags mastered)\n }\n\n return mastered;\n }\n\n /**\n * Get the set of tags that are unlocked (prerequisites met)\n */\n private getUnlockedTags(masteredTags: Set<string>): Set<string> {\n const unlocked = new Set<string>();\n\n for (const [tagId, prereqs] of Object.entries(this.config.prerequisites)) {\n const allPrereqsMet = prereqs.every((prereq) => masteredTags.has(prereq.tag));\n if (allPrereqsMet) {\n unlocked.add(tagId);\n }\n }\n\n return unlocked;\n }\n\n /**\n * Check if a tag has prerequisites defined in config\n */\n private hasPrerequisites(tagId: string): boolean {\n return tagId in this.config.prerequisites;\n }\n\n /**\n * Check if a card is unlocked and generate reason.\n */\n private async checkCardUnlock(\n card: WeightedCard,\n course: CourseDBInterface,\n unlockedTags: Set<string>,\n masteredTags: Set<string>\n ): Promise<{ isUnlocked: boolean; reason: string }> {\n try {\n // Pipeline hydrates tags before filters run\n const cardTags = card.tags ?? [];\n\n // Check each tag's prerequisite status\n const lockedTags = cardTags.filter(\n (tag) => this.hasPrerequisites(tag) && !unlockedTags.has(tag)\n );\n\n if (lockedTags.length === 0) {\n const tagList = cardTags.length > 0 ? cardTags.join(', ') : 'none';\n return {\n isUnlocked: true,\n reason: `Prerequisites met, tags: ${tagList}`,\n };\n }\n\n // Find missing prerequisites for locked tags\n const missingPrereqs = lockedTags.flatMap((tag) => {\n const prereqs = this.config.prerequisites[tag] || [];\n return prereqs.filter((p) => !masteredTags.has(p.tag)).map((p) => p.tag);\n });\n\n return {\n isUnlocked: false,\n reason: `Blocked: missing prerequisites ${missingPrereqs.join(', ')} for tags ${lockedTags.join(', ')}`,\n };\n } catch {\n // If we can't get tags, assume unlocked (fail open)\n return {\n isUnlocked: true,\n reason: 'Prerequisites check skipped (tag lookup failed)',\n };\n }\n }\n\n /**\n * CardFilter.transform implementation.\n *\n * Apply prerequisite gating to cards. Cards with locked tags receive score: 0.\n */\n async transform(cards: WeightedCard[], context: FilterContext): Promise<WeightedCard[]> {\n // Get mastery state\n const masteredTags = await this.getMasteredTags(context);\n const unlockedTags = this.getUnlockedTags(masteredTags);\n\n // Apply prerequisite gating as score multiplier\n const gated: WeightedCard[] = [];\n\n for (const card of cards) {\n const { isUnlocked, reason } = await this.checkCardUnlock(\n card,\n context.course,\n unlockedTags,\n masteredTags\n );\n const finalScore = isUnlocked ? card.score : 0;\n const action = isUnlocked ? 'passed' : 'penalized';\n\n gated.push({\n ...card,\n score: finalScore,\n provenance: [\n ...card.provenance,\n {\n strategy: 'hierarchyDefinition',\n strategyName: this.strategyName || this.name,\n strategyId: this.strategyId || 'NAVIGATION_STRATEGY-hierarchy',\n action,\n score: finalScore,\n reason,\n },\n ],\n });\n }\n\n return gated;\n }\n\n /**\n * Legacy getWeightedCards - now throws as filters should not be used as generators.\n *\n * Use transform() via Pipeline instead.\n */\n async getWeightedCards(_limit: number): Promise<WeightedCard[]> {\n throw new Error(\n 'HierarchyDefinitionNavigator is a filter and should not be used as a generator. ' +\n 'Use Pipeline with a generator and this filter via transform().'\n );\n }\n\n // Legacy methods - stub implementations since filters don't generate cards\n\n async getNewCards(_n?: number): Promise<StudySessionNewItem[]> {\n return [];\n }\n\n async getPendingReviews(): Promise<(StudySessionReviewItem & ScheduledCard)[]> {\n return [];\n }\n}\n","// ============================================================================\n// INFERRED PREFERENCE NAVIGATOR — STUB\n// ============================================================================\n//\n// STATUS: NOT IMPLEMENTED — This file documents architectural intent only.\n//\n// ============================================================================\n//\n// ## Purpose\n//\n// Inferred preferences are learned from user behavior, as opposed to explicit\n// preferences which are configured via UI. The system observes patterns in\n// user interactions and adjusts card selection accordingly.\n//\n// ## Inference Signals\n//\n// Potential signals to learn from:\n//\n// 1. **Card dismissal patterns**: User consistently skips certain card types\n// 2. **Time-on-card**: User spends less time on certain content (boredom?)\n// 3. **Error patterns**: User struggles with certain presentation styles\n// 4. **Session timing**: User performs better at certain times of day\n// 5. **Tag success rates**: User masters some tags faster than others\n//\n// ## Inferred State (Proposed)\n//\n// ```typescript\n// interface InferredPreferenceState {\n// // Learned tag affinities (positive = user does well, negative = struggles)\n// tagAffinities: Record<string, number>;\n//\n// // Presentation style preferences\n// preferredStyles: {\n// visualVsText: number; // -1 to 1 (negative = text, positive = visual)\n// shortVsLong: number; // -1 to 1 (negative = long, positive = short)\n// };\n//\n// // Temporal patterns\n// optimalSessionLength: number; // minutes\n// optimalTimeOfDay: number; // hour (0-23)\n//\n// // Confidence in inferences\n// sampleSize: number;\n// lastUpdated: string;\n// }\n// ```\n//\n// ## Relationship to Explicit Preferences\n//\n// - Explicit preferences (UserTagPreferenceFilter) always take precedence\n// - Inferred preferences act as soft suggestions when no explicit pref exists\n// - User can \"lock in\" an inference as an explicit preference via UI\n// - User can dismiss/override an inference (\"I actually like text cards\")\n//\n// ## Transparency Requirements\n//\n// Inferred preferences must be:\n//\n// 1. **Visible**: User can see what the system has inferred\n// 2. **Explainable**: \"We noticed you master visual cards faster\"\n// 3. **Overridable**: User can disable or invert any inference\n// 4. **Forgettable**: User can reset inferences and start fresh\n//\n// ## Implementation Considerations\n//\n// 1. **Cold start**: Need minimum sample size before inferring\n// 2. **Drift**: Preferences may change over time; use decay/recency weighting\n// 3. **Privacy**: Inference data is personal; handle with care\n// 4. **Bias**: Avoid reinforcing accidental patterns as permanent preferences\n//\n// ## Related Files\n//\n// - `filters/userTagPreference.ts` — Explicit preferences (takes precedence)\n// - `userGoal.ts` — Goals (destination, not path)\n// - `../types/strategyState.ts` — Storage mechanism\n//\n// ## Next Steps\n//\n// 1. Define minimum viable inference signals\n// 2. Design inference algorithms (simple heuristics vs ML)\n// 3. Build transparency UI (\"Here's what we learned about you\")\n// 4. Implement override/dismiss mechanism\n// 5. Add to card record collection for inference input\n//\n// ============================================================================\n\n// Placeholder export to make this a valid module\nexport const INFERRED_PREFERENCE_NAVIGATOR_STUB = true;\n\n/**\n * @stub InferredPreferenceNavigator\n *\n * A navigator that learns user preferences from behavior patterns.\n * See module-level documentation for architectural intent.\n *\n * NOT IMPLEMENTED — This is a design placeholder.\n */\nexport interface InferredPreferenceState {\n /** Learned affinity scores per tag (-1 to 1) */\n tagAffinities: Record<string, number>;\n\n /** Number of card interactions used to build inferences */\n sampleSize: number;\n\n /** ISO timestamp of last inference update */\n updatedAt: string;\n}\n","import type { ScheduledCard } from '../types/user';\nimport type { CourseDBInterface } from '../interfaces/courseDB';\nimport type { UserDBInterface } from '../interfaces/userDB';\nimport { ContentNavigator } from './index';\nimport type { WeightedCard } from './index';\nimport type { ContentNavigationStrategyData } from '../types/contentNavigationStrategy';\nimport type { StudySessionReviewItem, StudySessionNewItem } from '..';\nimport type { CardFilter, FilterContext } from './filters/types';\nimport { toCourseElo } from '@vue-skuilder/common';\n\n/**\n * Configuration for the InterferenceMitigator strategy.\n *\n * Course authors define explicit interference relationships between tags.\n * The mitigator discourages introducing new concepts that interfere with\n * currently immature learnings.\n */\n/**\n * A single interference group with its own decay coefficient.\n */\nexport interface InterferenceGroup {\n /** Tags that interfere with each other in this group */\n tags: string[];\n /** How strongly these tags interfere (0-1, default: 0.8). Higher = stronger avoidance. */\n decay?: number;\n}\n\nexport interface InterferenceConfig {\n /**\n * Groups of tags that interfere with each other.\n * Each group can have its own decay coefficient.\n *\n * Example: [\n * { tags: [\"b\", \"d\", \"p\"], decay: 0.9 }, // visual similarity - strong\n * { tags: [\"d\", \"t\"], decay: 0.7 }, // phonetic similarity - moderate\n * { tags: [\"m\", \"n\"], decay: 0.8 }\n * ]\n */\n interferenceSets: InterferenceGroup[];\n\n /**\n * Threshold below which a tag is considered \"immature\" (still being learned).\n * Immature tags trigger interference avoidance for their interference partners.\n */\n maturityThreshold?: {\n /** Minimum interaction count to be considered mature (default: 10) */\n minCount?: number;\n /** Minimum ELO score to be considered mature (optional) */\n minElo?: number;\n /**\n * Minimum elapsed time (in days) since first interaction to be considered mature.\n * Prevents recent cramming success from indicating maturity.\n * The skill should be \"lindy\" — maintained over time.\n */\n minElapsedDays?: number;\n };\n\n /**\n * Default decay for groups that don't specify their own (0-1, default: 0.8).\n */\n defaultDecay?: number;\n}\n\nconst DEFAULT_MIN_COUNT = 10;\nconst DEFAULT_MIN_ELAPSED_DAYS = 3;\nconst DEFAULT_INTERFERENCE_DECAY = 0.8;\n\n/**\n * A filter strategy that avoids introducing confusable concepts simultaneously.\n *\n * When a user is learning a concept (tag is \"immature\"), this strategy reduces\n * scores for cards tagged with interfering concepts. This encourages the system\n * to introduce new content that is maximally distant from current learning focus.\n *\n * Example: While learning 'd', prefer introducing 'x' over 'b' (visual interference)\n * or 't' (phonetic interference).\n *\n * Implements CardFilter for use in Pipeline architecture.\n * Also extends ContentNavigator for backward compatibility.\n */\nexport default class InterferenceMitigatorNavigator extends ContentNavigator implements CardFilter {\n private config: InterferenceConfig;\n private _strategyData: ContentNavigationStrategyData;\n\n /** Human-readable name for CardFilter interface */\n name: string;\n\n /** Precomputed map: tag -> set of { partner, decay } it interferes with */\n private interferenceMap: Map<string, Array<{ partner: string; decay: number }>>;\n\n constructor(\n user: UserDBInterface,\n course: CourseDBInterface,\n _strategyData: ContentNavigationStrategyData\n ) {\n super(user, course, _strategyData);\n this._strategyData = _strategyData;\n this.config = this.parseConfig(_strategyData.serializedData);\n this.interferenceMap = this.buildInterferenceMap();\n this.name = _strategyData.name || 'Interference Mitigator';\n }\n\n private parseConfig(serializedData: string): InterferenceConfig {\n try {\n const parsed = JSON.parse(serializedData);\n // Normalize legacy format (string[][]) to new format (InterferenceGroup[])\n let sets: InterferenceGroup[] = parsed.interferenceSets || [];\n if (sets.length > 0 && Array.isArray(sets[0])) {\n // Legacy format: convert string[][] to InterferenceGroup[]\n sets = (sets as unknown as string[][]).map((tags) => ({ tags }));\n }\n\n return {\n interferenceSets: sets,\n maturityThreshold: {\n minCount: parsed.maturityThreshold?.minCount ?? DEFAULT_MIN_COUNT,\n minElo: parsed.maturityThreshold?.minElo,\n minElapsedDays: parsed.maturityThreshold?.minElapsedDays ?? DEFAULT_MIN_ELAPSED_DAYS,\n },\n defaultDecay: parsed.defaultDecay ?? DEFAULT_INTERFERENCE_DECAY,\n };\n } catch {\n return {\n interferenceSets: [],\n maturityThreshold: {\n minCount: DEFAULT_MIN_COUNT,\n minElapsedDays: DEFAULT_MIN_ELAPSED_DAYS,\n },\n defaultDecay: DEFAULT_INTERFERENCE_DECAY,\n };\n }\n }\n\n /**\n * Build a map from each tag to its interference partners with decay coefficients.\n * If tags A, B, C are in an interference group with decay 0.8, then:\n * - A interferes with B (decay 0.8) and C (decay 0.8)\n * - B interferes with A (decay 0.8) and C (decay 0.8)\n * - etc.\n */\n private buildInterferenceMap(): Map<string, Array<{ partner: string; decay: number }>> {\n const map = new Map<string, Array<{ partner: string; decay: number }>>();\n\n for (const group of this.config.interferenceSets) {\n const decay = group.decay ?? this.config.defaultDecay ?? DEFAULT_INTERFERENCE_DECAY;\n\n for (const tag of group.tags) {\n if (!map.has(tag)) {\n map.set(tag, []);\n }\n const partners = map.get(tag)!;\n for (const other of group.tags) {\n if (other !== tag) {\n // Check if partner already exists (from overlapping groups)\n const existing = partners.find((p) => p.partner === other);\n if (existing) {\n // Use the stronger (higher) decay\n existing.decay = Math.max(existing.decay, decay);\n } else {\n partners.push({ partner: other, decay });\n }\n }\n }\n }\n }\n\n return map;\n }\n\n /**\n * Get the set of tags that are currently immature for this user.\n * A tag is immature if the user has interacted with it but hasn't\n * reached the maturity threshold.\n */\n private async getImmatureTags(context: FilterContext): Promise<Set<string>> {\n const immature = new Set<string>();\n\n try {\n const courseReg = await context.user.getCourseRegDoc(context.course.getCourseID());\n const userElo = toCourseElo(courseReg.elo);\n\n const minCount = this.config.maturityThreshold?.minCount ?? DEFAULT_MIN_COUNT;\n const minElo = this.config.maturityThreshold?.minElo;\n\n // TODO: To properly check elapsed time, we need access to first interaction timestamp.\n // For now, we use count as a proxy (more interactions = more time elapsed).\n // Future: query card history for earliest timestamp per tag.\n const minElapsedDays =\n this.config.maturityThreshold?.minElapsedDays ?? DEFAULT_MIN_ELAPSED_DAYS;\n const minCountForElapsed = minElapsedDays * 2; // Rough proxy: ~2 interactions per day\n\n for (const [tagId, tagElo] of Object.entries(userElo.tags)) {\n // Only consider tags that have been started (count > 0)\n if (tagElo.count === 0) continue;\n\n // Check if below maturity threshold\n const belowCount = tagElo.count < minCount;\n const belowElo = minElo !== undefined && tagElo.score < minElo;\n const belowElapsed = tagElo.count < minCountForElapsed; // Proxy for time\n\n if (belowCount || belowElo || belowElapsed) {\n immature.add(tagId);\n }\n }\n } catch {\n // If we can't get user data, assume no immature tags\n }\n\n return immature;\n }\n\n /**\n * Get all tags that interfere with any immature tag, along with their decay coefficients.\n * These are the tags we want to avoid introducing.\n */\n private getTagsToAvoid(immatureTags: Set<string>): Map<string, number> {\n const avoid = new Map<string, number>();\n\n for (const immatureTag of immatureTags) {\n const partners = this.interferenceMap.get(immatureTag);\n if (partners) {\n for (const { partner, decay } of partners) {\n // Avoid the partner, but not if it's also immature\n // (if both are immature, we're already learning both)\n if (!immatureTags.has(partner)) {\n // Use the strongest (highest) decay if partner appears multiple times\n const existing = avoid.get(partner) ?? 0;\n avoid.set(partner, Math.max(existing, decay));\n }\n }\n }\n }\n\n return avoid;\n }\n\n /**\n * Compute interference score reduction for a card.\n * Returns: { multiplier, interfering tags, reason }\n */\n private computeInterferenceEffect(\n cardTags: string[],\n tagsToAvoid: Map<string, number>,\n immatureTags: Set<string>\n ): { multiplier: number; interferingTags: string[]; reason: string } {\n if (tagsToAvoid.size === 0) {\n return {\n multiplier: 1.0,\n interferingTags: [],\n reason: 'No interference detected',\n };\n }\n\n let multiplier = 1.0;\n const interferingTags: string[] = [];\n\n for (const tag of cardTags) {\n const decay = tagsToAvoid.get(tag);\n if (decay !== undefined) {\n interferingTags.push(tag);\n multiplier *= 1.0 - decay;\n }\n }\n\n if (interferingTags.length === 0) {\n return {\n multiplier: 1.0,\n interferingTags: [],\n reason: 'No interference detected',\n };\n }\n\n // Find which immature tags these interfere with\n const causingTags = new Set<string>();\n for (const tag of interferingTags) {\n for (const immatureTag of immatureTags) {\n const partners = this.interferenceMap.get(immatureTag);\n if (partners?.some((p) => p.partner === tag)) {\n causingTags.add(immatureTag);\n }\n }\n }\n\n const reason = `Interferes with immature tags ${Array.from(causingTags).join(', ')} (tags: ${interferingTags.join(', ')}, multiplier: ${multiplier.toFixed(2)})`;\n\n return { multiplier, interferingTags, reason };\n }\n\n /**\n * CardFilter.transform implementation.\n *\n * Apply interference-aware scoring. Cards with tags that interfere with\n * immature learnings get reduced scores.\n */\n async transform(cards: WeightedCard[], context: FilterContext): Promise<WeightedCard[]> {\n // Identify what to avoid\n const immatureTags = await this.getImmatureTags(context);\n const tagsToAvoid = this.getTagsToAvoid(immatureTags);\n\n // Adjust scores based on interference\n const adjusted: WeightedCard[] = [];\n\n for (const card of cards) {\n const cardTags = card.tags ?? [];\n const { multiplier, reason } = this.computeInterferenceEffect(\n cardTags,\n tagsToAvoid,\n immatureTags\n );\n const finalScore = card.score * multiplier;\n\n const action = multiplier < 1.0 ? 'penalized' : multiplier > 1.0 ? 'boosted' : 'passed';\n\n adjusted.push({\n ...card,\n score: finalScore,\n provenance: [\n ...card.provenance,\n {\n strategy: 'interferenceMitigator',\n strategyName: this.strategyName || this.name,\n strategyId: this.strategyId || 'NAVIGATION_STRATEGY-interference',\n action,\n score: finalScore,\n reason,\n },\n ],\n });\n }\n\n return adjusted;\n }\n\n /**\n * Legacy getWeightedCards - now throws as filters should not be used as generators.\n *\n * Use transform() via Pipeline instead.\n */\n async getWeightedCards(_limit: number): Promise<WeightedCard[]> {\n throw new Error(\n 'InterferenceMitigatorNavigator is a filter and should not be used as a generator. ' +\n 'Use Pipeline with a generator and this filter via transform().'\n );\n }\n\n // Legacy methods - stub implementations since filters don't generate cards\n\n async getNewCards(_n?: number): Promise<StudySessionNewItem[]> {\n return [];\n }\n\n async getPendingReviews(): Promise<(StudySessionReviewItem & ScheduledCard)[]> {\n return [];\n }\n}\n","import type { ScheduledCard } from '../types/user';\nimport type { CourseDBInterface } from '../interfaces/courseDB';\nimport type { UserDBInterface } from '../interfaces/userDB';\nimport { ContentNavigator } from './index';\nimport type { WeightedCard } from './index';\nimport type { ContentNavigationStrategyData } from '../types/contentNavigationStrategy';\nimport type { StudySessionReviewItem, StudySessionNewItem } from '..';\nimport type { CardFilter, FilterContext } from './filters/types';\n\n/**\n * Configuration for the RelativePriority strategy.\n *\n * Course authors define priority weights for tags, allowing the system\n * to prefer high-utility content (common, well-behaved patterns) over\n * lower-utility content (rare, irregular patterns).\n *\n * Example use case: In phonics, prefer teaching 's' (common, consistent)\n * before 'x' or 'z' (rare, sometimes irregular).\n */\nexport interface RelativePriorityConfig {\n /**\n * Map of tag ID to priority weight (0-1).\n *\n * 1.0 = highest priority (present first)\n * 0.5 = neutral\n * 0.0 = lowest priority (defer until later)\n *\n * Example:\n * {\n * \"letter-s\": 0.95,\n * \"letter-t\": 0.90,\n * \"letter-x\": 0.10,\n * \"letter-z\": 0.05\n * }\n */\n tagPriorities: { [tagId: string]: number };\n\n /**\n * Priority for tags not explicitly listed (default: 0.5).\n * 0.5 means unlisted tags have neutral effect on scoring.\n */\n defaultPriority?: number;\n\n /**\n * How to combine priorities when a card has multiple tags.\n *\n * - 'max': Use the highest priority among the card's tags (default)\n * - 'average': Average all tag priorities\n * - 'min': Use the lowest priority (conservative)\n */\n combineMode?: 'max' | 'average' | 'min';\n\n /**\n * How strongly priority influences the final score (0-1, default: 0.5).\n *\n * At 0.0: Priority has no effect (pure delegate scoring)\n * At 0.5: Priority can boost/reduce scores by up to 25%\n * At 1.0: Priority can boost/reduce scores by up to 50%\n *\n * The boost factor formula: 1 + (priority - 0.5) * priorityInfluence\n * - Priority 1.0 with influence 0.5 → boost of 1.25\n * - Priority 0.5 with influence 0.5 → boost of 1.00 (neutral)\n * - Priority 0.0 with influence 0.5 → boost of 0.75\n */\n priorityInfluence?: number;\n}\n\nconst DEFAULT_PRIORITY = 0.5;\nconst DEFAULT_PRIORITY_INFLUENCE = 0.5;\nconst DEFAULT_COMBINE_MODE: 'max' | 'average' | 'min' = 'max';\n\n/**\n * A filter strategy that boosts scores for high-utility content.\n *\n * Course authors assign priority weights to tags. Cards with high-priority\n * tags get boosted scores, making them more likely to be presented first.\n * This allows teaching the most useful, well-behaved concepts before\n * moving on to rarer or more irregular ones.\n *\n * Example: When teaching phonics, prioritize common letters (s, t, a) over\n * rare ones (x, z, q) by assigning higher priority weights to common letters.\n *\n * Implements CardFilter for use in Pipeline architecture.\n * Also extends ContentNavigator for backward compatibility.\n */\nexport default class RelativePriorityNavigator extends ContentNavigator implements CardFilter {\n private config: RelativePriorityConfig;\n private _strategyData: ContentNavigationStrategyData;\n\n /** Human-readable name for CardFilter interface */\n name: string;\n\n constructor(\n user: UserDBInterface,\n course: CourseDBInterface,\n _strategyData: ContentNavigationStrategyData\n ) {\n super(user, course, _strategyData);\n this._strategyData = _strategyData;\n this.config = this.parseConfig(_strategyData.serializedData);\n this.name = _strategyData.name || 'Relative Priority';\n }\n\n private parseConfig(serializedData: string): RelativePriorityConfig {\n try {\n const parsed = JSON.parse(serializedData);\n return {\n tagPriorities: parsed.tagPriorities || {},\n defaultPriority: parsed.defaultPriority ?? DEFAULT_PRIORITY,\n combineMode: parsed.combineMode ?? DEFAULT_COMBINE_MODE,\n priorityInfluence: parsed.priorityInfluence ?? DEFAULT_PRIORITY_INFLUENCE,\n };\n } catch {\n // Return safe defaults if parsing fails\n return {\n tagPriorities: {},\n defaultPriority: DEFAULT_PRIORITY,\n combineMode: DEFAULT_COMBINE_MODE,\n priorityInfluence: DEFAULT_PRIORITY_INFLUENCE,\n };\n }\n }\n\n /**\n * Look up the priority for a tag.\n */\n private getTagPriority(tagId: string): number {\n return this.config.tagPriorities[tagId] ?? this.config.defaultPriority ?? DEFAULT_PRIORITY;\n }\n\n /**\n * Compute combined priority for a card based on its tags.\n */\n private computeCardPriority(cardTags: string[]): number {\n if (cardTags.length === 0) {\n return this.config.defaultPriority ?? DEFAULT_PRIORITY;\n }\n\n const priorities = cardTags.map((tag) => this.getTagPriority(tag));\n\n switch (this.config.combineMode) {\n case 'max':\n return Math.max(...priorities);\n case 'min':\n return Math.min(...priorities);\n case 'average':\n return priorities.reduce((sum, p) => sum + p, 0) / priorities.length;\n default:\n return Math.max(...priorities);\n }\n }\n\n /**\n * Compute boost factor based on priority.\n *\n * The formula: 1 + (priority - 0.5) * priorityInfluence\n *\n * This creates a multiplier centered around 1.0:\n * - Priority 1.0 with influence 0.5 → 1.25 (25% boost)\n * - Priority 0.5 with any influence → 1.00 (neutral)\n * - Priority 0.0 with influence 0.5 → 0.75 (25% reduction)\n */\n private computeBoostFactor(priority: number): number {\n const influence = this.config.priorityInfluence ?? DEFAULT_PRIORITY_INFLUENCE;\n return 1 + (priority - 0.5) * influence;\n }\n\n /**\n * Build human-readable reason for priority adjustment.\n */\n private buildPriorityReason(\n cardTags: string[],\n priority: number,\n boostFactor: number,\n finalScore: number\n ): string {\n if (cardTags.length === 0) {\n return `No tags, neutral priority (${priority.toFixed(2)})`;\n }\n\n const tagList = cardTags.slice(0, 3).join(', ');\n const more = cardTags.length > 3 ? ` (+${cardTags.length - 3} more)` : '';\n\n if (boostFactor === 1.0) {\n return `Neutral priority (${priority.toFixed(2)}) for tags: ${tagList}${more}`;\n } else if (boostFactor > 1.0) {\n return `High-priority tags: ${tagList}${more} (priority ${priority.toFixed(2)} → boost ${boostFactor.toFixed(2)}x → ${finalScore.toFixed(2)})`;\n } else {\n return `Low-priority tags: ${tagList}${more} (priority ${priority.toFixed(2)} → reduce ${boostFactor.toFixed(2)}x → ${finalScore.toFixed(2)})`;\n }\n }\n\n /**\n * CardFilter.transform implementation.\n *\n * Apply priority-adjusted scoring. Cards with high-priority tags get boosted,\n * cards with low-priority tags get reduced scores.\n */\n async transform(cards: WeightedCard[], _context: FilterContext): Promise<WeightedCard[]> {\n const adjusted: WeightedCard[] = await Promise.all(\n cards.map(async (card) => {\n const cardTags = card.tags ?? [];\n const priority = this.computeCardPriority(cardTags);\n const boostFactor = this.computeBoostFactor(priority);\n const finalScore = Math.max(0, Math.min(1, card.score * boostFactor));\n\n // Determine action based on boost factor\n const action = boostFactor > 1.0 ? 'boosted' : boostFactor < 1.0 ? 'penalized' : 'passed';\n\n // Build reason explaining priority adjustment\n const reason = this.buildPriorityReason(cardTags, priority, boostFactor, finalScore);\n\n return {\n ...card,\n score: finalScore,\n provenance: [\n ...card.provenance,\n {\n strategy: 'relativePriority',\n strategyName: this.strategyName || this.name,\n strategyId: this.strategyId || 'NAVIGATION_STRATEGY-priority',\n action,\n score: finalScore,\n reason,\n },\n ],\n };\n })\n );\n\n return adjusted;\n }\n\n /**\n * Legacy getWeightedCards - now throws as filters should not be used as generators.\n *\n * Use transform() via Pipeline instead.\n */\n async getWeightedCards(_limit: number): Promise<WeightedCard[]> {\n throw new Error(\n 'RelativePriorityNavigator is a filter and should not be used as a generator. ' +\n 'Use Pipeline with a generator and this filter via transform().'\n );\n }\n\n // Legacy methods - stub implementations since filters don't generate cards\n\n async getNewCards(_n?: number): Promise<StudySessionNewItem[]> {\n return [];\n }\n\n async getPendingReviews(): Promise<(StudySessionReviewItem & ScheduledCard)[]> {\n return [];\n }\n}\n","import moment from 'moment';\nimport type { ScheduledCard } from '../types/user';\nimport type { CourseDBInterface } from '../interfaces/courseDB';\nimport type { UserDBInterface } from '../interfaces/userDB';\nimport { ContentNavigator } from './index';\nimport type { WeightedCard } from './index';\nimport type { ContentNavigationStrategyData } from '../types/contentNavigationStrategy';\nimport type { StudySessionReviewItem, StudySessionNewItem } from '../interfaces/contentSource';\nimport type { CardGenerator, GeneratorContext } from './generators/types';\n\n// ============================================================================\n// SRS NAVIGATOR\n// ============================================================================\n//\n// A generator strategy that scores review cards by urgency.\n//\n// Urgency is determined by two factors:\n// 1. Overdueness - how far past the scheduled review time\n// 2. Interval recency - shorter scheduled intervals indicate \"novel content in progress\"\n//\n// A card with a 3-day interval that's 2 days overdue is more urgent than a card\n// with a 6-month interval that's 2 days overdue. The shorter interval represents\n// active learning at higher resolution.\n//\n// This navigator only handles reviews - it does not generate new cards.\n// For new cards, use ELONavigator or another generator via CompositeGenerator.\n//\n// ============================================================================\n\n/**\n * Configuration for the SRS strategy.\n * Currently minimal - the algorithm is not parameterized.\n */\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type\nexport interface SRSConfig {\n // Future: configurable urgency curves, thresholds, etc.\n}\n\n/**\n * A navigation strategy that scores review cards by urgency.\n *\n * Implements CardGenerator for use in Pipeline architecture.\n * Also extends ContentNavigator for backward compatibility with legacy code.\n *\n * Higher scores indicate more urgent reviews:\n * - Cards that are more overdue (relative to their interval) score higher\n * - Cards with shorter intervals (recent learning) score higher\n *\n * Only returns cards that are actually due (reviewTime has passed).\n * Does not generate new cards - use with CompositeGenerator for mixed content.\n */\nexport default class SRSNavigator extends ContentNavigator implements CardGenerator {\n /** Human-readable name for CardGenerator interface */\n name: string;\n\n constructor(\n user: UserDBInterface,\n course: CourseDBInterface,\n strategyData?: ContentNavigationStrategyData\n ) {\n super(user, course, strategyData as ContentNavigationStrategyData);\n this.name = strategyData?.name || 'SRS';\n }\n\n /**\n * Get review cards scored by urgency.\n *\n * Score formula combines:\n * - Relative overdueness: hoursOverdue / intervalHours\n * - Interval recency: exponential decay favoring shorter intervals\n *\n * Cards not yet due are excluded (not scored as 0).\n *\n * This method supports both the legacy signature (limit only) and the\n * CardGenerator interface signature (limit, context).\n *\n * @param limit - Maximum number of cards to return\n * @param _context - Optional GeneratorContext (currently unused, but required for interface)\n */\n async getWeightedCards(limit: number, _context?: GeneratorContext): Promise<WeightedCard[]> {\n if (!this.user || !this.course) {\n throw new Error('SRSNavigator requires user and course to be set');\n }\n\n const reviews = await this.user.getPendingReviews(this.course.getCourseID());\n const now = moment.utc();\n\n // Filter to only cards that are actually due\n const dueReviews = reviews.filter((r) => now.isAfter(moment.utc(r.reviewTime)));\n\n const scored = dueReviews.map((review) => {\n const { score, reason } = this.computeUrgencyScore(review, now);\n\n return {\n cardId: review.cardId,\n courseId: review.courseId,\n score,\n provenance: [\n {\n strategy: 'srs',\n strategyName: this.strategyName || this.name,\n strategyId: this.strategyId || 'NAVIGATION_STRATEGY-SRS-default',\n action: 'generated' as const,\n score,\n reason,\n },\n ],\n };\n });\n\n // Sort by score descending and limit\n return scored.sort((a, b) => b.score - a.score).slice(0, limit);\n }\n\n /**\n * Compute urgency score for a review card.\n *\n * Two factors:\n * 1. Relative overdueness = hoursOverdue / intervalHours\n * - 2 days overdue on 3-day interval = 0.67 (urgent)\n * - 2 days overdue on 180-day interval = 0.01 (not urgent)\n *\n * 2. Interval recency factor = 0.3 + 0.7 * exp(-intervalHours / 720)\n * - 24h interval → ~1.0 (very recent learning)\n * - 30 days (720h) → ~0.56\n * - 180 days → ~0.30\n *\n * Combined: base 0.5 + weighted average of factors * 0.45\n * Result range: approximately 0.5 to 0.95\n */\n private computeUrgencyScore(\n review: ScheduledCard,\n now: moment.Moment\n ): { score: number; reason: string } {\n const scheduledAt = moment.utc(review.scheduledAt);\n const due = moment.utc(review.reviewTime);\n\n // Interval = time between scheduling and due date (minimum 1 hour to avoid division issues)\n const intervalHours = Math.max(1, due.diff(scheduledAt, 'hours'));\n const hoursOverdue = now.diff(due, 'hours');\n\n // Relative overdueness: how late relative to the interval\n const relativeOverdue = hoursOverdue / intervalHours;\n\n // Interval recency factor: shorter intervals = more urgent\n // Exponential decay with 720h (30 days) as the characteristic time\n const recencyFactor = 0.3 + 0.7 * Math.exp(-intervalHours / 720);\n\n // Combined urgency: weighted average of relative overdue and recency\n // Clamp relative overdue contribution to [0, 1] to avoid runaway scores\n const overdueContribution = Math.min(1.0, Math.max(0, relativeOverdue));\n const urgency = overdueContribution * 0.5 + recencyFactor * 0.5;\n\n // Final score: base 0.5 + urgency contribution, capped at 0.95\n const score = Math.min(0.95, 0.5 + urgency * 0.45);\n\n const reason =\n `${Math.round(hoursOverdue)}h overdue (interval: ${Math.round(intervalHours)}h, ` +\n `relative: ${relativeOverdue.toFixed(2)}), recency: ${recencyFactor.toFixed(2)}, review`;\n\n return { score, reason };\n }\n\n /**\n * Get pending reviews in legacy format.\n *\n * Returns all pending reviews for the course, enriched with session item fields.\n */\n async getPendingReviews(): Promise<(StudySessionReviewItem & ScheduledCard)[]> {\n if (!this.user || !this.course) {\n throw new Error('SRSNavigator requires user and course to be set');\n }\n\n const reviews = await this.user.getPendingReviews(this.course.getCourseID());\n\n return reviews.map((r) => ({\n ...r,\n contentSourceType: 'course' as const,\n contentSourceID: this.course!.getCourseID(),\n cardID: r.cardId,\n courseID: r.courseId,\n qualifiedID: `${r.courseId}-${r.cardId}`,\n reviewID: r._id,\n status: 'review' as const,\n }));\n }\n\n /**\n * SRS does not generate new cards.\n * Use ELONavigator or another generator for new cards.\n */\n async getNewCards(_n?: number): Promise<StudySessionNewItem[]> {\n return [];\n }\n}\n","// ============================================================================\n// USER GOAL NAVIGATOR — STUB\n// ============================================================================\n//\n// STATUS: NOT IMPLEMENTED — This file documents architectural intent only.\n//\n// ============================================================================\n//\n// ## Purpose\n//\n// Goals define WHAT the user wants to learn, as opposed to preferences which\n// define HOW they want to learn. Goals affect:\n//\n// 1. **Content scoping**: Which tags/content are relevant to this user\n// 2. **Progress tracking**: ELO is measured against goal-relevant content\n// 3. **Completion criteria**: User is \"done\" when goal mastery is achieved\n// 4. **Curriculum composition**: Goals enable cross-curriculum dependencies\n//\n// ## Goals vs Preferences\n//\n// | Aspect | Goal | Preference |\n// |---------------|-------------------------------|-------------------------------|\n// | Defines | Destination (what to learn) | Path (how to learn) |\n// | Example | \"Master ear-training\" | \"Skip text-heavy cards\" |\n// | Affects ELO | Yes — scopes what's tracked | No — just filters cards |\n// | Completion | Yes — defines \"done\" | No — persists indefinitely |\n// | Filter impl | UserGoalNavigator | UserTagPreferenceFilter |\n//\n// ## Curriculum Composition\n//\n// Goals enable software-style composition for curricula. A physics course\n// can teach classical mechanics without owning the calculus prerequisites.\n//\n// Instead, it declares a dependency:\n//\n// ```typescript\n// interface CurriculumDependency {\n// // NPM-style package resolution\n// curriculumId: string; // e.g., \"@skuilder/calculus\"\n// version: string; // e.g., \"^2.0.0\" (semver)\n//\n// // Goal within that curriculum\n// goal: string; // e.g., \"differential-calculus\"\n//\n// // How this maps to local prerequisites\n// satisfiesLocalTags: string[]; // e.g., [\"calculus-prereq\"]\n// }\n// ```\n//\n// When a physics card requires \"calculus-prereq\", the system:\n// 1. Checks if user has achieved the \"differential-calculus\" goal in @skuilder/calculus\n// 2. If not, defers to that curriculum to teach the prerequisite\n// 3. Returns to physics once the goal is satisfied\n//\n// This allows:\n// - Specialized curricula (calculus experts author calculus content)\n// - Reusable prerequisites across multiple courses\n// - User can bring their own \"calculus credential\" from prior learning\n//\n// ## User Goal State (Proposed)\n//\n// ```typescript\n// interface UserGoalState {\n// // Primary goals — what the user wants to achieve\n// targetTags: string[];\n//\n// // Excluded goals — content the user explicitly doesn't care about\n// excludedTags: string[];\n//\n// // Cross-curriculum goals (for composition)\n// externalGoals?: {\n// curriculumId: string;\n// goal: string;\n// status: 'not-started' | 'in-progress' | 'achieved';\n// }[];\n//\n// // When this goal configuration was set\n// updatedAt: string;\n// }\n// ```\n//\n// ## Implementation Considerations\n//\n// 1. **ELO Scoping**: When goals are set, user ELO tracking should focus on\n// goal-relevant tags. This may require changes to ELO update logic.\n//\n// 2. **Progress Reporting**: UI should show progress toward goals, not just\n// overall course completion.\n//\n// 3. **Goal Achievement**: Need to define when a goal is \"achieved\" —\n// probably ELO threshold + mastery percentage on goal-tagged content.\n//\n// 4. **Curriculum Registry**: For cross-curriculum composition, need a\n// registry/resolver for curriculum packages (similar to npm registry).\n//\n// 5. **Interaction with HierarchyDefinition**: Goals should work with\n// prerequisite chains — user can't skip prerequisites just because\n// they're not part of their goal.\n//\n// ## Related Files\n//\n// - `filters/userTagPreference.ts` — Preferences (path constraints)\n// - `hierarchyDefinition.ts` — Prerequisites (enforced regardless of goals)\n// - `../types/strategyState.ts` — Storage mechanism for user state\n//\n// ## Next Steps\n//\n// 1. Design goal state schema in detail\n// 2. Define goal achievement criteria\n// 3. Implement goal-scoped ELO tracking\n// 4. Build UI for goal configuration\n// 5. Design curriculum dependency resolution\n//\n// ============================================================================\n\n// Placeholder export to make this a valid module\nexport const USER_GOAL_NAVIGATOR_STUB = true;\n\n/**\n * @stub UserGoalNavigator\n *\n * A navigator that scopes learning to user-defined goals.\n * See module-level documentation for architectural intent.\n *\n * NOT IMPLEMENTED — This is a design placeholder.\n */\nexport interface UserGoalState {\n /** Tags the user wants to master (defines \"success\") */\n targetTags: string[];\n\n /** Tags the user explicitly doesn't care about */\n excludedTags: string[];\n\n /** ISO timestamp of last update */\n updatedAt: string;\n}\n","import {\n StudyContentSource,\n UserDBInterface,\n CourseDBInterface,\n StudySessionReviewItem,\n StudySessionNewItem,\n} from '..';\n\n// Re-export filter types\nexport type { CardFilter, FilterContext, CardFilterFactory } from './filters/types';\n\n// Re-export generator types\nexport type { CardGenerator, GeneratorContext, CardGeneratorFactory } from './generators/types';\n\nimport { ContentNavigationStrategyData } from '../types/contentNavigationStrategy';\nimport { ScheduledCard } from '../types/user';\nimport { logger } from '../../util/logger';\n\n// ============================================================================\n// NAVIGATION STRATEGY API\n// ============================================================================\n//\n// This module defines the ContentNavigator base class and the WeightedCard type,\n// which form the foundation of the pluggable navigation strategy system.\n//\n// KEY CONCEPTS:\n//\n// 1. WeightedCard - A card with a suitability score (0-1) and provenance trail.\n// The provenance tracks how each strategy in the pipeline contributed to\n// the card's final score, ensuring transparency and debuggability.\n//\n// 2. ContentNavigator - Abstract base class for backward compatibility.\n// New code should use CardGenerator or CardFilter interfaces directly.\n//\n// 3. CardGenerator vs CardFilter:\n// - Generators (ELO, SRS, HardcodedOrder) produce candidate cards with scores\n// - Filters (Hierarchy, Interference, Priority, EloDistance) transform scores\n//\n// 4. Pipeline architecture:\n// Pipeline(generator, [filter1, filter2, ...]) executes:\n// cards = generator.getWeightedCards()\n// cards = filter1.transform(cards, context)\n// cards = filter2.transform(cards, context)\n// return sorted(cards)\n//\n// 5. Provenance tracking - Each strategy adds an entry explaining its contribution.\n// This makes the system transparent and debuggable.\n//\n// ============================================================================\n\n/**\n * Tracks a single strategy's contribution to a card's final score.\n *\n * Each strategy in the pipeline adds a StrategyContribution entry to the\n * card's provenance array, creating an audit trail of scoring decisions.\n */\nexport interface StrategyContribution {\n /**\n * Strategy type (implementing class name).\n * Examples: 'elo', 'hierarchyDefinition', 'interferenceMitigator'\n */\n strategy: string;\n\n /**\n * Human-readable name identifying this specific strategy instance.\n * Extracted from ContentNavigationStrategyData.name.\n * Courses may have multiple instances of the same strategy type with\n * different configurations.\n *\n * Examples:\n * - \"ELO (default)\"\n * - \"Interference: b/d/p confusion\"\n * - \"Interference: phonetic confusables\"\n * - \"Priority: Common letters first\"\n */\n strategyName: string;\n\n /**\n * Unique database document ID for this strategy instance.\n * Extracted from ContentNavigationStrategyData._id.\n * Use this to fetch the full strategy configuration document.\n *\n * Examples:\n * - \"NAVIGATION_STRATEGY-ELO-default\"\n * - \"NAVIGATION_STRATEGY-interference-bdp\"\n * - \"NAVIGATION_STRATEGY-priority-common-letters\"\n */\n strategyId: string;\n\n /**\n * What the strategy did:\n * - 'generated': Strategy produced this card (generators only)\n * - 'passed': Strategy evaluated but didn't change score (transparent pass-through)\n * - 'boosted': Strategy increased the score\n * - 'penalized': Strategy decreased the score\n */\n action: 'generated' | 'passed' | 'boosted' | 'penalized';\n\n /** Score after this strategy's processing */\n score: number;\n\n /**\n * Human-readable explanation of the strategy's decision.\n *\n * Examples:\n * - \"ELO distance 75, new card\"\n * - \"Prerequisites met: letter-sounds\"\n * - \"Interferes with immature tag 'd' (decay 0.8)\"\n * - \"High-priority tag 's' (0.95) → boost 1.15x\"\n *\n * Required for transparency - silent adjusters are anti-patterns.\n */\n reason: string;\n}\n\n/**\n * A card with a suitability score and provenance trail.\n *\n * Scores range from 0-1:\n * - 1.0 = fully suitable\n * - 0.0 = hard filter (e.g., prerequisite not met)\n * - 0.5 = neutral\n * - Intermediate values = soft preference\n *\n * Provenance tracks the scoring pipeline:\n * - First entry: Generator that produced the card\n * - Subsequent entries: Filters that transformed the score\n * - Each entry includes action and human-readable reason\n */\nexport interface WeightedCard {\n cardId: string;\n courseId: string;\n /** Suitability score from 0-1 */\n score: number;\n /**\n * Audit trail of strategy contributions.\n * First entry is from the generator, subsequent entries from filters.\n */\n provenance: StrategyContribution[];\n /**\n * Pre-fetched tags. Populated by Pipeline before filters run.\n * Filters should use this instead of querying getAppliedTags() individually.\n */\n tags?: string[];\n}\n\n/**\n * Extract card origin from provenance trail.\n *\n * The first provenance entry (from the generator) indicates whether\n * this is a new card, review, or failed card. We parse the reason\n * string to extract this information.\n *\n * @param card - Card with provenance trail\n * @returns Card origin ('new', 'review', or 'failed')\n */\nexport function getCardOrigin(card: WeightedCard): 'new' | 'review' | 'failed' {\n if (card.provenance.length === 0) {\n throw new Error('Card has no provenance - cannot determine origin');\n }\n\n const firstEntry = card.provenance[0];\n const reason = firstEntry.reason.toLowerCase();\n\n if (reason.includes('failed')) {\n return 'failed';\n }\n if (reason.includes('review')) {\n return 'review';\n }\n return 'new';\n}\n\nexport enum Navigators {\n ELO = 'elo',\n SRS = 'srs',\n HARDCODED = 'hardcodedOrder',\n HIERARCHY = 'hierarchyDefinition',\n INTERFERENCE = 'interferenceMitigator',\n RELATIVE_PRIORITY = 'relativePriority',\n USER_TAG_PREFERENCE = 'userTagPreference',\n}\n\n// ============================================================================\n// NAVIGATOR ROLE CLASSIFICATION\n// ============================================================================\n//\n// Navigators are classified as either generators or filters:\n// - Generators: Produce candidate cards (ELO, SRS, HardcodedOrder)\n// - Filters: Transform/score candidates (Hierarchy, Interference, RelativePriority)\n//\n// This classification is used by PipelineAssembler to build pipelines:\n// 1. Instantiate generators (possibly into a CompositeGenerator)\n// 2. Instantiate filters\n// 3. Create Pipeline(generator, filters)\n//\n// ============================================================================\n\n/**\n * Role classification for navigation strategies.\n *\n * - GENERATOR: Produces candidate cards with initial scores\n * - FILTER: Transforms cards with score multipliers\n */\nexport enum NavigatorRole {\n GENERATOR = 'generator',\n FILTER = 'filter',\n}\n\n/**\n * Registry mapping navigator implementations to their roles.\n */\nexport const NavigatorRoles: Record<Navigators, NavigatorRole> = {\n [Navigators.ELO]: NavigatorRole.GENERATOR,\n [Navigators.SRS]: NavigatorRole.GENERATOR,\n [Navigators.HARDCODED]: NavigatorRole.GENERATOR,\n [Navigators.HIERARCHY]: NavigatorRole.FILTER,\n [Navigators.INTERFERENCE]: NavigatorRole.FILTER,\n [Navigators.RELATIVE_PRIORITY]: NavigatorRole.FILTER,\n [Navigators.USER_TAG_PREFERENCE]: NavigatorRole.FILTER,\n};\n\n/**\n * Check if a navigator implementation is a generator.\n *\n * @param impl - Navigator implementation name (e.g., 'elo', 'hierarchyDefinition')\n * @returns true if the navigator is a generator, false otherwise\n */\nexport function isGenerator(impl: string): boolean {\n return NavigatorRoles[impl as Navigators] === NavigatorRole.GENERATOR;\n}\n\n/**\n * Check if a navigator implementation is a filter.\n *\n * @param impl - Navigator implementation name (e.g., 'elo', 'hierarchyDefinition')\n * @returns true if the navigator is a filter, false otherwise\n */\nexport function isFilter(impl: string): boolean {\n return NavigatorRoles[impl as Navigators] === NavigatorRole.FILTER;\n}\n\n/**\n * Abstract base class for navigation strategies.\n *\n * This class exists primarily for backward compatibility with legacy code.\n * New code should use CardGenerator or CardFilter interfaces directly.\n *\n * The class implements StudyContentSource for compatibility with SessionController.\n * Once SessionController migrates to use getWeightedCards() exclusively,\n * the legacy methods can be removed.\n */\nexport abstract class ContentNavigator implements StudyContentSource {\n /** User interface for this navigation session */\n protected user?: UserDBInterface;\n\n /** Course interface for this navigation session */\n protected course?: CourseDBInterface;\n\n /** Human-readable name for this strategy instance (from ContentNavigationStrategyData.name) */\n protected strategyName?: string;\n\n /** Unique document ID for this strategy instance (from ContentNavigationStrategyData._id) */\n protected strategyId?: string;\n\n /**\n * Constructor for standard navigators.\n * Call this from subclass constructors to initialize common fields.\n *\n * Note: CompositeGenerator doesn't use this pattern and should call super() without args.\n */\n constructor(\n user?: UserDBInterface,\n course?: CourseDBInterface,\n strategyData?: ContentNavigationStrategyData\n ) {\n if (user && course && strategyData) {\n this.user = user;\n this.course = course;\n this.strategyName = strategyData.name;\n this.strategyId = strategyData._id;\n }\n }\n\n // ============================================================================\n // STRATEGY STATE HELPERS\n // ============================================================================\n //\n // These methods allow strategies to persist their own state (user preferences,\n // learned patterns, temporal tracking) in the user database.\n //\n // ============================================================================\n\n /**\n * Unique key identifying this strategy for state storage.\n *\n * Defaults to the constructor name (e.g., \"UserTagPreferenceFilter\").\n * Override in subclasses if multiple instances of the same strategy type\n * need separate state storage.\n */\n protected get strategyKey(): string {\n return this.constructor.name;\n }\n\n /**\n * Get this strategy's persisted state for the current course.\n *\n * @returns The strategy's data payload, or null if no state exists\n * @throws Error if user or course is not initialized\n */\n protected async getStrategyState<T>(): Promise<T | null> {\n if (!this.user || !this.course) {\n throw new Error(\n `Cannot get strategy state: navigator not properly initialized. ` +\n `Ensure user and course are provided to constructor.`\n );\n }\n return this.user.getStrategyState<T>(this.course.getCourseID(), this.strategyKey);\n }\n\n /**\n * Persist this strategy's state for the current course.\n *\n * @param data - The strategy's data payload to store\n * @throws Error if user or course is not initialized\n */\n protected async putStrategyState<T>(data: T): Promise<void> {\n if (!this.user || !this.course) {\n throw new Error(\n `Cannot put strategy state: navigator not properly initialized. ` +\n `Ensure user and course are provided to constructor.`\n );\n }\n return this.user.putStrategyState<T>(this.course.getCourseID(), this.strategyKey, data);\n }\n\n /**\n * Factory method to create navigator instances dynamically.\n *\n * @param user - User interface\n * @param course - Course interface\n * @param strategyData - Strategy configuration document\n * @returns the runtime object used to steer a study session.\n */\n static async create(\n user: UserDBInterface,\n course: CourseDBInterface,\n strategyData: ContentNavigationStrategyData\n ): Promise<ContentNavigator> {\n const implementingClass = strategyData.implementingClass;\n let NavigatorImpl;\n\n // Try different extension variations\n const variations = ['.ts', '.js', ''];\n\n for (const ext of variations) {\n try {\n const module = await import(`./${implementingClass}${ext}`);\n NavigatorImpl = module.default;\n break; // Break the loop if loading succeeds\n } catch (e) {\n // Continue to next variation if this one fails\n logger.debug(`Failed to load with extension ${ext}:`, e);\n }\n }\n\n if (!NavigatorImpl) {\n throw new Error(`Could not load navigator implementation for: ${implementingClass}`);\n }\n\n return new NavigatorImpl(user, course, strategyData);\n }\n\n /**\n * Get cards scheduled for review.\n *\n * @deprecated This method is part of the legacy StudyContentSource interface.\n * New strategies should focus on implementing CardGenerator.getWeightedCards() instead.\n */\n abstract getPendingReviews(): Promise<(StudySessionReviewItem & ScheduledCard)[]>;\n\n /**\n * Get new cards for introduction.\n *\n * @deprecated This method is part of the legacy StudyContentSource interface.\n * New strategies should focus on implementing CardGenerator.getWeightedCards() instead.\n *\n * @param n - Maximum number of new cards to return\n */\n abstract getNewCards(n?: number): Promise<StudySessionNewItem[]>;\n\n /**\n * Get cards with suitability scores and provenance trails.\n *\n * **This is the PRIMARY API for navigation strategies.**\n *\n * Returns cards ranked by suitability score (0-1). Higher scores indicate\n * better candidates for presentation. Each card includes a provenance trail\n * documenting how strategies contributed to the final score.\n *\n * ## For Generators\n * Override this method to generate candidates and compute scores based on\n * your strategy's logic (e.g., ELO proximity, review urgency). Create the\n * initial provenance entry with action='generated'.\n *\n * ## Default Implementation\n * The base class provides a backward-compatible default that:\n * 1. Calls legacy getNewCards() and getPendingReviews()\n * 2. Assigns score=1.0 to all cards\n * 3. Creates minimal provenance from legacy methods\n * 4. Returns combined results up to limit\n *\n * This allows existing strategies to work without modification while\n * new strategies can override with proper scoring and provenance.\n *\n * @param limit - Maximum cards to return\n * @returns Cards sorted by score descending, with provenance trails\n */\n async getWeightedCards(limit: number): Promise<WeightedCard[]> {\n // Default implementation: delegate to legacy methods, assign score=1.0\n const newCards = await this.getNewCards(limit);\n const reviews = await this.getPendingReviews();\n\n const weighted: WeightedCard[] = [\n ...newCards.map((c) => ({\n cardId: c.cardID,\n courseId: c.courseID,\n score: 1.0,\n provenance: [\n {\n strategy: 'legacy',\n strategyName: this.strategyName || 'Legacy API',\n strategyId: this.strategyId || 'legacy-fallback',\n action: 'generated' as const,\n score: 1.0,\n reason: 'Generated via legacy getNewCards(), new card',\n },\n ],\n })),\n ...reviews.map((r) => ({\n cardId: r.cardID,\n courseId: r.courseID,\n score: 1.0,\n provenance: [\n {\n strategy: 'legacy',\n strategyName: this.strategyName || 'Legacy API',\n strategyId: this.strategyId || 'legacy-fallback',\n action: 'generated' as const,\n score: 1.0,\n reason: 'Generated via legacy getPendingReviews(), review',\n },\n ],\n })),\n ];\n\n return weighted.slice(0, limit);\n }\n}\n","import { CourseDBInterface, CourseInfo, CoursesDBInterface, UserDBInterface } from '@db/core';\nimport { ScheduledCard } from '@db/core/types/user';\nimport {\n CourseConfig,\n CourseElo,\n DataShape,\n EloToNumber,\n Status,\n blankCourseElo,\n toCourseElo,\n} from '@vue-skuilder/common';\n\nimport { filterAllDocsByPrefix, getCourseDB, getCourseDoc, getCourseDocs } from '.';\nimport UpdateQueue from './updateQueue';\nimport {\n StudyContentSource,\n StudySessionItem,\n StudySessionNewItem,\n StudySessionReviewItem,\n} from '../../core/interfaces/contentSource';\nimport {\n CardData,\n DocType,\n QualifiedCardID,\n SkuilderCourseData,\n Tag,\n TagStub,\n DocTypePrefixes,\n} from '../../core/types/types-legacy';\nimport { logger } from '../../util/logger';\nimport { GET_CACHED } from './clientCache';\nimport { addNote55, addTagToCard, getCredentialledCourseConfig, getTagID } from './courseAPI';\nimport { DataLayerResult } from '@db/core/types/db';\nimport { PouchError } from './types';\nimport CourseLookup from './courseLookupDB';\nimport { ContentNavigationStrategyData } from '@db/core/types/contentNavigationStrategy';\nimport { ContentNavigator, Navigators, WeightedCard } from '@db/core/navigators';\nimport { Pipeline } from '@db/core/navigators/Pipeline';\nimport { PipelineAssembler } from '@db/core/navigators/PipelineAssembler';\nimport CompositeGenerator from '@db/core/navigators/CompositeGenerator';\nimport ELONavigator from '@db/core/navigators/elo';\nimport SRSNavigator from '@db/core/navigators/srs';\nimport { createEloDistanceFilter } from '@db/core/navigators/filters/eloDistance';\n\nexport class CoursesDB implements CoursesDBInterface {\n _courseIDs: string[] | undefined;\n\n constructor(courseIDs?: string[]) {\n if (courseIDs && courseIDs.length > 0) {\n this._courseIDs = courseIDs;\n } else {\n this._courseIDs = undefined;\n }\n }\n\n public async getCourseList(): Promise<CourseConfig[]> {\n let crsList = await CourseLookup.allCourseWare();\n logger.debug(`AllCourses: ${crsList.map((c) => c.name + ', ' + c._id + '\\n\\t')}`);\n if (this._courseIDs) {\n crsList = crsList.filter((c) => this._courseIDs!.includes(c._id));\n }\n\n logger.debug(`AllCourses.filtered: ${crsList.map((c) => c.name + ', ' + c._id + '\\n\\t')}`);\n\n const cfgs = await Promise.all(\n crsList.map(async (c) => {\n try {\n const cfg = await getCredentialledCourseConfig(c._id);\n logger.debug(`Found cfg: ${JSON.stringify(cfg)}`);\n return cfg;\n } catch (e) {\n logger.warn(`Error fetching cfg for course ${c.name}, ${c._id}: ${e}`);\n return undefined;\n }\n })\n );\n return cfgs.filter((c) => !!c);\n }\n\n async getCourseConfig(courseId: string): Promise<CourseConfig> {\n if (this._courseIDs && this._courseIDs.length && !this._courseIDs.includes(courseId)) {\n throw new Error(`Course ${courseId} not in course list`);\n }\n\n const cfg = await getCredentialledCourseConfig(courseId);\n if (cfg === undefined) {\n throw new Error(`Error fetching cfg for course ${courseId}`);\n } else {\n return cfg;\n }\n }\n\n public async disambiguateCourse(courseId: string, disambiguator: string): Promise<void> {\n await CourseLookup.updateDisambiguator(courseId, disambiguator);\n }\n}\n\nfunction randIntWeightedTowardZero(n: number) {\n return Math.floor(Math.random() * Math.random() * Math.random() * n);\n}\n\nexport class CourseDB implements StudyContentSource, CourseDBInterface {\n // private log(msg: string): void {\n // log(`CourseLog: ${this.id}\\n ${msg}`);\n // }\n\n private db: PouchDB.Database;\n private id: string;\n private _getCurrentUser: () => Promise<UserDBInterface>;\n private updateQueue: UpdateQueue;\n\n constructor(id: string, userLookup: () => Promise<UserDBInterface>) {\n this.id = id;\n this.db = getCourseDB(this.id);\n this._getCurrentUser = userLookup;\n this.updateQueue = new UpdateQueue(this.db);\n }\n\n public getCourseID(): string {\n return this.id;\n }\n\n public async getCourseInfo(): Promise<CourseInfo> {\n const cardCount = (\n await this.db.find({\n selector: {\n docType: DocType.CARD,\n },\n limit: 1000,\n })\n ).docs.length;\n\n return {\n cardCount,\n registeredUsers: 0,\n };\n }\n\n public async getInexperiencedCards(limit: number = 2) {\n return (\n await this.db.query('cardsByInexperience', {\n limit,\n })\n ).rows.map((r) => {\n const ret = {\n courseId: this.id,\n cardId: r.id,\n count: r.key,\n elo: r.value,\n };\n return ret;\n });\n }\n\n public async getCardsByEloLimits(\n options: {\n low: number;\n high: number;\n limit: number;\n page: number;\n } = {\n low: 0,\n high: Number.MIN_SAFE_INTEGER,\n limit: 25,\n page: 0,\n }\n ) {\n return (\n await this.db.query('elo', {\n startkey: options.low,\n endkey: options.high,\n limit: options.limit,\n skip: options.limit * options.page,\n })\n ).rows.map((r) => {\n return `${this.id}-${r.id}-${r.key}`;\n });\n }\n public async getCardEloData(id: string[]): Promise<CourseElo[]> {\n const docs = await this.db.allDocs<CardData>({\n keys: id,\n include_docs: true,\n });\n const ret: CourseElo[] = [];\n docs.rows.forEach((r) => {\n // [ ] remove these ts-ignore directives.\n if (isSuccessRow(r)) {\n if (r.doc && r.doc.elo) {\n ret.push(toCourseElo(r.doc.elo));\n } else {\n logger.warn('no elo data for card: ' + r.id);\n ret.push(blankCourseElo());\n }\n } else {\n logger.warn('no elo data for card: ' + JSON.stringify(r));\n ret.push(blankCourseElo());\n }\n });\n return ret;\n }\n\n /**\n * Returns the lowest and highest `global` ELO ratings in the course\n */\n public async getELOBounds() {\n const [low, high] = await Promise.all([\n (\n await this.db.query('elo', {\n startkey: 0,\n limit: 1,\n include_docs: false,\n })\n ).rows[0].key,\n (\n await this.db.query('elo', {\n limit: 1,\n descending: true,\n startkey: 100_000,\n })\n ).rows[0].key,\n ]);\n\n return {\n low: low,\n high: high,\n };\n }\n\n public async removeCard(id: string) {\n const doc = await this.db.get<CardData>(id);\n if (!doc.docType || !(doc.docType === DocType.CARD)) {\n throw new Error(`failed to remove ${id} from course ${this.id}. id does not point to a card`);\n }\n\n // Remove card from all associated tags before deleting the card\n try {\n const appliedTags = await this.getAppliedTags(id);\n const results = await Promise.allSettled(\n appliedTags.rows.map(async (tagRow) => {\n const tagId = tagRow.id;\n await this.removeTagFromCard(id, tagId);\n })\n );\n\n // Log any individual tag cleanup failures\n results.forEach((result, index) => {\n if (result.status === 'rejected') {\n const tagId = appliedTags.rows[index].id;\n logger.error(`Failed to remove card ${id} from tag ${tagId}: ${result.reason}`);\n }\n });\n } catch (error) {\n logger.error(`Error removing card ${id} from tags: ${error}`);\n // Continue with card deletion even if tag cleanup fails\n }\n\n return this.db.remove(doc);\n }\n\n public async getCardDisplayableDataIDs(id: string[]) {\n logger.debug(id.join(', '));\n const cards = await this.db.allDocs<CardData>({\n keys: id,\n include_docs: true,\n });\n const ret: { [card: string]: string[] } = {};\n cards.rows.forEach((r) => {\n if (isSuccessRow(r)) {\n ret[r.id] = r.doc!.id_displayable_data;\n }\n });\n\n return ret;\n }\n\n async getCardsByELO(elo: number, cardLimit?: number) {\n elo = parseInt(elo as any);\n const limit = cardLimit ? cardLimit : 25;\n\n const below: PouchDB.Query.Response<object> = await this.db.query('elo', {\n limit: Math.ceil(limit / 2),\n startkey: elo,\n descending: true,\n });\n\n const aboveLimit = limit - below.rows.length;\n\n const above: PouchDB.Query.Response<object> = await this.db.query('elo', {\n limit: aboveLimit,\n startkey: elo + 1,\n });\n // logger.log(JSON.stringify(below));\n\n let cards = below.rows;\n cards = cards.concat(above.rows);\n\n const ret = cards\n .sort((a, b) => {\n const s = Math.abs(a.key - elo) - Math.abs(b.key - elo);\n if (s === 0) {\n return Math.random() - 0.5;\n } else {\n return s;\n }\n })\n .map((c) => {\n return {\n courseID: this.id,\n cardID: c.id,\n elo: c.key,\n };\n });\n\n const str = `below:\\n${below.rows.map((r) => `\\t${r.id}-${r.key}\\n`)}\n\nabove:\\n${above.rows.map((r) => `\\t${r.id}-${r.key}\\n`)}`;\n\n logger.debug(`Getting ${limit} cards centered around elo: ${elo}:\\n\\n` + str);\n\n return ret;\n }\n\n async getCourseConfig(): Promise<CourseConfig> {\n const ret = await getCredentialledCourseConfig(this.id);\n if (ret) {\n return ret;\n } else {\n throw new Error(`Course config not found for course ID: ${this.id}`);\n }\n }\n\n async updateCourseConfig(cfg: CourseConfig): Promise<PouchDB.Core.Response> {\n logger.debug(`Updating: ${JSON.stringify(cfg)}`);\n // write both to the course DB:\n try {\n return await updateCredentialledCourseConfig(this.id, cfg);\n } catch (error) {\n logger.error(`Error updating course config in course DB: ${error}`);\n throw error;\n }\n }\n\n async updateCardElo(cardId: string, elo: CourseElo): Promise<PouchDB.Core.Response> {\n if (!elo) {\n throw new Error(`Cannot update card elo with null or undefined value for card ID: ${cardId}`);\n }\n\n try {\n const result = await this.updateQueue.update<\n CardData & PouchDB.Core.GetMeta & PouchDB.Core.IdMeta\n >(cardId, (card) => {\n logger.debug(`Replacing ${JSON.stringify(card.elo)} with ${JSON.stringify(elo)}`);\n card.elo = elo;\n return card;\n });\n return { ok: true, id: cardId, rev: result._rev };\n } catch (error) {\n logger.error(`Failed to update card elo for card ID: ${cardId}`, error);\n throw new Error(`Failed to update card elo for card ID: ${cardId}`);\n }\n }\n\n async getAppliedTags(cardId: string): Promise<PouchDB.Query.Response<TagStub>> {\n const ret = await getAppliedTags(this.id, cardId);\n if (ret) {\n return ret;\n } else {\n throw new Error(`Failed to find tags for card ${this.id}-${cardId}`);\n }\n }\n\n async getAppliedTagsBatch(cardIds: string[]): Promise<Map<string, string[]>> {\n if (cardIds.length === 0) {\n return new Map();\n }\n\n const db = getCourseDB(this.id);\n const result = await db.query<TagStub>('getTags', {\n keys: cardIds,\n include_docs: false,\n });\n\n const tagsByCard = new Map<string, string[]>();\n\n // Initialize all requested cards with empty arrays\n for (const cardId of cardIds) {\n tagsByCard.set(cardId, []);\n }\n\n // Populate from query results\n for (const row of result.rows) {\n const cardId = row.key as string;\n const tagName = row.value?.name;\n if (tagName && tagsByCard.has(cardId)) {\n tagsByCard.get(cardId)!.push(tagName);\n }\n }\n\n return tagsByCard;\n }\n\n async addTagToCard(\n cardId: string,\n tagId: string,\n updateELO?: boolean\n ): Promise<PouchDB.Core.Response> {\n return await addTagToCard(\n this.id,\n cardId,\n tagId,\n (await this._getCurrentUser()).getUsername(),\n updateELO\n );\n }\n\n async removeTagFromCard(cardId: string, tagId: string): Promise<PouchDB.Core.Response> {\n return await removeTagFromCard(this.id, cardId, tagId);\n }\n\n async createTag(name: string, author: string): Promise<PouchDB.Core.Response> {\n return await createTag(this.id, name, author);\n }\n\n async getTag(tagId: string): Promise<PouchDB.Core.GetMeta & PouchDB.Core.Document<Tag>> {\n return await getTag(this.id, tagId);\n }\n\n async updateTag(tag: Tag): Promise<PouchDB.Core.Response> {\n if (tag.course !== this.id) {\n throw new Error(`Tag ${JSON.stringify(tag)} does not belong to course ${this.id}`);\n }\n\n return await updateTag(tag);\n }\n\n async getCourseTagStubs(): Promise<PouchDB.Core.AllDocsResponse<Tag>> {\n return getCourseTagStubs(this.id);\n }\n\n async addNote(\n codeCourse: string,\n shape: DataShape,\n data: unknown,\n author: string,\n tags: string[],\n uploads?: { [key: string]: PouchDB.Core.FullAttachment },\n elo: CourseElo = blankCourseElo()\n ): Promise<DataLayerResult> {\n try {\n const resp = await addNote55(this.id, codeCourse, shape, data, author, tags, uploads, elo);\n if (resp.ok) {\n // Check if card creation failed (property added by addNote55)\n if ((resp as any).cardCreationFailed) {\n logger.warn(\n `[courseDB.addNote] Note added but card creation failed: ${\n (resp as any).cardCreationError\n }`\n );\n return {\n status: Status.error,\n message: `Note was added but no cards were created: ${(resp as any).cardCreationError}`,\n id: resp.id,\n };\n }\n return {\n status: Status.ok,\n message: '',\n id: resp.id,\n };\n } else {\n return {\n status: Status.error,\n message: 'Unexpected error adding note',\n };\n }\n } catch (e) {\n const err = e as PouchDB.Core.Error;\n logger.error(\n `[addNote] error ${err.name}\\n\\treason: ${err.reason}\\n\\tmessage: ${err.message}`\n );\n return {\n status: Status.error,\n message: `Error adding note to course. ${(e as PouchError).reason || err.message}`,\n };\n }\n }\n\n async getCourseDoc<T extends SkuilderCourseData>(\n id: string,\n options?: PouchDB.Core.GetOptions\n ): Promise<PouchDB.Core.GetMeta & PouchDB.Core.Document<T>> {\n return await getCourseDoc(this.id, id, options);\n }\n\n async getCourseDocs<T extends SkuilderCourseData>(\n ids: string[],\n options: PouchDB.Core.AllDocsOptions = {}\n ): Promise<PouchDB.Core.AllDocsWithKeysResponse<{} & T>> {\n return await getCourseDocs(this.id, ids, options);\n }\n\n ////////////////////////////////////\n // NavigationStrategyManager implementation\n ////////////////////////////////////\n\n getNavigationStrategy(id: string): Promise<ContentNavigationStrategyData> {\n logger.debug(`[courseDB] Getting navigation strategy: ${id}`);\n\n if (id == '') {\n const strategy: ContentNavigationStrategyData = {\n _id: 'NAVIGATION_STRATEGY-ELO',\n docType: DocType.NAVIGATION_STRATEGY,\n name: 'ELO',\n description: 'ELO-based navigation strategy for ordering content by difficulty',\n implementingClass: Navigators.ELO,\n course: this.id,\n serializedData: '', // serde is a noop for ELO navigator.\n };\n return Promise.resolve(strategy);\n } else {\n return this.db.get(id);\n }\n }\n\n async getAllNavigationStrategies(): Promise<ContentNavigationStrategyData[]> {\n const prefix = DocTypePrefixes[DocType.NAVIGATION_STRATEGY];\n const result = await this.db.allDocs<ContentNavigationStrategyData>({\n startkey: prefix,\n endkey: `${prefix}\\ufff0`,\n include_docs: true,\n });\n return result.rows.map((row) => row.doc!);\n }\n\n async addNavigationStrategy(data: ContentNavigationStrategyData): Promise<void> {\n logger.debug(`[courseDB] Adding navigation strategy: ${data._id}`);\n // // For now, just log the data and return success\n // logger.debug(JSON.stringify(data));\n return this.db.put(data).then(() => {});\n }\n updateNavigationStrategy(id: string, data: ContentNavigationStrategyData): Promise<void> {\n logger.debug(`[courseDB] Updating navigation strategy: ${id}`);\n // For now, just log the data and return success\n logger.debug(JSON.stringify(data));\n return Promise.resolve();\n }\n\n /**\n * Creates an instantiated navigator for this course.\n *\n * Handles multiple generators by wrapping them in CompositeGenerator.\n * This is the preferred method for getting a ready-to-use navigator.\n *\n * @param user - User database interface\n * @returns Instantiated ContentNavigator ready for use\n */\n async createNavigator(user: UserDBInterface): Promise<ContentNavigator> {\n try {\n const allStrategies = await this.getAllNavigationStrategies();\n\n if (allStrategies.length === 0) {\n // No strategies configured: use default Pipeline(Composite(ELO, SRS), [eloDistanceFilter])\n logger.debug(\n '[courseDB] No strategy documents found, using default Pipeline(Composite(ELO, SRS), [eloDistanceFilter])'\n );\n return this.createDefaultPipeline(user);\n }\n\n // Use PipelineAssembler to build a Pipeline from strategy documents\n const assembler = new PipelineAssembler();\n const { pipeline, generatorStrategies, filterStrategies, warnings } =\n await assembler.assemble({\n strategies: allStrategies,\n user,\n course: this,\n });\n\n // Log any warnings from assembly\n for (const warning of warnings) {\n logger.warn(`[PipelineAssembler] ${warning}`);\n }\n\n if (!pipeline) {\n // Assembly failed - fall back to default\n logger.debug('[courseDB] Pipeline assembly failed, using default pipeline');\n return this.createDefaultPipeline(user);\n }\n\n logger.debug(\n `[courseDB] Using assembled pipeline with ${generatorStrategies.length} generator(s) and ${filterStrategies.length} filter(s)`\n );\n return pipeline;\n } catch (e) {\n logger.error(`[courseDB] Error creating navigator: ${e}`);\n throw e;\n }\n }\n\n private makeDefaultEloStrategy(): ContentNavigationStrategyData {\n return {\n _id: 'NAVIGATION_STRATEGY-ELO-default',\n docType: DocType.NAVIGATION_STRATEGY,\n name: 'ELO (default)',\n description: 'Default ELO-based navigation strategy for new cards',\n implementingClass: Navigators.ELO,\n course: this.id,\n serializedData: '',\n };\n }\n\n private makeDefaultSrsStrategy(): ContentNavigationStrategyData {\n return {\n _id: 'NAVIGATION_STRATEGY-SRS-default',\n docType: DocType.NAVIGATION_STRATEGY,\n name: 'SRS (default)',\n description: 'Default SRS-based navigation strategy for reviews',\n implementingClass: Navigators.SRS,\n course: this.id,\n serializedData: '',\n };\n }\n\n /**\n * Creates the default navigation pipeline for courses with no configured strategies.\n *\n * Default: Pipeline(Composite(ELO, SRS), [eloDistanceFilter])\n * - ELO generator: scores new cards by skill proximity\n * - SRS generator: scores reviews by overdueness and interval recency\n * - ELO distance filter: penalizes cards far from user's current level\n */\n private createDefaultPipeline(user: UserDBInterface): Pipeline {\n const eloNavigator = new ELONavigator(user, this, this.makeDefaultEloStrategy());\n const srsNavigator = new SRSNavigator(user, this, this.makeDefaultSrsStrategy());\n\n const compositeGenerator = new CompositeGenerator([eloNavigator, srsNavigator]);\n const eloDistanceFilter = createEloDistanceFilter();\n\n return new Pipeline(compositeGenerator, [eloDistanceFilter], user, this);\n }\n\n ////////////////////////////////////\n // END NavigationStrategyManager implementation\n ////////////////////////////////////\n\n ////////////////////////////////////\n // StudyContentSource implementation\n ////////////////////////////////////\n\n public async getNewCards(limit: number = 99): Promise<StudySessionNewItem[]> {\n const u = await this._getCurrentUser();\n\n try {\n const navigator = await this.createNavigator(u);\n return navigator.getNewCards(limit);\n } catch (e) {\n logger.error(`[courseDB] Error in getNewCards: ${e}`);\n throw e;\n }\n }\n\n public async getPendingReviews(): Promise<(StudySessionReviewItem & ScheduledCard)[]> {\n const u = await this._getCurrentUser();\n\n try {\n const navigator = await this.createNavigator(u);\n return navigator.getPendingReviews();\n } catch (e) {\n logger.error(`[courseDB] Error in getPendingReviews: ${e}`);\n throw e;\n }\n }\n\n /**\n * Get cards with suitability scores for presentation.\n *\n * This is the PRIMARY API for content sources going forward. Delegates to the\n * course's configured NavigationStrategy to get scored candidates.\n *\n * @param limit - Maximum number of cards to return\n * @returns Cards sorted by score descending\n */\n public async getWeightedCards(limit: number): Promise<WeightedCard[]> {\n const u = await this._getCurrentUser();\n\n try {\n const navigator = await this.createNavigator(u);\n return navigator.getWeightedCards(limit);\n } catch (e) {\n logger.error(`[courseDB] Error getting weighted cards: ${e}`);\n throw e;\n }\n }\n\n public async getCardsCenteredAtELO(\n options: {\n limit: number;\n elo: 'user' | 'random' | number;\n } = {\n limit: 99,\n elo: 'user',\n },\n filter?: (a: QualifiedCardID) => boolean\n ): Promise<StudySessionItem[]> {\n let targetElo: number;\n\n if (options.elo === 'user') {\n const u = await this._getCurrentUser();\n\n targetElo = -1;\n try {\n const courseDoc = (await u.getCourseRegistrationsDoc()).courses.find((c) => {\n return c.courseID === this.id;\n })!;\n targetElo = EloToNumber(courseDoc.elo);\n } catch {\n targetElo = 1000;\n }\n } else if (options.elo === 'random') {\n const bounds = await GET_CACHED(`elo-bounds-${this.id}`, () => this.getELOBounds());\n targetElo = Math.round(bounds.low + Math.random() * (bounds.high - bounds.low));\n // logger.log(`Picked ${targetElo} from [${bounds.low}, ${bounds.high}]`);\n } else {\n targetElo = options.elo;\n }\n\n let cards: (QualifiedCardID & { elo?: number })[] = [];\n let mult: number = 4;\n let previousCount: number = -1;\n let newCount: number = 0;\n\n while (cards.length < options.limit && newCount !== previousCount) {\n cards = await this.getCardsByELO(targetElo, mult * options.limit);\n previousCount = newCount;\n newCount = cards.length;\n\n logger.debug(`Found ${cards.length} elo neighbor cards...`);\n\n if (filter) {\n cards = cards.filter(filter);\n logger.debug(`Filtered to ${cards.length} cards...`);\n }\n\n mult *= 2;\n }\n\n const selectedCards: {\n courseID: string;\n cardID: string;\n elo?: number;\n }[] = [];\n\n while (selectedCards.length < options.limit && cards.length > 0) {\n const index = randIntWeightedTowardZero(cards.length);\n const card = cards.splice(index, 1)[0];\n selectedCards.push(card);\n }\n\n return selectedCards.map((c) => {\n return {\n courseID: this.id,\n cardID: c.cardID,\n contentSourceType: 'course',\n contentSourceID: this.id,\n elo: c.elo,\n status: 'new',\n };\n });\n }\n\n // Admin search methods\n public async searchCards(query: string): Promise<any[]> {\n logger.log(`[CourseDB ${this.id}] Searching for: \"${query}\"`);\n\n // Try multiple search approaches\n let displayableData;\n\n try {\n // Try regex search on the correct data structure: data[0].data\n displayableData = await this.db.find({\n selector: {\n docType: 'DISPLAYABLE_DATA',\n 'data.0.data': { $regex: `.*${query}.*` },\n },\n });\n logger.log(`[CourseDB ${this.id}] Regex search on data[0].data successful`);\n } catch (regexError) {\n logger.log(\n `[CourseDB ${this.id}] Regex search failed, falling back to manual search:`,\n regexError\n );\n\n // Fallback: get all displayable data and filter manually\n const allDisplayable = await this.db.find({\n selector: {\n docType: 'DISPLAYABLE_DATA',\n },\n });\n\n logger.log(\n `[CourseDB ${this.id}] Retrieved ${allDisplayable.docs.length} documents for manual filtering`\n );\n\n displayableData = {\n docs: allDisplayable.docs.filter((doc) => {\n // Search entire document as JSON string - inclusive approach for admin tool\n const docString = JSON.stringify(doc).toLowerCase();\n const match = docString.includes(query.toLowerCase());\n if (match) {\n logger.log(`[CourseDB ${this.id}] Manual match found in document: ${doc._id}`);\n }\n return match;\n }),\n };\n }\n\n logger.log(\n `[CourseDB ${this.id}] Found ${displayableData.docs.length} displayable data documents`\n );\n\n if (displayableData.docs.length === 0) {\n // Debug: Let's see what displayable data exists\n const allDisplayableData = await this.db.find({\n selector: {\n docType: 'DISPLAYABLE_DATA',\n },\n limit: 5, // Just sample a few\n });\n\n logger.log(\n `[CourseDB ${this.id}] Sample displayable data:`,\n allDisplayableData.docs.map((d) => ({\n id: d._id,\n docType: (d as any).docType,\n dataStructure: (d as any).data ? Object.keys((d as any).data) : 'no data field',\n dataContent: (d as any).data,\n fullDoc: d,\n }))\n );\n }\n\n const allResults: any[] = [];\n\n for (const dd of displayableData.docs) {\n const cards = await this.db.find({\n selector: {\n docType: 'CARD',\n id_displayable_data: { $in: [dd._id] },\n },\n });\n\n logger.log(\n `[CourseDB ${this.id}] Displayable data ${dd._id} linked to ${cards.docs.length} cards`\n );\n allResults.push(...cards.docs);\n }\n\n logger.log(`[CourseDB ${this.id}] Total cards found: ${allResults.length}`);\n return allResults;\n }\n\n public async find(\n request: PouchDB.Find.FindRequest<any>\n ): Promise<PouchDB.Find.FindResponse<any>> {\n return this.db.find(request);\n }\n}\n\n/**\n * Returns a list of registered datashapes for the specified\n * course.\n * @param courseID The ID of the course\n */\nexport async function getCourseDataShapes(courseID: string) {\n const cfg = await getCredentialledCourseConfig(courseID);\n return cfg!.dataShapes;\n}\n\nexport async function getCredentialledDataShapes(courseID: string) {\n const cfg = await getCredentialledCourseConfig(courseID);\n\n return cfg.dataShapes;\n}\n\nexport async function getCourseQuestionTypes(courseID: string) {\n const cfg = await getCredentialledCourseConfig(courseID);\n return cfg!.questionTypes;\n}\n\n// todo: this is actually returning full tag docs now.\n// - performance issue when tags have lots of\n// applied docs\n// - will require a computed couch DB view\nexport async function getCourseTagStubs(\n courseID: string\n): Promise<PouchDB.Core.AllDocsResponse<Tag>> {\n logger.debug(`Getting tag stubs for course: ${courseID}`);\n const stubs = await filterAllDocsByPrefix<Tag>(\n getCourseDB(courseID),\n DocType.TAG.valueOf() + '-'\n );\n\n stubs.rows.forEach((row) => {\n logger.debug(`\\tTag stub for doc: ${row.id}`);\n });\n\n return stubs;\n}\n\nexport async function deleteTag(courseID: string, tagName: string) {\n tagName = getTagID(tagName);\n const courseDB = getCourseDB(courseID);\n const doc = await courseDB.get<Tag>(DocType.TAG.valueOf() + '-' + tagName);\n const resp = await courseDB.remove(doc);\n return resp;\n}\n\nexport async function createTag(courseID: string, tagName: string, author: string) {\n logger.debug(`Creating tag: ${tagName}...`);\n const tagID = getTagID(tagName);\n const courseDB = getCourseDB(courseID);\n const resp = await courseDB.put<Tag>({\n course: courseID,\n docType: DocType.TAG,\n name: tagName,\n snippet: '',\n taggedCards: [],\n wiki: '',\n author,\n _id: tagID,\n });\n return resp;\n}\n\nexport async function updateTag(tag: Tag) {\n const prior = await getTag(tag.course, tag.name);\n return await getCourseDB(tag.course).put<Tag>({\n ...tag,\n _rev: prior._rev,\n });\n}\n\nexport async function getTag(courseID: string, tagName: string) {\n const tagID = getTagID(tagName);\n const courseDB = getCourseDB(courseID);\n return courseDB.get<Tag>(tagID);\n}\n\nexport async function removeTagFromCard(courseID: string, cardID: string, tagID: string) {\n // todo: possible future perf. hit if tags have large #s of taggedCards.\n // In this case, should be converted to a server-request\n tagID = getTagID(tagID);\n const courseDB = getCourseDB(courseID);\n const tag = await courseDB.get<Tag>(tagID);\n tag.taggedCards = tag.taggedCards.filter((taggedID) => {\n return cardID !== taggedID;\n });\n return courseDB.put<Tag>(tag);\n}\n\n/**\n * Returns an array of ancestor tag IDs, where:\n * return[0] = parent,\n * return[1] = grandparent,\n * return[2] = great grandparent,\n * etc.\n *\n * If ret is empty, the tag itself is a root\n */\nexport function getAncestorTagIDs(courseID: string, tagID: string): string[] {\n tagID = getTagID(tagID);\n const split = tagID.split('>');\n if (split.length === 1) {\n return [];\n } else {\n split.pop();\n const parent = split.join('>');\n return [parent].concat(getAncestorTagIDs(courseID, parent));\n }\n}\n\nexport async function getChildTagStubs(courseID: string, tagID: string) {\n return await filterAllDocsByPrefix(getCourseDB(courseID), tagID + '>');\n}\n\nexport async function getAppliedTags(id_course: string, id_card: string) {\n const db = getCourseDB(id_course);\n\n const result = await db.query<TagStub>('getTags', {\n startkey: id_card,\n endkey: id_card,\n // include_docs: true\n });\n\n // log(`getAppliedTags looked up: ${id_card}`);\n // log(`getAppliedTags returning: ${JSON.stringify(result)}`);\n\n return result;\n}\n\nexport async function updateCardElo(courseID: string, cardID: string, elo: CourseElo) {\n if (elo) {\n // checking against null, undefined, NaN\n const cDB = getCourseDB(courseID);\n const card = await cDB.get<CardData>(cardID);\n logger.debug(`Replacing ${JSON.stringify(card.elo)} with ${JSON.stringify(elo)}`);\n card.elo = elo;\n return cDB.put(card); // race conditions - is it important? probably not (net-zero effect)\n }\n}\n\nexport async function updateCredentialledCourseConfig(courseID: string, config: CourseConfig) {\n logger.debug(`Updating course config:\n\n${JSON.stringify(config)}\n`);\n\n const db = getCourseDB(courseID);\n const old = await getCredentialledCourseConfig(courseID);\n\n return await db.put<CourseConfig>({\n ...config,\n _rev: (old as any)._rev,\n });\n}\n\nfunction isSuccessRow<T>(\n row:\n | {\n key: PouchDB.Core.DocumentKey;\n error: 'not_found';\n }\n | {\n doc?: PouchDB.Core.ExistingDocument<PouchDB.Core.AllDocsMeta & T> | null | undefined;\n id: PouchDB.Core.DocumentId;\n key: PouchDB.Core.DocumentKey;\n value: {\n rev: PouchDB.Core.RevisionId;\n deleted?: boolean | undefined;\n };\n }\n): row is {\n doc?: PouchDB.Core.ExistingDocument<PouchDB.Core.AllDocsMeta & T> | null | undefined;\n id: PouchDB.Core.DocumentId;\n key: PouchDB.Core.DocumentKey;\n value: {\n rev: PouchDB.Core.RevisionId;\n deleted?: boolean | undefined;\n };\n} {\n return 'doc' in row && row.doc !== null && row.doc !== undefined;\n}\n","import {\n StudyContentSource,\n StudySessionNewItem,\n StudySessionReviewItem,\n} from '@db/core/interfaces/contentSource';\nimport { WeightedCard } from '@db/core/navigators';\nimport { ClassroomConfig } from '@vue-skuilder/common';\nimport { ENV } from '@db/factory';\nimport { logger } from '@db/util/logger';\nimport moment from 'moment';\nimport pouch from './pouchdb-setup';\nimport { getCourseDB, getStartAndEndKeys, createPouchDBConfig, REVIEW_TIME_FORMAT } from '.';\nimport { CourseDB, getTag } from './courseDB';\n\nimport { UserDBInterface } from '@db/core';\nimport {\n AssignedContent,\n AssignedCourse,\n AssignedTag,\n StudentClassroomDBInterface,\n TeacherClassroomDBInterface,\n} from '@db/core/interfaces/classroomDB';\nimport { ScheduledCard } from '@db/core/types/user';\n\nconst classroomLookupDBTitle = 'classdb-lookup';\nexport const CLASSROOM_CONFIG = 'ClassroomConfig';\n\nexport type ClassroomMessage = object;\n\nabstract class ClassroomDBBase {\n public _id!: string;\n protected _db!: PouchDB.Database;\n protected _cfg!: ClassroomConfig;\n protected _initComplete: boolean = false;\n\n protected readonly _content_prefix: string = 'content';\n protected get _content_searchkeys() {\n return getStartAndEndKeys(this._content_prefix);\n }\n\n protected abstract init(): Promise<void>;\n\n public async getAssignedContent(): Promise<AssignedContent[]> {\n logger.info(`Getting assigned content...`);\n // see couchdb docs 6.2.2:\n // Guide to Views -> Views Collation -> String Ranges\n const docRows = await this._db.allDocs<AssignedContent>({\n startkey: this._content_prefix,\n endkey: this._content_prefix + `\\ufff0`,\n include_docs: true,\n });\n\n const ret = docRows.rows.map((row) => {\n return row.doc!;\n });\n // logger.info(`Assigned content: ${JSON.stringify(ret)}`);\n\n return ret;\n }\n\n protected getContentId(content: AssignedContent): string {\n if (content.type === 'tag') {\n return `${this._content_prefix}-${content.courseID}-${content.tagID}`;\n } else {\n return `${this._content_prefix}-${content.courseID}`;\n }\n }\n\n public get ready(): boolean {\n return this._initComplete;\n }\n public getConfig(): ClassroomConfig {\n return this._cfg;\n }\n}\n\nexport class StudentClassroomDB\n extends ClassroomDBBase\n implements StudyContentSource, StudentClassroomDBInterface\n{\n // private readonly _prefix: string = 'content';\n private userMessages!: PouchDB.Core.Changes<object>;\n private _user: UserDBInterface;\n\n private constructor(classID: string, user: UserDBInterface) {\n super();\n this._id = classID;\n this._user = user;\n // init() is called explicitly in factory method, not in constructor\n }\n\n async init(): Promise<void> {\n const dbName = `classdb-student-${this._id}`;\n this._db = new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,\n createPouchDBConfig()\n );\n try {\n const cfg = await this._db.get<ClassroomConfig>(CLASSROOM_CONFIG);\n this._cfg = cfg;\n this.userMessages = this._db.changes({\n since: 'now',\n live: true,\n include_docs: true,\n });\n this._initComplete = true;\n return;\n } catch (e) {\n throw new Error(`Error in StudentClassroomDB constructor: ${JSON.stringify(e)}`);\n }\n }\n\n public static async factory(classID: string, user: UserDBInterface): Promise<StudentClassroomDB> {\n const ret = new StudentClassroomDB(classID, user);\n await ret.init();\n return ret;\n }\n\n public setChangeFcn(f: (value: unknown) => object): void {\n // todo: make this into a view request, w/ the user's name attached\n // todo: requires creating the view doc on classroom create in /express\n void this.userMessages.on('change', f);\n }\n\n public async getPendingReviews(): Promise<(StudySessionReviewItem & ScheduledCard)[]> {\n const u = this._user;\n return (await u.getPendingReviews())\n .filter((r) => r.scheduledFor === 'classroom' && r.schedulingAgentId === this._id)\n .map((r) => {\n return {\n ...r,\n qualifiedID: `${r.courseId}-${r.cardId}`,\n courseID: r.courseId,\n cardID: r.cardId,\n contentSourceType: 'classroom',\n contentSourceID: this._id,\n reviewID: r._id,\n status: 'review',\n };\n });\n }\n\n public async getNewCards(): Promise<StudySessionNewItem[]> {\n const activeCards = await this._user.getActiveCards();\n const now = moment.utc();\n const assigned = await this.getAssignedContent();\n const due = assigned.filter((c) => now.isAfter(moment.utc(c.activeOn, REVIEW_TIME_FORMAT)));\n\n logger.info(`Due content: ${JSON.stringify(due)}`);\n\n let ret: StudySessionNewItem[] = [];\n\n for (let i = 0; i < due.length; i++) {\n const content = due[i];\n\n if (content.type === 'course') {\n const db = new CourseDB(content.courseID, async () => this._user);\n ret = ret.concat(await db.getNewCards());\n } else if (content.type === 'tag') {\n const tagDoc = await getTag(content.courseID, content.tagID);\n\n ret = ret.concat(\n tagDoc.taggedCards.map((c) => {\n return {\n courseID: content.courseID,\n cardID: c,\n qualifiedID: `${content.courseID}-${c}`,\n contentSourceType: 'classroom',\n contentSourceID: this._id,\n status: 'new',\n };\n })\n );\n } else if (content.type === 'card') {\n // returning card docs - not IDs\n ret.push(await getCourseDB(content.courseID).get(content.cardID));\n }\n }\n\n logger.info(\n `New Cards from classroom ${this._cfg.name}: ${ret.map((c) => `${c.courseID}-${c.cardID}`)}`\n );\n\n return ret.filter((c) => {\n if (activeCards.some((ac) => c.cardID === ac.cardID)) {\n // [ ] almost certainly broken after removing qualifiedID from StudySessionItem\n return false;\n } else {\n return true;\n }\n });\n }\n\n /**\n * Get cards with suitability scores for presentation.\n *\n * This implementation wraps the legacy getNewCards/getPendingReviews methods,\n * assigning score=1.0 to all cards. StudentClassroomDB does not currently\n * support pluggable navigation strategies.\n *\n * @param limit - Maximum number of cards to return\n * @returns Cards sorted by score descending (all scores = 1.0)\n */\n public async getWeightedCards(limit: number): Promise<WeightedCard[]> {\n const [newCards, reviews] = await Promise.all([this.getNewCards(), this.getPendingReviews()]);\n\n const weighted: WeightedCard[] = [\n ...newCards.map((c) => ({\n cardId: c.cardID,\n courseId: c.courseID,\n score: 1.0,\n provenance: [\n {\n strategy: 'classroom',\n strategyName: 'Classroom',\n strategyId: 'CLASSROOM',\n action: 'generated' as const,\n score: 1.0,\n reason: 'Classroom legacy getNewCards(), new card',\n },\n ],\n })),\n ...reviews.map((r) => ({\n cardId: r.cardID,\n courseId: r.courseID,\n score: 1.0,\n provenance: [\n {\n strategy: 'classroom',\n strategyName: 'Classroom',\n strategyId: 'CLASSROOM',\n action: 'generated' as const,\n score: 1.0,\n reason: 'Classroom legacy getPendingReviews(), review',\n },\n ],\n })),\n ];\n\n // Sort by score descending (all 1.0 in this case) and limit\n return weighted.slice(0, limit);\n }\n}\n\n/**\n * Interface for managing a classroom.\n */\nexport class TeacherClassroomDB extends ClassroomDBBase implements TeacherClassroomDBInterface {\n private _stuDb!: PouchDB.Database;\n\n private constructor(classID: string) {\n super();\n this._id = classID;\n }\n\n async init(): Promise<void> {\n const dbName = `classdb-teacher-${this._id}`;\n const stuDbName = `classdb-student-${this._id}`;\n this._db = new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,\n createPouchDBConfig()\n );\n this._stuDb = new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + stuDbName,\n createPouchDBConfig()\n );\n try {\n return this._db\n .get<ClassroomConfig>(CLASSROOM_CONFIG)\n .then((cfg) => {\n this._cfg = cfg;\n this._initComplete = true;\n })\n .then(() => {\n return;\n });\n } catch (e) {\n throw new Error(`Error in TeacherClassroomDB constructor: ${JSON.stringify(e)}`);\n }\n }\n\n public static async factory(classID: string): Promise<TeacherClassroomDB> {\n const ret = new TeacherClassroomDB(classID);\n await ret.init();\n return ret;\n }\n\n public async removeContent(content: AssignedContent): Promise<void> {\n const contentID = this.getContentId(content);\n\n try {\n const doc = await this._db.get(contentID);\n await this._db.remove(doc);\n void this._db.replicate.to(this._stuDb, {\n doc_ids: [contentID],\n });\n } catch (error) {\n logger.error('Failed to remove content:', contentID, error);\n }\n }\n\n public async assignContent(content: AssignedContent): Promise<boolean> {\n let put: PouchDB.Core.Response;\n const id: string = this.getContentId(content);\n\n if (content.type === 'tag') {\n put = await this._db.put<AssignedTag>({\n courseID: content.courseID,\n tagID: content.tagID,\n type: 'tag',\n _id: id,\n assignedBy: content.assignedBy,\n assignedOn: moment.utc(),\n activeOn: content.activeOn || moment.utc(),\n });\n } else {\n put = await this._db.put<AssignedCourse>({\n courseID: content.courseID,\n type: 'course',\n _id: id,\n assignedBy: content.assignedBy,\n assignedOn: moment.utc(),\n activeOn: content.activeOn || moment.utc(),\n });\n }\n\n if (put.ok) {\n void this._db.replicate.to(this._stuDb, {\n doc_ids: [id],\n });\n return true;\n } else {\n return false;\n }\n }\n}\n\nexport const ClassroomLookupDB: () => PouchDB.Database = () =>\n new pouch(ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + classroomLookupDBTitle, {\n skip_setup: true,\n });\n\nexport function getClassroomDB(classID: string, version: 'student' | 'teacher'): PouchDB.Database {\n const dbName = `classdb-${version}-${classID}`;\n logger.info(`Retrieving classroom db: ${dbName}`);\n\n return new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,\n createPouchDBConfig()\n );\n}\n\nexport async function getClassroomConfig(classID: string): Promise<ClassroomConfig> {\n return await getClassroomDB(classID, 'student').get<ClassroomConfig>(CLASSROOM_CONFIG);\n}\n","import {\n StudyContentSource,\n StudySessionNewItem,\n StudySessionReviewItem,\n} from '@db/core/interfaces/contentSource';\nimport { WeightedCard } from '@db/core/navigators';\nimport { UserDBInterface } from '@db/core';\nimport { ScheduledCard } from '@db/core/types/user';\nimport { TagFilter, hasActiveFilter } from '@vue-skuilder/common';\nimport { getTag } from '../impl/couch/courseDB';\nimport { logger } from '@db/util/logger';\n\n/**\n * A StudyContentSource that filters cards based on tag inclusion/exclusion.\n *\n * This enables ephemeral, tag-scoped study sessions where users can focus\n * on specific topics within a course without permanent configuration.\n *\n * Filter logic:\n * - If `include` is non-empty: card must have at least one of the included tags\n * - If `exclude` is non-empty: card must not have any of the excluded tags\n * - Both filters are applied (include first, then exclude)\n */\nexport class TagFilteredContentSource implements StudyContentSource {\n private courseId: string;\n private filter: TagFilter;\n private user: UserDBInterface;\n\n // Cache resolved card IDs to avoid repeated lookups within a session\n private resolvedCardIds: Set<string> | null = null;\n\n constructor(courseId: string, filter: TagFilter, user: UserDBInterface) {\n this.courseId = courseId;\n this.filter = filter;\n this.user = user;\n\n logger.info(\n `[TagFilteredContentSource] Created for course \"${courseId}\" with filter:`,\n JSON.stringify(filter)\n );\n }\n\n /**\n * Resolves the TagFilter to a set of eligible card IDs.\n *\n * - Cards in `include` tags are OR'd together (card needs at least one)\n * - Cards in `exclude` tags are removed from the result\n */\n private async resolveFilteredCardIds(): Promise<Set<string>> {\n // Return cached result if available\n if (this.resolvedCardIds !== null) {\n return this.resolvedCardIds;\n }\n\n const includedCardIds = new Set<string>();\n\n // Build inclusion set (OR of all include tags)\n if (this.filter.include.length > 0) {\n for (const tagName of this.filter.include) {\n try {\n const tagDoc = await getTag(this.courseId, tagName);\n tagDoc.taggedCards.forEach((cardId) => includedCardIds.add(cardId));\n } catch (error) {\n logger.warn(\n `[TagFilteredContentSource] Could not resolve tag \"${tagName}\" for inclusion:`,\n error\n );\n }\n }\n }\n\n // If no include tags specified or none resolved, return empty set\n // (requiring explicit inclusion prevents \"study everything\" on empty filter)\n if (includedCardIds.size === 0 && this.filter.include.length > 0) {\n logger.warn(\n `[TagFilteredContentSource] No cards found for include tags: ${this.filter.include.join(', ')}`\n );\n this.resolvedCardIds = new Set();\n return this.resolvedCardIds;\n }\n\n // Build exclusion set\n const excludedCardIds = new Set<string>();\n if (this.filter.exclude.length > 0) {\n for (const tagName of this.filter.exclude) {\n try {\n const tagDoc = await getTag(this.courseId, tagName);\n tagDoc.taggedCards.forEach((cardId) => excludedCardIds.add(cardId));\n } catch (error) {\n logger.warn(\n `[TagFilteredContentSource] Could not resolve tag \"${tagName}\" for exclusion:`,\n error\n );\n }\n }\n }\n\n // Apply exclusion filter\n const finalCardIds = new Set<string>();\n for (const cardId of includedCardIds) {\n if (!excludedCardIds.has(cardId)) {\n finalCardIds.add(cardId);\n }\n }\n\n logger.info(\n `[TagFilteredContentSource] Resolved ${finalCardIds.size} cards ` +\n `(included: ${includedCardIds.size}, excluded: ${excludedCardIds.size})`\n );\n\n this.resolvedCardIds = finalCardIds;\n return finalCardIds;\n }\n\n /**\n * Gets new cards that match the tag filter and are not already active for the user.\n */\n public async getNewCards(limit?: number): Promise<StudySessionNewItem[]> {\n if (!hasActiveFilter(this.filter)) {\n logger.warn('[TagFilteredContentSource] getNewCards called with no active filter');\n return [];\n }\n\n const eligibleCardIds = await this.resolveFilteredCardIds();\n const activeCards = await this.user.getActiveCards();\n const activeCardIds = new Set(activeCards.map((c) => c.cardID));\n\n const newItems: StudySessionNewItem[] = [];\n for (const cardId of eligibleCardIds) {\n if (!activeCardIds.has(cardId)) {\n newItems.push({\n courseID: this.courseId,\n cardID: cardId,\n contentSourceType: 'course',\n contentSourceID: this.courseId,\n status: 'new',\n });\n }\n\n if (limit !== undefined && newItems.length >= limit) {\n break;\n }\n }\n\n logger.info(`[TagFilteredContentSource] Found ${newItems.length} new cards matching filter`);\n return newItems;\n }\n\n /**\n * Gets pending reviews, filtered to only include cards that match the tag filter.\n */\n public async getPendingReviews(): Promise<(StudySessionReviewItem & ScheduledCard)[]> {\n if (!hasActiveFilter(this.filter)) {\n logger.warn('[TagFilteredContentSource] getPendingReviews called with no active filter');\n return [];\n }\n\n const eligibleCardIds = await this.resolveFilteredCardIds();\n const allReviews = await this.user.getPendingReviews(this.courseId);\n\n const filteredReviews = allReviews.filter((review) => {\n return eligibleCardIds.has(review.cardId);\n });\n\n logger.info(\n `[TagFilteredContentSource] Found ${filteredReviews.length} pending reviews matching filter ` +\n `(of ${allReviews.length} total)`\n );\n\n return filteredReviews.map((r) => ({\n ...r,\n courseID: r.courseId,\n cardID: r.cardId,\n contentSourceType: 'course' as const,\n contentSourceID: this.courseId,\n reviewID: r._id,\n status: 'review' as const,\n }));\n }\n\n /**\n * Get cards with suitability scores for presentation.\n *\n * This implementation wraps the legacy getNewCards/getPendingReviews methods,\n * assigning score=1.0 to all cards. TagFilteredContentSource does not currently\n * support pluggable navigation strategies - it returns flat-scored candidates.\n *\n * @param limit - Maximum number of cards to return\n * @returns Cards sorted by score descending (all scores = 1.0)\n */\n public async getWeightedCards(limit: number): Promise<WeightedCard[]> {\n const [newCards, reviews] = await Promise.all([\n this.getNewCards(limit),\n this.getPendingReviews(),\n ]);\n\n const weighted: WeightedCard[] = [\n ...reviews.map((r) => ({\n cardId: r.cardID,\n courseId: r.courseID,\n score: 1.0,\n provenance: [\n {\n strategy: 'tagFilter',\n strategyName: 'Tag Filter',\n strategyId: 'TAG_FILTER',\n action: 'generated' as const,\n score: 1.0,\n reason: `Tag-filtered review (tags: ${this.filter.include.join(', ')})`,\n },\n ],\n })),\n ...newCards.map((c) => ({\n cardId: c.cardID,\n courseId: c.courseID,\n score: 1.0,\n provenance: [\n {\n strategy: 'tagFilter',\n strategyName: 'Tag Filter',\n strategyId: 'TAG_FILTER',\n action: 'generated' as const,\n score: 1.0,\n reason: `Tag-filtered new card (tags: ${this.filter.include.join(', ')})`,\n },\n ],\n })),\n ];\n\n // Reviews first, then new cards; respect limit\n return weighted.slice(0, limit);\n }\n\n /**\n * Clears the cached resolved card IDs.\n * Call this if the underlying tag data may have changed during a session.\n */\n public clearCache(): void {\n this.resolvedCardIds = null;\n }\n\n /**\n * Returns the course ID this source is filtering.\n */\n public getCourseId(): string {\n return this.courseId;\n }\n\n /**\n * Returns the active tag filter.\n */\n public getFilter(): TagFilter {\n return this.filter;\n }\n}\n","import { getDataLayer } from '@db/factory';\nimport { UserDBInterface } from '..';\nimport { StudentClassroomDB } from '../../impl/couch/classroomDB';\nimport { ScheduledCard } from '@db/core/types/user';\nimport { WeightedCard } from '../navigators';\nimport { TagFilter, hasActiveFilter } from '@vue-skuilder/common';\nimport { TagFilteredContentSource } from '../../study/TagFilteredContentSource';\n\n// ============================================================================\n// API MIGRATION NOTICE\n// ============================================================================\n//\n// The StudyContentSource interface is being superseded by the ContentNavigator\n// class and its getWeightedCards() API. See:\n// packages/db/src/core/navigators/ARCHITECTURE.md\n//\n// HISTORICAL CONTEXT:\n// - This interface was designed to abstract 'classrooms' and 'courses' as\n// content sources for study sessions.\n// - getNewCards() and getPendingReviews() were artifacts of two hard-coded\n// navigation strategies: ELO proximity (new) and SRS scheduling (reviews).\n// - The new/review split reflected implementation details, not fundamentals.\n//\n// THE PROBLEM:\n// - \"What does 'get reviews' mean for an interference mitigator?\" - it doesn't.\n// - SRS is just one strategy that could express review urgency as scores.\n// - Some strategies generate candidates, others filter/score them.\n//\n// THE SOLUTION:\n// - ContentNavigator.getWeightedCards() returns unified scored candidates.\n// - WeightedCard.source field distinguishes new/review/failed (metadata, not API).\n// - Strategies compose via delegate pattern (filter wraps generator).\n//\n// MIGRATION PATH:\n// 1. ContentNavigator implements StudyContentSource for backward compat\n// 2. SessionController will migrate to call getWeightedCards()\n// 3. Legacy methods will be deprecated, then removed\n//\n// ============================================================================\n\nexport type StudySessionFailedItem = StudySessionFailedNewItem | StudySessionFailedReviewItem;\n\nexport interface StudySessionFailedNewItem extends StudySessionItem {\n status: 'failed-new';\n}\nexport interface StudySessionFailedReviewItem extends StudySessionReviewItem {\n status: 'failed-review';\n}\n\nexport interface StudySessionNewItem extends StudySessionItem {\n status: 'new';\n}\n\nexport interface StudySessionReviewItem extends StudySessionItem {\n reviewID: string;\n status: 'review' | 'failed-review';\n}\nexport function isReview(item: StudySessionItem): item is StudySessionReviewItem {\n const ret = item.status === 'review' || item.status === 'failed-review' || 'reviewID' in item;\n\n // console.log(`itemIsReview: ${ret}\n // \\t${JSON.stringify(item)}`);\n\n return ret;\n}\n\nexport interface StudySessionItem {\n status: 'new' | 'review' | 'failed-new' | 'failed-review';\n contentSourceType: 'course' | 'classroom';\n contentSourceID: string;\n // qualifiedID: `${string}-${string}` | `${string}-${string}-${number}`;\n cardID: string;\n courseID: string;\n elo?: number;\n // reviewID?: string;\n}\n\nexport interface ContentSourceID {\n type: 'course' | 'classroom';\n id: string;\n /**\n * Optional tag filter for scoped study sessions.\n * When present, creates a TagFilteredContentSource instead of a regular course source.\n */\n tagFilter?: TagFilter;\n}\n\n// #region docs_StudyContentSource\n/**\n * Interface for sources that provide study content to SessionController.\n *\n * @deprecated This interface will be superseded by ContentNavigator.getWeightedCards().\n * The getNewCards/getPendingReviews split was an artifact of hard-coded ELO and SRS\n * strategies. The new API returns unified WeightedCard[] with scores.\n *\n * MIGRATION:\n * - Implement ContentNavigator instead of StudyContentSource directly\n * - Override getWeightedCards() as the primary method\n * - Legacy methods can delegate to getWeightedCards() or be left as-is\n *\n * See: packages/db/src/core/navigators/ARCHITECTURE.md\n */\nexport interface StudyContentSource {\n /**\n * Get cards scheduled for review based on SRS algorithm.\n *\n * @deprecated Will be replaced by getWeightedCards() which returns scored candidates.\n * Review urgency will be expressed as a score rather than a separate method.\n */\n getPendingReviews(): Promise<(StudySessionReviewItem & ScheduledCard)[]>;\n\n /**\n * Get new cards for introduction, typically ordered by ELO proximity.\n *\n * @deprecated Will be replaced by getWeightedCards() which returns scored candidates.\n * New card selection and scoring will be unified with review scoring.\n *\n * @param n - Maximum number of new cards to return\n */\n getNewCards(n?: number): Promise<StudySessionNewItem[]>;\n\n /**\n * Get cards with suitability scores for presentation.\n *\n * This is the PRIMARY API for content sources going forward. Returns unified\n * scored candidates that can be sorted and selected by SessionController.\n *\n * The `source` field on WeightedCard indicates origin ('new' | 'review' | 'failed')\n * for queue routing purposes during the migration period.\n *\n * @param limit - Maximum number of cards to return\n * @returns Cards sorted by score descending\n */\n getWeightedCards?(limit: number): Promise<WeightedCard[]>;\n}\n// #endregion docs_StudyContentSource\n\nexport async function getStudySource(\n source: ContentSourceID,\n user: UserDBInterface\n): Promise<StudyContentSource> {\n if (source.type === 'classroom') {\n return await StudentClassroomDB.factory(source.id, user);\n } else {\n // Check if this is a tag-filtered course source\n if (hasActiveFilter(source.tagFilter)) {\n return new TagFilteredContentSource(source.id, source.tagFilter!, user);\n }\n\n // Regular course source\n return getDataLayer().getCourseDB(source.id) as unknown as StudyContentSource;\n }\n}\n","import { CourseConfig, CourseElo, DataShape, SkuilderCourseData } from '@vue-skuilder/common';\nimport { StudySessionNewItem, StudySessionItem } from './contentSource';\nimport { TagStub, Tag, QualifiedCardID } from '../types/types-legacy';\nimport { DataLayerResult } from '../types/db';\nimport { NavigationStrategyManager } from './navigationStrategyManager';\n\n/**\n * Course content and management\n */\nexport interface CoursesDBInterface {\n /**\n * Get course config\n */\n getCourseConfig(courseId: string): Promise<CourseConfig>;\n\n /**\n * Get a list of all courses\n */\n getCourseList(): Promise<CourseConfig[]>;\n\n disambiguateCourse(courseId: string, disambiguator: string): Promise<void>;\n}\n\nexport interface CourseInfo {\n cardCount: number;\n registeredUsers: number;\n}\n\nexport interface CourseDBInterface extends NavigationStrategyManager {\n /**\n * Get course config\n */\n getCourseConfig(): Promise<CourseConfig>;\n\n getCourseID(): string;\n\n /**\n * Set course config\n */\n updateCourseConfig(cfg: CourseConfig): Promise<PouchDB.Core.Response>;\n\n getCourseInfo(): Promise<CourseInfo>;\n\n getCourseDoc<T extends SkuilderCourseData>(\n id: string,\n options?: PouchDB.Core.GetOptions\n ): Promise<T>;\n getCourseDocs<T extends SkuilderCourseData>(\n ids: string[],\n options?: PouchDB.Core.AllDocsOptions\n ): Promise<PouchDB.Core.AllDocsWithKeysResponse<{} & T>>;\n\n /**\n * Get cards sorted by ELO rating\n */\n getCardsByELO(\n elo: number,\n limit?: number\n ): Promise<\n {\n courseID: string;\n cardID: string;\n elo?: number;\n }[]\n >;\n\n /**\n * Get ELO data for specific cards\n */\n getCardEloData(cardIds: string[]): Promise<CourseElo[]>;\n\n /**\n * Update card ELO rating\n */\n updateCardElo(cardId: string, elo: CourseElo): Promise<PouchDB.Core.Response>;\n\n /**\n * Get new cards for study\n */\n getNewCards(limit?: number): Promise<StudySessionNewItem[]>;\n\n /**\n * Get cards centered at a particular ELO rating\n */\n getCardsCenteredAtELO(\n options: { limit: number; elo: 'user' | 'random' | number },\n filter?: (card: QualifiedCardID) => boolean\n ): Promise<StudySessionItem[]>;\n\n /**\n * Get tags for a card\n */\n getAppliedTags(cardId: string): Promise<PouchDB.Query.Response<TagStub>>;\n\n /**\n * Get tags for multiple cards in a single batch query.\n * More efficient than calling getAppliedTags() for each card.\n *\n * This method reduces redundant database operations when multiple filters\n * need tag data for the same cards. The Pipeline uses this to pre-hydrate\n * tags on WeightedCard objects before filters run.\n *\n * @param cardIds - Array of card IDs to fetch tags for\n * @returns Map from cardId to array of tag names\n */\n getAppliedTagsBatch(cardIds: string[]): Promise<Map<string, string[]>>;\n\n /**\n * Add a tag to a card\n */\n addTagToCard(cardId: string, tagId: string, updateELO?: boolean): Promise<PouchDB.Core.Response>;\n\n /**\n * Remove a tag from a card\n */\n removeTagFromCard(cardId: string, tagId: string): Promise<PouchDB.Core.Response>;\n\n /**\n * Create a new tag\n */\n createTag(tagName: string, author: string): Promise<PouchDB.Core.Response>;\n\n /**\n * Get a tag by name\n */\n getTag(tagName: string): Promise<Tag>;\n\n /**\n * Update a tag\n */\n updateTag(tag: Tag): Promise<PouchDB.Core.Response>;\n\n /**\n * Get all tag stubs for a course\n */\n getCourseTagStubs(): Promise<PouchDB.Core.AllDocsResponse<Tag>>;\n\n /**\n * Add a note to the course\n */\n addNote(\n codeCourse: string,\n shape: DataShape,\n data: unknown,\n author: string,\n tags: string[],\n uploads?: { [key: string]: PouchDB.Core.FullAttachment },\n elo?: CourseElo\n ): Promise<DataLayerResult>;\n\n removeCard(cardId: string): Promise<PouchDB.Core.Response>;\n\n getInexperiencedCards(): Promise<\n {\n courseId: string;\n cardId: string;\n count: number;\n elo: CourseElo;\n }[]\n >;\n\n /**\n * Search for cards by text content\n * @param query Text to search for\n * @returns Array of matching card data\n */\n searchCards(query: string): Promise<any[]>;\n\n /**\n * Find documents using PouchDB query syntax\n * @param request PouchDB find request\n * @returns Query response\n */\n find(request: PouchDB.Find.FindRequest<any>): Promise<PouchDB.Find.FindResponse<any>>;\n}\n","// db/src/core/interfaces.ts\n\nimport { UserDBInterface, UserDBReader } from './userDB';\nimport { CourseDBInterface, CoursesDBInterface } from './courseDB';\nimport { ClassroomDBInterface } from './classroomDB';\nimport { AdminDBInterface } from './adminDB';\n\n/**\n * Main factory interface for data access\n */\nexport interface DataLayerProvider {\n /**\n * Get the user database interface\n */\n getUserDB(): UserDBInterface;\n\n /**\n * Create a UserDBReader for a specific user (admin access required)\n * Uses session authentication to verify requesting user is admin\n * @param targetUsername - The username to create a reader for\n * @throws Error if requesting user is not 'admin'\n */\n createUserReaderForUser(targetUsername: string): Promise<UserDBReader>;\n\n /**\n * Get a course database interface\n */\n getCourseDB(courseId: string): CourseDBInterface;\n\n /**\n * Get the courses-lookup interface\n */\n getCoursesDB(): CoursesDBInterface;\n\n /**\n * Get a classroom database interface\n */\n getClassroomDB(classId: string, type: 'student' | 'teacher'): Promise<ClassroomDBInterface>;\n\n /**\n * Get the admin database interface\n */\n getAdminDB(): AdminDBInterface;\n\n /**\n * Initialize the data layer\n */\n initialize(): Promise<void>;\n\n /**\n * Teardown the data layer\n */\n teardown(): Promise<void>;\n\n /**\n * Check if this data layer is read-only\n */\n isReadOnly(): boolean;\n}\n","import {\n ActivityRecord,\n CourseRegistration,\n CourseRegistrationDoc,\n ScheduledCard,\n} from '@db/core/types/user';\nimport { CourseElo, Status } from '@vue-skuilder/common';\nimport { Moment } from 'moment';\nimport { CardHistory, CardRecord, QualifiedCardID } from '../types/types-legacy';\nimport { UserConfig } from '../types/user';\nimport { DocumentUpdater } from '@db/study';\n\n/**\n * Read-only user data operations\n */\nexport interface UserDBReader {\n get<T>(id: string): Promise<T & PouchDB.Core.RevisionIdMeta>;\n getUsername(): string;\n isLoggedIn(): boolean;\n\n /**\n * Get user configuration\n */\n getConfig(): Promise<UserConfig>;\n\n /**\n * Get cards that the user has seen\n */\n getSeenCards(courseId?: string): Promise<string[]>;\n\n /**\n * Get cards that are actively scheduled for review\n */\n getActiveCards(): Promise<QualifiedCardID[]>;\n\n /**\n * Get user's course registrations\n */\n getCourseRegistrationsDoc(): Promise<CourseRegistrationDoc>;\n\n /**\n * Get the registration doc for a specific course.\n * @param courseId\n */\n getCourseRegDoc(courseId: string): Promise<CourseRegistration>;\n\n /**\n * Get user's active courses\n */\n getActiveCourses(): Promise<CourseRegistration[]>;\n\n /**\n * Get user's pending reviews\n */\n getPendingReviews(courseId?: string): Promise<ScheduledCard[]>;\n\n getActivityRecords(): Promise<ActivityRecord[]>;\n\n /**\n * Get strategy-specific state for a course.\n *\n * Strategies use this to persist preferences, learned patterns, or temporal\n * tracking data across sessions. Each strategy owns its own namespace.\n *\n * @param courseId - The course this state applies to\n * @param strategyKey - Unique key identifying the strategy (typically class name)\n * @returns The strategy's data payload, or null if no state exists\n */\n getStrategyState<T>(courseId: string, strategyKey: string): Promise<T | null>;\n\n /**\n * Get user's classroom registrations\n */\n getUserClassrooms(): Promise<ClassroomRegistrationDoc>;\n\n /**\n * Get user's active classes\n */\n getActiveClasses(): Promise<string[]>;\n\n getCourseInterface(courseId: string): Promise<UsrCrsDataInterface>;\n}\n\n/**\n * User data mutation operations\n */\nexport interface UserDBWriter extends DocumentUpdater {\n /**\n * Update user configuration\n */\n setConfig(config: Partial<UserConfig>): Promise<void>;\n\n /**\n * Record a user's interaction with a card\n */\n putCardRecord<T extends CardRecord>(record: T): Promise<CardHistory<CardRecord>>;\n\n /**\n * Register user for a course\n */\n registerForCourse(courseId: string, previewMode?: boolean): Promise<PouchDB.Core.Response>;\n\n /**\n * Drop a course registration\n */\n dropCourse(courseId: string, dropStatus?: string): Promise<PouchDB.Core.Response>;\n\n /**\n * Schedule a card for review\n */\n scheduleCardReview(review: {\n user: string;\n course_id: string;\n card_id: string;\n time: Moment;\n scheduledFor: 'course' | 'classroom';\n schedulingAgentId: string;\n }): Promise<void>;\n\n /**\n * Remove a scheduled card review\n */\n removeScheduledCardReview(reviewId: string): Promise<void>;\n\n /**\n * Register user for a classroom\n */\n registerForClassroom(\n classId: string,\n registerAs: 'student' | 'teacher' | 'aide' | 'admin'\n ): Promise<PouchDB.Core.Response>;\n\n /**\n * Drop user from classroom\n */\n dropFromClassroom(classId: string): Promise<PouchDB.Core.Response>;\n\n /**\n * Update user's ELO rating for a course\n */\n updateUserElo(courseId: string, elo: CourseElo): Promise<PouchDB.Core.Response>;\n\n /**\n * Reset all user data (progress, registrations, etc.) while preserving authentication\n */\n resetUserData(): Promise<{ status: Status; error?: string }>;\n\n /**\n * Store strategy-specific state for a course.\n *\n * Strategies use this to persist preferences, learned patterns, or temporal\n * tracking data across sessions. Each strategy owns its own namespace.\n *\n * @param courseId - The course this state applies to\n * @param strategyKey - Unique key identifying the strategy (typically class name)\n * @param data - The strategy's data payload to store\n */\n putStrategyState<T>(courseId: string, strategyKey: string, data: T): Promise<void>;\n\n /**\n * Delete strategy-specific state for a course.\n *\n * @param courseId - The course this state applies to\n * @param strategyKey - Unique key identifying the strategy (typically class name)\n */\n deleteStrategyState(courseId: string, strategyKey: string): Promise<void>;\n}\n\n/**\n * Authentication and account management operations\n */\nexport interface UserDBAuthenticator {\n /**\n * Create a new user account\n */\n createAccount(\n username: string,\n password: string\n ): Promise<{\n status: Status;\n error: string;\n }>;\n\n /**\n * Log in as a user\n */\n login(\n username: string,\n password: string\n ): Promise<{\n ok: boolean;\n name?: string;\n roles?: string[];\n }>;\n\n /**\n * Log out the current user\n */\n logout(): Promise<{\n ok: boolean;\n }>;\n}\n\n/**\n * Complete user database interface - combines all user operations\n * This maintains backward compatibility with existing code\n */\nexport interface UserDBInterface extends UserDBReader, UserDBWriter, UserDBAuthenticator {}\n\nexport interface UserCourseSettings {\n [setting: string]: string | number | boolean;\n}\n\nexport interface UserCourseSetting {\n key: string;\n value: string | number | boolean;\n}\n\n// [ ] reconsider here. Should maybe be generic type based on <T extends StudyContentSource> ?\nexport interface UsrCrsDataInterface {\n getScheduledReviewCount(): Promise<number>;\n getCourseSettings(): Promise<UserCourseSettings>;\n updateCourseSettings(updates: UserCourseSetting[]): void; // [ ] return a result of some sort?\n // getRegistrationDoc(): Promise<CourseRegistration>;\n}\n\nexport type ClassroomRegistrationDesignation = 'student' | 'teacher' | 'aide' | 'admin';\n\nexport interface ClassroomRegistration {\n classID: string;\n registeredAs: ClassroomRegistrationDesignation;\n}\n\nexport interface ClassroomRegistrationDoc {\n registrations: ClassroomRegistration[];\n}\n","export * from './adminDB';\nexport * from './classroomDB';\nexport * from './contentSource';\nexport * from './courseDB';\nexport * from './dataLayerProvider';\n\nexport * from './userDB';\n","import { CourseElo } from '@vue-skuilder/common';\nimport { Moment } from 'moment';\n\nexport interface SessionTrackingData {\n peekSessionCount: number;\n studySessionCount: number;\n sessionCount: number; // total\n firstSessionDate: string;\n lastSessionDate: string;\n signupPrompted: boolean;\n promptDismissalCount: number;\n studyModeAcknowledged: boolean;\n}\n\nexport interface UserConfig {\n darkMode: boolean;\n likesConfetti: boolean;\n sessionTimeLimit: number; // Session time limit in minutes\n email?: string; // Optional email for verification flows (added for enhanced auth)\n\n // Session tracking for trial enforcement (per-course)\n // Key is courseId (e.g., 'letterspractice-basic')\n sessionTracking?: Record<string, SessionTrackingData>;\n}\n\nexport interface ActivityRecord {\n timeStamp: number | string;\n [key: string]: any;\n}\n\nexport interface CourseRegistration {\n status?: 'active' | 'dropped' | 'maintenance-mode' | 'preview';\n courseID: string;\n admin: boolean;\n moderator: boolean;\n user: boolean;\n settings?: {\n [setting: string]: string | number | boolean;\n };\n elo: number | CourseElo;\n}\n\ninterface StudyWeights {\n [courseID: string]: number;\n}\n\nexport interface CourseRegistrationDoc {\n courses: CourseRegistration[];\n studyWeight: StudyWeights;\n}\n\nexport interface ScheduledCard {\n _id: PouchDB.Core.DocumentId;\n\n /**\n * The docID of the card to be reviewed\n */\n cardId: PouchDB.Core.DocumentId;\n /**\n * The ID of the course\n */\n courseId: string;\n /**\n * The time at which the card becomes eligible for review.\n *\n * (Should probably be UTC adjusted so that performance is\n * not wonky across time zones)\n * \n * Note: Stored as ISO string for PouchDB serialization compatibility,\n * but can be consumed as Moment objects via moment.utc(reviewTime)\n */\n reviewTime: string | Moment;\n\n /**\n * The time at which this scheduled event was created.\n * \n * Note: Stored as ISO string for PouchDB serialization compatibility,\n * but can be consumed as Moment objects via moment.utc(scheduledAt)\n */\n scheduledAt: string | Moment;\n\n /**\n * Classifying whether this card is scheduled on behalf of a\n * user-registered course or by as assigned content from a\n * user-registered classroom\n */\n scheduledFor: 'course' | 'classroom';\n\n /**\n * The ID of the course or classroom that requested this card\n */\n schedulingAgentId: string;\n}\n","import { DocType, DocTypePrefixes } from './types-legacy';\n\n/**\n * Template literal type for strategy state document IDs.\n *\n * Format: `STRATEGY_STATE-{courseId}-{strategyKey}`\n */\nexport type StrategyStateId =\n `${(typeof DocTypePrefixes)[DocType.STRATEGY_STATE]}::${string}::${string}`;\n\n/**\n * Document storing strategy-specific state in the user database.\n *\n * Each strategy can persist its own state (user preferences, learned patterns,\n * temporal tracking, etc.) using this document type. The state is scoped to\n * a (user, course, strategy) tuple.\n *\n * ## Use Cases\n *\n * 1. **Explicit user preferences**: User configures tag filters, difficulty\n * preferences, or learning goals. UI writes to strategy state.\n *\n * 2. **Learned/temporal state**: Strategy tracks patterns over time, e.g.,\n * \"when did I last introduce confusable concepts together?\"\n *\n * 3. **Adaptive personalization**: Strategy infers user preferences from\n * behavior and stores them for future sessions.\n *\n * ## Storage Location\n *\n * These documents live in the **user database**, not the course database.\n * They sync with the user's data across devices.\n *\n * ## Document ID Format\n *\n * `STRATEGY_STATE::{courseId}::{strategyKey}`\n *\n * Example: `STRATEGY_STATE::piano-basics::UserTagPreferenceFilter`\n *\n * @template T - The shape of the strategy-specific data payload\n */\nexport interface StrategyStateDoc<T = unknown> {\n _id: StrategyStateId;\n _rev?: string;\n docType: DocType.STRATEGY_STATE;\n\n /**\n * The course this state applies to.\n */\n courseId: string;\n\n /**\n * Unique key identifying the strategy instance.\n * Typically the strategy class name (e.g., \"UserTagPreferenceFilter\",\n * \"InterferenceMitigatorNavigator\").\n *\n * If a course has multiple instances of the same strategy type with\n * different configurations, use a more specific key.\n */\n strategyKey: string;\n\n /**\n * Strategy-specific data payload.\n * Each strategy defines its own schema for this field.\n */\n data: T;\n\n /**\n * ISO timestamp of last update.\n * Use `moment.utc(updatedAt)` to parse into a Moment object.\n */\n updatedAt: string;\n}\n\n/**\n * Build the document ID for a strategy state document.\n *\n * @param courseId - The course ID\n * @param strategyKey - The strategy key (typically class name)\n * @returns The document ID in format `STRATEGY_STATE::{courseId}::{strategyKey}`\n */\nexport function buildStrategyStateId(courseId: string, strategyKey: string): StrategyStateId {\n return `STRATEGY_STATE::${courseId}::${strategyKey}`;\n}\n","import { DocType, DocTypePrefixes, CardHistory, CardRecord, QuestionRecord } from '../types/types-legacy';\n\nexport function areQuestionRecords(h: CardHistory<CardRecord>): h is CardHistory<QuestionRecord> {\n return isQuestionRecord(h.records[0]);\n}\n\nexport function isQuestionRecord(c: CardRecord): c is QuestionRecord {\n return (c as QuestionRecord).userAnswer !== undefined;\n}\n\nexport function getCardHistoryID(courseID: string, cardID: string): PouchDB.Core.DocumentId {\n return `${DocTypePrefixes[DocType.CARDRECORD]}-${courseID}-${cardID}`;\n}\n\nexport function parseCardHistoryID(id: string): {\n courseID: string;\n cardID: string;\n} {\n const split = id.split('-');\n let error: string = '';\n error += split.length === 3 ? '' : `\\n\\tgiven ID has incorrect number of '-' characters`;\n error +=\n split[0] === DocTypePrefixes[DocType.CARDRECORD] ? '' : `\n\tgiven ID does not start with ${DocTypePrefixes[DocType.CARDRECORD]}`;\n\n if (split.length === 3 && split[0] === DocTypePrefixes[DocType.CARDRECORD]) {\n return {\n courseID: split[1],\n cardID: split[2],\n };\n } else {\n throw new Error('parseCardHistory Error:' + error);\n }\n}\n\ninterface PouchDBError extends Error {\n error?: string;\n reason?: string;\n}\n\nexport function docIsDeleted(e: PouchDBError): boolean {\n return Boolean(e?.error === 'not_found' && e?.reason === 'deleted');\n}\n","import { CourseElo, Status, ParsedCard, BulkImportCardData } from '@vue-skuilder/common';\nimport { CourseDBInterface } from '../../core/interfaces/courseDB';\nimport { ImportResult, BulkCardProcessorConfig } from './types';\nimport { logger } from '../../util/logger';\n\n/**\n * Processes multiple cards from bulk text input\n *\n * @param parsedCards - Array of parsed cards to import\n * @param courseDB - Course database interface\n * @param config - Configuration for the card processor\n * @returns Array of import results\n */\nexport async function importParsedCards(\n parsedCards: ParsedCard[],\n courseDB: CourseDBInterface,\n config: BulkCardProcessorConfig\n): Promise<ImportResult[]> {\n const results: ImportResult[] = [];\n\n for (const parsedCard of parsedCards) {\n try {\n // processCard takes a ParsedCard and returns an ImportResult\n const result = await processCard(parsedCard, courseDB, config);\n results.push(result);\n } catch (error) {\n logger.error('Error processing card:', error);\n // Reconstruct originalText from parsedCard for this specific catch block\n // This is a fallback if processCard itself throws an unhandled error.\n // Normally, processCard should return an ImportResult with status 'error'.\n let errorOriginalText = parsedCard.markdown;\n if (parsedCard.tags && parsedCard.tags.length > 0) {\n errorOriginalText += `\\ntags: ${parsedCard.tags.join(', ')}`;\n }\n if (parsedCard.elo !== undefined) {\n errorOriginalText += `\\nelo: ${parsedCard.elo}`;\n }\n results.push({\n originalText: errorOriginalText,\n status: 'error',\n message: `Error processing card: ${\n error instanceof Error ? error.message : 'Unknown error'\n }`,\n });\n }\n }\n\n return results;\n}\n\n/**\n * Processes a single parsed card\n *\n * @param parsedCard - Parsed card data\n * @param courseDB - Course database interface\n * @param config - Configuration for the card processor\n * @returns Import result for the card\n */\nasync function processCard(\n parsedCard: ParsedCard,\n courseDB: CourseDBInterface,\n config: BulkCardProcessorConfig\n): Promise<ImportResult> {\n const { markdown, tags, elo } = parsedCard;\n\n // Build the original text representation including metadata\n let originalText = markdown;\n if (tags.length > 0) {\n originalText += `\\ntags: ${tags.join(', ')}`;\n }\n if (elo !== undefined) {\n originalText += `\\nelo: ${elo}`;\n }\n\n // Create card data object\n const cardData: BulkImportCardData = {\n Input: markdown,\n Uploads: [], // No uploads for bulk import\n };\n\n const tagsElo: CourseElo['tags'] = {};\n for (const tag of tags) {\n tagsElo[tag] = {\n score: elo || 0,\n count: 1,\n };\n }\n\n try {\n const result = await courseDB.addNote(\n config.courseCode,\n config.dataShape,\n cardData,\n config.userName,\n tags,\n undefined, // attachments\n elo\n ? {\n global: {\n score: elo,\n count: 1,\n },\n tags: tagsElo,\n misc: {},\n }\n : undefined\n );\n\n if (result.status === Status.ok) {\n return {\n originalText,\n status: 'success',\n message: 'Card added successfully.',\n cardId: result.id ? result.id : '(unknown)',\n };\n } else {\n return {\n originalText,\n status: 'error',\n message: result.message || 'Failed to add card to database. Unknown error.',\n };\n }\n } catch (error) {\n logger.error('Error adding note:', error);\n return {\n originalText,\n status: 'error',\n message: `Error adding card: ${error instanceof Error ? error.message : 'Unknown error'}`,\n };\n }\n}\n\n/**\n * Validates the configuration for bulk card processing\n *\n * @param config - Configuration to validate\n * @returns Object with validation result and error message if any\n */\nexport function validateProcessorConfig(config: Partial<BulkCardProcessorConfig>): {\n isValid: boolean;\n errorMessage?: string;\n} {\n if (!config.dataShape) {\n return {\n isValid: false,\n errorMessage: 'No data shape provided for card processing',\n };\n }\n\n if (!config.courseCode) {\n return {\n isValid: false,\n errorMessage: 'No course code provided for card processing',\n };\n }\n\n if (!config.userName) {\n return {\n isValid: false,\n errorMessage: 'No user name provided for card processing',\n };\n }\n\n return { isValid: true };\n}\n","import { DataShape } from '@vue-skuilder/common';\n\n/**\n * Interface representing the result of a bulk import operation for a single card\n */\nexport interface ImportResult {\n /** The original text input for the card */\n originalText: string;\n /** Status of the import operation */\n status: 'success' | 'error';\n /** Message describing the result or error */\n message: string;\n /** ID of the newly created card (only for success) */\n cardId?: string;\n}\n\n/**\n * Configuration for the bulk card processor\n */\nexport interface BulkCardProcessorConfig {\n /** The data shape to use for the cards */\n dataShape: DataShape;\n /** The course code used for adding notes */\n courseCode: string;\n /** The username of the current user */\n userName: string;\n}\n","export * from './cardProcessor.js';\nexport * from './types.js';","// Export all core interfaces and types\n\nexport * from './interfaces';\nexport * from './types/types-legacy';\nexport * from './types/user';\nexport * from './types/strategyState';\nexport * from '../util/Loggable';\nexport * from './util';\nexport * from './navigators';\nexport * from './bulkImport';\n","// TUI-aware logging utility that redirects logs to file in Node.js\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport { getAppDataDirectory } from './dataDirectory';\n\nlet logFile: string | null = null;\nlet isNodeEnvironment = false;\n\n/**\n * Initialize TUI logging - redirect console logs to file in Node.js\n */\nexport function initializeTuiLogging(): void {\n // Detect Node.js environment\n isNodeEnvironment = typeof window === 'undefined' && typeof process !== 'undefined';\n \n if (!isNodeEnvironment) {\n return; // Browser environment - keep normal console logging\n }\n\n try {\n // Set up log file path\n logFile = path.join(getAppDataDirectory(), 'lastrun.log');\n \n // Clear previous log file\n if (fs.existsSync(logFile)) {\n fs.unlinkSync(logFile);\n }\n \n // Create initial log entry\n const startTime = new Date().toISOString();\n fs.writeFileSync(logFile, `=== TUI Session Started: ${startTime} ===\\n`);\n \n // Redirect console methods to file\n const originalConsole = {\n // eslint-disable-next-line no-console\n log: console.log,\n // eslint-disable-next-line no-console\n error: console.error,\n // eslint-disable-next-line no-console\n warn: console.warn,\n // eslint-disable-next-line no-console\n info: console.info\n };\n \n const writeToLog = (level: string, args: any[]) => {\n const timestamp = new Date().toISOString();\n const message = args.map(arg => \n typeof arg === 'object' ? JSON.stringify(arg, null, 2) : String(arg)\n ).join(' ');\n \n const logEntry = `[${timestamp}] ${level}: ${message}\\n`;\n \n try {\n fs.appendFileSync(logFile!, logEntry);\n } catch (err) {\n // Fallback to original console if file write fails\n originalConsole.error('Failed to write to log file:', err);\n originalConsole[level.toLowerCase() as keyof typeof originalConsole](...args);\n }\n };\n \n // Override console methods\n // eslint-disable-next-line no-console\n console.log = (...args) => writeToLog('INFO', args);\n // eslint-disable-next-line no-console\n console.info = (...args) => writeToLog('INFO', args);\n // eslint-disable-next-line no-console\n console.warn = (...args) => writeToLog('WARN', args);\n // eslint-disable-next-line no-console\n console.error = (...args) => writeToLog('ERROR', args);\n \n // Store original methods for potential restoration\n (console as any)._originalMethods = originalConsole;\n \n // eslint-disable-next-line no-console\n console.log('TUI logging initialized - logs redirected to', logFile);\n \n } catch (err) {\n // eslint-disable-next-line no-console\n console.error('Failed to initialize TUI logging:', err);\n }\n}\n\n/**\n * Get the current log file path (for debugging)\n */\nexport function getLogFilePath(): string | null {\n return logFile;\n}\n\n/**\n * Show user-facing message (always visible in TUI)\n */\nexport function showUserMessage(message: string): void {\n if (isNodeEnvironment) {\n // In Node.js, write directly to stdout to bypass log redirection\n process.stdout.write(message + '\\n');\n } else {\n // In browser, use normal console\n // eslint-disable-next-line no-console\n console.log(message);\n }\n}\n\n/**\n * Show user-facing error (always visible in TUI)\n */\nexport function showUserError(message: string): void {\n if (isNodeEnvironment) {\n // In Node.js, write directly to stderr to bypass log redirection\n process.stderr.write('Error: ' + message + '\\n');\n } else {\n // In browser, use normal console\n // eslint-disable-next-line no-console\n console.error(message);\n }\n}\n\n/**\n * Logger object with standard log levels\n */\nexport const logger = {\n debug: (message: string, ...args: any[]) => {\n // eslint-disable-next-line no-console\n console.log(`[DEBUG] ${message}`, ...args);\n },\n info: (message: string, ...args: any[]) => {\n // eslint-disable-next-line no-console\n console.info(`[INFO] ${message}`, ...args);\n },\n warn: (message: string, ...args: any[]) => {\n // eslint-disable-next-line no-console\n console.warn(`[WARN] ${message}`, ...args);\n },\n error: (message: string, ...args: any[]) => {\n // eslint-disable-next-line no-console\n console.error(`[ERROR] ${message}`, ...args);\n },\n};","// Cross-platform data directory utilities for PouchDB\n// Provides OS-appropriate application data directories\n\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport * as os from 'os';\nimport { logger } from './tuiLogger';\nimport { ENV } from '@db/factory';\n\n/**\n * Get the application data directory for the current platform\n * Uses ~/.tuilder as requested by user for simplicity\n */\nexport function getAppDataDirectory(): string {\n if (ENV.LOCAL_STORAGE_PREFIX) {\n return path.join(os.homedir(), `.tuilder`, ENV.LOCAL_STORAGE_PREFIX);\n } else {\n return path.join(os.homedir(), '.tuilder');\n }\n}\n\n/**\n * Ensure the application data directory exists\n * Creates directory recursively if it doesn't exist\n */\nexport async function ensureAppDataDirectory(): Promise<string> {\n const appDataDir = getAppDataDirectory();\n \n try {\n await fs.promises.mkdir(appDataDir, { recursive: true });\n } catch (err: any) {\n if (err.code !== 'EEXIST') {\n throw new Error(`Failed to create app data directory ${appDataDir}: ${err.message}`);\n }\n }\n \n return appDataDir;\n}\n\n/**\n * Get the full path for a PouchDB database file\n * @param dbName - The database name (e.g., 'userdb-Colin')\n */\nexport function getDbPath(dbName: string): string {\n return path.join(getAppDataDirectory(), dbName);\n}\n\n/**\n * Initialize data directory for PouchDB usage\n * Should be called once at application startup\n */\nexport async function initializeDataDirectory(): Promise<void> {\n await ensureAppDataDirectory();\n \n // Log initialization\n logger.info(`PouchDB data directory initialized: ${getAppDataDirectory()}`);\n}","// packages/db/src/impl/common/userDBHelpers.ts\n\nimport moment from 'moment';\nimport { DocType, DocTypePrefixes } from '@db/core';\nimport { logger } from '../../util/logger';\nimport { ScheduledCard } from '@db/core/types/user';\n\nexport const REVIEW_TIME_FORMAT: string = 'YYYY-MM-DD--kk:mm:ss-SSS';\n\nimport pouch from '../couch/pouchdb-setup';\nimport { getDbPath } from '../../util/dataDirectory';\n\nconst log = (s: any) => {\n logger.info(s);\n};\n\nexport function hexEncode(str: string): string {\n let hex: string;\n let returnStr: string = '';\n\n for (let i = 0; i < str.length; i++) {\n hex = str.charCodeAt(i).toString(16);\n returnStr += ('000' + hex).slice(3);\n }\n\n return returnStr;\n}\n\nexport function filterAllDocsByPrefix<T>(\n db: PouchDB.Database,\n prefix: string,\n opts?: PouchDB.Core.AllDocsOptions\n) {\n // see couchdb docs 6.2.2:\n // Guide to Views -> Views Collation -> String Ranges\n const options: PouchDB.Core.AllDocsWithinRangeOptions = {\n startkey: prefix,\n endkey: prefix + '\\ufff0',\n include_docs: true,\n };\n\n if (opts) {\n Object.assign(options, opts);\n }\n return db.allDocs<T>(options);\n}\n\nexport function getStartAndEndKeys(key: string): {\n startkey: string;\n endkey: string;\n} {\n return {\n startkey: key,\n endkey: key + '\\ufff0',\n };\n}\n\nexport function updateGuestAccountExpirationDate(guestDB: PouchDB.Database<object>) {\n const currentTime = moment.utc();\n const expirationDate: string = currentTime.add(2, 'months').toISOString();\n const expiryDocID: string = 'GuestAccountExpirationDate';\n\n void guestDB\n .get(expiryDocID)\n .then((doc) => {\n return guestDB.put({\n _id: expiryDocID,\n _rev: doc._rev,\n date: expirationDate,\n });\n })\n .catch(() => {\n return guestDB.put({\n _id: expiryDocID,\n date: expirationDate,\n });\n });\n}\n\n/**\n * Get local user database with appropriate adapter for environment\n */\nexport function getLocalUserDB(username: string): PouchDB.Database {\n // // Choose adapter based on environment\n //\n // Not certain of this is required. Let's let pouch's auto detection\n // handle it until we specifically know we need to intervene.\n //\n // let adapter: string;\n // if (typeof window !== 'undefined') {\n // // Browser environment - use IndexedDB\n // adapter = 'idb';\n // } else {\n // // Node.js environment (tests) - use memory adapter\n // adapter = 'memory';\n // }\n\n const dbName = `userdb-${username}`;\n \n // Use proper data directory in Node.js, browser will use IndexedDB\n if (typeof window === 'undefined') {\n // Node.js environment - use filesystem with proper app data directory\n return new pouch(getDbPath(dbName), {});\n } else {\n // Browser environment - use default (IndexedDB)\n return new pouch(dbName, {});\n }\n}\n\n/**\n * Schedule a card review (strategy-agnostic version)\n */\nexport function scheduleCardReviewLocal(\n userDB: PouchDB.Database,\n review: {\n card_id: PouchDB.Core.DocumentId;\n time: moment.Moment;\n course_id: string;\n scheduledFor: ScheduledCard['scheduledFor'];\n schedulingAgentId: ScheduledCard['schedulingAgentId'];\n }\n) {\n const now = moment.utc();\n logger.info(`Scheduling for review in: ${review.time.diff(now, 'h') / 24} days`);\n void userDB.put<ScheduledCard>({\n _id: DocTypePrefixes[DocType.SCHEDULED_CARD] + review.time.format(REVIEW_TIME_FORMAT),\n cardId: review.card_id,\n reviewTime: review.time.toISOString(),\n courseId: review.course_id,\n scheduledAt: now.toISOString(),\n scheduledFor: review.scheduledFor,\n schedulingAgentId: review.schedulingAgentId,\n });\n}\n\n/**\n * Remove a scheduled card review (strategy-agnostic version)\n */\nexport async function removeScheduledCardReviewLocal(\n userDB: PouchDB.Database,\n reviewDocID: string\n) {\n const reviewDoc = await userDB.get(reviewDocID);\n userDB\n .remove(reviewDoc)\n .then((res) => {\n if (res.ok) {\n log(`Removed Review Doc: ${reviewDocID}`);\n }\n })\n .catch((err) => {\n log(`Failed to remove Review Doc: ${reviewDocID},\\n${JSON.stringify(err)}`);\n });\n}\n","import {\n ScheduledCard,\n UserCourseSetting,\n UserCourseSettings,\n UsrCrsDataInterface,\n} from '@db/core';\n\nimport moment, { Moment } from 'moment';\n\nimport { UserDBInterface } from '@db/core';\nimport { logger } from '../../util/logger';\n\nexport class UsrCrsData implements UsrCrsDataInterface {\n private user: UserDBInterface;\n private _courseId: string;\n\n constructor(user: UserDBInterface, courseId: string) {\n this.user = user;\n this._courseId = courseId;\n }\n\n public async getReviewsForcast(daysCount: number) {\n const time = moment.utc().add(daysCount, 'days');\n return this.getReviewstoDate(time);\n }\n\n public async getPendingReviews() {\n const now = moment.utc();\n return this.getReviewstoDate(now);\n }\n\n public async getScheduledReviewCount(): Promise<number> {\n return (await this.getPendingReviews()).length;\n }\n\n public async getCourseSettings(): Promise<UserCourseSettings> {\n const regDoc = await this.user.getCourseRegistrationsDoc();\n const crsDoc = regDoc.courses.find((c) => c.courseID === this._courseId);\n\n if (crsDoc && crsDoc.settings) {\n return crsDoc.settings;\n } else {\n logger.warn(`no settings found during lookup on course ${this._courseId}`);\n return {};\n }\n }\n public updateCourseSettings(updates: UserCourseSetting[]): void {\n // TODO: Add updateCourseSettings method to UserDBInterface\n // For now, we'll need to cast to access the concrete implementation\n if ('updateCourseSettings' in this.user) {\n void (this.user as any).updateCourseSettings(this._courseId, updates);\n }\n }\n\n private async getReviewstoDate(targetDate: Moment) {\n // Use the interface method instead of direct database access\n const allReviews = await this.user.getPendingReviews(this._courseId);\n\n logger.debug(\n `Fetching ${this.user.getUsername()}'s scheduled reviews for course ${this._courseId}.`\n );\n\n return allReviews.filter((review: ScheduledCard) => {\n const reviewTime = moment.utc(review.reviewTime);\n return targetDate.isAfter(reviewTime);\n });\n }\n}\n","import { DocType, DocTypePrefixes, StrategyStateDoc, buildStrategyStateId } from '@db/core';\nimport { getCardHistoryID } from '@db/core/util';\nimport { CourseElo, Status } from '@vue-skuilder/common';\nimport moment, { Moment } from 'moment';\nimport { GuestUsername } from '../../core/types/types-legacy';\nimport { logger } from '../../util/logger';\n\nimport {\n ClassroomRegistrationDoc,\n UserCourseSetting,\n UserDBInterface,\n UsrCrsDataInterface,\n} from '@db/core';\nimport {\n ActivityRecord,\n CourseRegistration,\n CourseRegistrationDoc,\n ScheduledCard,\n UserConfig,\n} from '@db/core/types/user';\nimport { DocumentUpdater } from '@db/study';\nimport { CardHistory, CardRecord } from '../../core/types/types-legacy';\nimport type { SyncStrategy } from './SyncStrategy';\nimport {\n filterAllDocsByPrefix,\n getStartAndEndKeys,\n REVIEW_TIME_FORMAT,\n getLocalUserDB,\n scheduleCardReviewLocal,\n removeScheduledCardReviewLocal,\n} from './userDBHelpers';\nimport { PouchError } from '../couch/types';\nimport UpdateQueue, { Update } from '../couch/updateQueue';\nimport { UsrCrsData } from '../couch/user-course-relDB';\nimport { getCredentialledCourseConfig } from '../couch/index';\n\nconst log = (s: any) => {\n logger.info(s);\n};\n\n// logger.log(`Connecting to remote: ${remoteStr}`);\n\ninterface DesignDoc {\n _id: string;\n views: {\n [viewName: string]: {\n map: string; // String representation of the map function\n };\n };\n}\n\n/**\n * Base user database implementation that uses a pluggable sync strategy.\n * Handles local storage operations and delegates sync/remote operations to the strategy.\n */\nexport class BaseUser implements UserDBInterface, DocumentUpdater {\n private static _instance: BaseUser;\n private static _initialized: boolean = false;\n\n public static Dummy(syncStrategy: SyncStrategy): BaseUser {\n return new BaseUser('Me', syncStrategy);\n }\n\n static readonly DOC_IDS = {\n CONFIG: 'CONFIG',\n COURSE_REGISTRATIONS: 'CourseRegistrations',\n CLASSROOM_REGISTRATIONS: 'ClassroomRegistrations',\n };\n\n // private email: string;\n private _username: string;\n private syncStrategy: SyncStrategy;\n\n public getUsername(): string {\n return this._username;\n }\n\n public isLoggedIn(): boolean {\n return !this._username.startsWith(GuestUsername);\n }\n\n public remote(): PouchDB.Database {\n return this.remoteDB;\n }\n\n private localDB!: PouchDB.Database;\n private remoteDB!: PouchDB.Database;\n private writeDB!: PouchDB.Database; // Database to use for write operations (local-first approach)\n\n private updateQueue!: UpdateQueue;\n\n public async createAccount(\n username: string,\n password: string\n ): Promise<{\n status: Status;\n error: string;\n }> {\n if (!this.syncStrategy.canCreateAccount()) {\n throw new Error('Account creation not supported by current sync strategy');\n }\n\n if (!this._username.startsWith(GuestUsername)) {\n throw new Error(\n `Cannot create a new account while logged in:\nCurrently logged-in as ${this._username}.`\n );\n }\n\n const result = await this.syncStrategy.createAccount!(username, password);\n\n // If account creation was successful, update the username and reinitialize\n if (result.status === Status.ok) {\n log(`Account created successfully, updating username to ${username}`);\n this._username = username;\n try {\n localStorage.removeItem('sk-guest-uuid');\n } catch (e) {\n logger.warn('localStorage not available (Node.js environment):', e);\n }\n await this.init();\n }\n\n return {\n status: result.status,\n error: result.error || '',\n };\n }\n public async login(username: string, password: string) {\n if (!this.syncStrategy.canAuthenticate()) {\n throw new Error('Authentication not supported by current sync strategy');\n }\n\n if (!this._username.startsWith(GuestUsername) && this._username != username) {\n if (this._username != username) {\n throw new Error(`Cannot change accounts while logged in.\n Log out of account ${this.getUsername()} before logging in as ${username}.`);\n }\n logger.warn(`User ${this._username} is already logged in, but executing login again.`);\n }\n\n const loginResult = await this.syncStrategy.authenticate!(username, password);\n if (loginResult.ok) {\n log(`Logged in as ${username}`);\n this._username = username;\n try {\n localStorage.removeItem('sk-guest-uuid');\n } catch (e) {\n logger.warn('localStorage not available (Node.js environment):', e);\n }\n await this.init();\n }\n return loginResult;\n }\n\n public async resetUserData(): Promise<{ status: Status; error?: string }> {\n // Only allow reset for local-only sync strategies\n if (this.syncStrategy.canAuthenticate()) {\n return {\n status: Status.error,\n error:\n 'Reset user data is only available for local-only mode. Use logout instead for remote sync.',\n };\n }\n\n try {\n const localDB = getLocalUserDB(this._username);\n\n // Get all documents to identify user data to clear\n const allDocs = await localDB.allDocs({ include_docs: false });\n\n // Identify documents to delete (preserve authentication and user identity)\n const docsToDelete = allDocs.rows\n .filter((row) => {\n const id = row.id;\n // Delete user progress data but preserve core user documents\n return (\n id.startsWith(DocTypePrefixes[DocType.CARDRECORD]) || // Card interaction history\n id.startsWith(DocTypePrefixes[DocType.SCHEDULED_CARD]) || // Scheduled reviews\n id === BaseUser.DOC_IDS.COURSE_REGISTRATIONS || // Course registrations\n id === BaseUser.DOC_IDS.CLASSROOM_REGISTRATIONS || // Classroom registrations\n id === BaseUser.DOC_IDS.CONFIG // User config\n );\n })\n .map((row) => ({ _id: row.id, _rev: row.value.rev, _deleted: true }));\n\n if (docsToDelete.length > 0) {\n await localDB.bulkDocs(docsToDelete);\n }\n\n // Reinitialize to create fresh default documents\n await this.init();\n\n return { status: Status.ok };\n } catch (error) {\n logger.error('Failed to reset user data:', error);\n return {\n status: Status.error,\n error: error instanceof Error ? error.message : 'Unknown error during reset',\n };\n }\n }\n\n public async logout() {\n if (!this.syncStrategy.canAuthenticate()) {\n // For strategies that don't support authentication, just switch to guest\n this._username = await this.syncStrategy.getCurrentUsername();\n await this.init();\n return { ok: true };\n }\n\n const ret = await this.syncStrategy.logout!();\n // return to 'guest' mode\n this._username = await this.syncStrategy.getCurrentUsername();\n await this.init();\n\n return ret;\n }\n\n public async get<T>(id: string): Promise<T & PouchDB.Core.RevisionIdMeta> {\n return this.localDB.get<T>(id);\n }\n\n public update<T extends PouchDB.Core.Document<object>>(id: string, update: Update<T>) {\n return this.updateQueue.update(id, update);\n }\n\n public async getCourseRegistrationsDoc(): Promise<\n CourseRegistrationDoc & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta\n > {\n logger.debug(`Fetching courseRegistrations for ${this.getUsername()}`);\n\n let ret;\n\n try {\n const regDoc = await this.localDB.get<CourseRegistrationDoc>(\n BaseUser.DOC_IDS.COURSE_REGISTRATIONS\n );\n return regDoc;\n } catch (e) {\n const err = e as PouchError;\n if (err.status === 404) {\n await this.localDB.put<CourseRegistrationDoc>({\n _id: BaseUser.DOC_IDS.COURSE_REGISTRATIONS,\n courses: [],\n studyWeight: {},\n });\n ret = await this.getCourseRegistrationsDoc();\n } else {\n throw new Error(\n `Unexpected error ${JSON.stringify(e)} in getOrCreateCourseRegistrationDoc...`\n );\n }\n }\n\n return ret;\n }\n\n public async getActiveCourses() {\n const reg = await this.getCourseRegistrationsDoc();\n return reg.courses.filter((c) => {\n return c.status === undefined || c.status === 'active';\n });\n }\n\n /**\n * Returns a promise of the card IDs that the user has\n * a scheduled review for.\n *\n */\n public async getActiveCards() {\n const keys = getStartAndEndKeys(DocTypePrefixes[DocType.SCHEDULED_CARD]);\n\n const reviews = await this.remoteDB.allDocs<ScheduledCard>({\n startkey: keys.startkey,\n endkey: keys.endkey,\n include_docs: true,\n });\n\n return reviews.rows.map((r) => {\n return {\n courseID: r.doc!.courseId,\n cardID: r.doc!.cardId,\n };\n });\n }\n\n public async getActivityRecords(): Promise<ActivityRecord[]> {\n try {\n const hist = await this.getHistory();\n\n const allRecords: ActivityRecord[] = [];\n if (!Array.isArray(hist)) {\n logger.error('getHistory did not return an array:', hist);\n return allRecords;\n }\n\n // Sample the first few records to understand structure\n let sampleCount = 0;\n\n for (let i = 0; i < hist.length; i++) {\n try {\n if (hist[i] && Array.isArray(hist[i]!.records)) {\n hist[i]!.records.forEach((record: CardRecord) => {\n try {\n // Skip this record if timeStamp is missing\n if (!record.timeStamp) {\n return;\n }\n\n let timeStamp;\n\n // Handle different timestamp formats\n if (typeof record.timeStamp === 'object') {\n // It's likely a Moment object\n if (typeof record.timeStamp.toDate === 'function') {\n // It's definitely a Moment object\n timeStamp = record.timeStamp.toISOString();\n } else if (record.timeStamp instanceof Date) {\n // It's a Date object\n timeStamp = record.timeStamp.toISOString();\n } else {\n // Log a sample of unknown object types, but don't flood logger\n if (sampleCount < 3) {\n logger.warn('Unknown timestamp object type:', record.timeStamp);\n sampleCount++;\n }\n return;\n }\n } else if (typeof record.timeStamp === 'string') {\n // It's already a string, but make sure it's a valid date\n const date = new Date(record.timeStamp);\n if (isNaN(date.getTime())) {\n return; // Invalid date string\n }\n timeStamp = record.timeStamp;\n } else if (typeof record.timeStamp === 'number') {\n // Assume it's a Unix timestamp (milliseconds since epoch)\n timeStamp = new Date(record.timeStamp).toISOString();\n } else {\n // Unknown type, skip\n return;\n }\n\n allRecords.push({\n timeStamp,\n courseID: record.courseID || 'unknown',\n cardID: record.cardID || 'unknown',\n timeSpent: record.timeSpent || 0,\n type: 'card_view',\n });\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (err) {\n // Silently skip problematic records to avoid flooding logs\n }\n });\n }\n } catch (err) {\n logger.error('Error processing history item:', err);\n }\n }\n\n logger.debug(`Found ${allRecords.length} activity records`);\n return allRecords;\n } catch (err) {\n logger.error('Error in getActivityRecords:', err);\n return [];\n }\n }\n\n private async getReviewstoDate(targetDate: Moment, course_id?: string) {\n const keys = getStartAndEndKeys(DocTypePrefixes[DocType.SCHEDULED_CARD]);\n\n const reviews = await this.remoteDB.allDocs<ScheduledCard>({\n startkey: keys.startkey,\n endkey: keys.endkey,\n include_docs: true,\n });\n\n log(\n `Fetching ${this._username}'s scheduled reviews${\n course_id ? ` for course ${course_id}` : ''\n }.`\n );\n return reviews.rows\n .filter((r) => {\n if (r.id.startsWith(DocTypePrefixes[DocType.SCHEDULED_CARD])) {\n const date = moment.utc(\n r.id.substr(DocTypePrefixes[DocType.SCHEDULED_CARD].length),\n REVIEW_TIME_FORMAT\n );\n if (targetDate.isAfter(date)) {\n if (course_id === undefined || r.doc!.courseId === course_id) {\n return true;\n }\n }\n }\n })\n .map((r) => r.doc!);\n }\n\n public async getReviewsForcast(daysCount: number) {\n const time = moment.utc().add(daysCount, 'days');\n return this.getReviewstoDate(time);\n }\n\n public async getPendingReviews(course_id?: string) {\n const now = moment.utc();\n return this.getReviewstoDate(now, course_id);\n }\n\n public async getScheduledReviewCount(course_id: string): Promise<number> {\n return (await this.getPendingReviews(course_id)).length;\n }\n\n public async getRegisteredCourses() {\n const regDoc = await this.getCourseRegistrationsDoc();\n return regDoc.courses.filter((c) => {\n return !c.status || c.status === 'active' || c.status === 'maintenance-mode';\n });\n }\n\n public async getCourseRegDoc(courseID: string) {\n const regDocs = await this.getCourseRegistrationsDoc();\n const ret = regDocs.courses.find((c) => c.courseID === courseID);\n if (ret) {\n return ret;\n } else {\n throw new Error(`Course registration not found for course ID: ${courseID}`);\n }\n }\n\n public async registerForCourse(course_id: string, previewMode: boolean = false) {\n return this.getCourseRegistrationsDoc()\n .then((doc: CourseRegistrationDoc) => {\n const status = previewMode ? 'preview' : 'active';\n logger.debug(`Registering for ${course_id} with status: ${status}`);\n\n const regItem: CourseRegistration = {\n status: status,\n courseID: course_id,\n user: true,\n admin: false,\n moderator: false,\n elo: {\n global: {\n score: 1000,\n count: 0,\n },\n tags: {},\n misc: {},\n },\n };\n\n if (\n doc.courses.filter((course) => {\n return course.courseID === regItem.courseID;\n }).length === 0\n ) {\n log(`It's a new course registration!`);\n doc.courses.push(regItem);\n doc.studyWeight[course_id] = 1;\n } else {\n doc.courses.forEach((c) => {\n log(`Found the previously registered course!`);\n if (c.courseID === course_id) {\n c.status = status;\n }\n });\n }\n\n return this.localDB.put<CourseRegistrationDoc>(doc);\n })\n .catch((e) => {\n log(`Registration failed because of: ${JSON.stringify(e)}`);\n throw e;\n });\n }\n public async dropCourse(course_id: string, dropStatus: CourseRegistration['status'] = 'dropped') {\n return this.getCourseRegistrationsDoc().then((doc) => {\n let index: number = -1;\n for (let i = 0; i < doc.courses.length; i++) {\n if (doc.courses[i].courseID === course_id) {\n index = i;\n }\n }\n\n if (index !== -1) {\n // remove from the relative-weighting of course study\n delete doc.studyWeight[course_id];\n // set drop status\n doc.courses[index].status = dropStatus;\n } else {\n throw new Error(\n `User ${this.getUsername()} is not currently registered for course ${course_id}`\n );\n }\n\n return this.localDB.put<CourseRegistrationDoc>(doc);\n });\n }\n\n public async getCourseInterface(courseId: string): Promise<UsrCrsDataInterface> {\n return new UsrCrsData(this, courseId);\n }\n\n public async getUserEditableCourses() {\n let courseIDs: string[] = [];\n\n const registeredCourses = await this.getCourseRegistrationsDoc();\n\n courseIDs = courseIDs.concat(\n registeredCourses.courses.map((course) => {\n return course.courseID;\n })\n );\n\n const cfgs = await Promise.all(\n courseIDs.map(async (id) => {\n return await getCredentialledCourseConfig(id);\n })\n );\n return cfgs;\n }\n\n public async getConfig(): Promise<UserConfig & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta> {\n const defaultConfig: PouchDB.Core.Document<UserConfig> = {\n _id: BaseUser.DOC_IDS.CONFIG,\n darkMode: false,\n likesConfetti: false,\n sessionTimeLimit: 5,\n };\n\n try {\n const cfg = await this.localDB.get<UserConfig>(BaseUser.DOC_IDS.CONFIG);\n logger.debug('Raw config from DB:', cfg);\n\n return cfg;\n } catch (e) {\n const err = e as PouchError;\n if (err.name && err.name === 'not_found') {\n await this.localDB.put<UserConfig>(defaultConfig);\n return this.getConfig();\n } else {\n logger.error(`Error setting user default config:`, e);\n throw new Error(`Error returning the user's configuration: ${JSON.stringify(e)}`);\n }\n }\n }\n\n public async setConfig(items: Partial<UserConfig>) {\n logger.debug(`Setting Config items ${JSON.stringify(items)}`);\n\n const c = await this.getConfig();\n const put = await this.localDB.put<UserConfig>({\n ...c,\n ...items,\n });\n\n if (put.ok) {\n logger.debug(`Config items set: ${JSON.stringify(items)}`);\n } else {\n logger.error(`Error setting config items: ${JSON.stringify(put)}`);\n }\n }\n\n /**\n *\n * This function should be called *only* by the pouchdb datalayer provider\n * auth store.\n *\n *\n * Anyone else seeking the current user should use the auth store's\n * exported `getCurrentUser` method.\n *\n */\n public static async instance(syncStrategy: SyncStrategy, username?: string): Promise<BaseUser> {\n if (username) {\n BaseUser._instance = new BaseUser(username, syncStrategy);\n await BaseUser._instance.init();\n return BaseUser._instance;\n } else if (BaseUser._instance && BaseUser._initialized) {\n // log(`USER.instance() returning user ${BaseUser._instance._username}`);\n return BaseUser._instance;\n } else if (BaseUser._instance) {\n return new Promise((resolve) => {\n (function waitForUser() {\n if (BaseUser._initialized) {\n return resolve(BaseUser._instance);\n } else {\n setTimeout(waitForUser, 50);\n }\n })();\n });\n } else {\n const guestUsername = await syncStrategy.getCurrentUsername();\n BaseUser._instance = new BaseUser(guestUsername, syncStrategy);\n await BaseUser._instance.init();\n return BaseUser._instance;\n }\n }\n\n private constructor(username: string, syncStrategy: SyncStrategy) {\n BaseUser._initialized = false;\n this._username = username;\n this.syncStrategy = syncStrategy;\n this.setDBandQ();\n }\n\n private setDBandQ() {\n this.localDB = getLocalUserDB(this._username);\n this.remoteDB = this.syncStrategy.setupRemoteDB(this._username);\n // writeDB follows local-first pattern: static mode writes to local, CouchDB writes to remote/local as appropriate\n this.writeDB = this.syncStrategy.getWriteDB\n ? this.syncStrategy.getWriteDB(this._username)\n : this.localDB;\n this.updateQueue = new UpdateQueue(this.localDB, this.writeDB);\n }\n\n private async init() {\n BaseUser._initialized = false;\n\n // Skip admin user\n if (this._username === 'admin') {\n BaseUser._initialized = true;\n return;\n }\n\n this.setDBandQ();\n\n this.syncStrategy.startSync(this.localDB, this.remoteDB);\n this.applyDesignDocs().catch((error) => {\n log(`Error in applyDesignDocs background task: ${error}`);\n if (error && typeof error === 'object') {\n log(`Full error details in applyDesignDocs: ${JSON.stringify(error)}`);\n }\n });\n this.deduplicateReviews().catch((error) => {\n log(`Error in deduplicateReviews background task: ${error}`);\n if (error && typeof error === 'object') {\n log(`Full error details in background task: ${JSON.stringify(error)}`);\n }\n });\n BaseUser._initialized = true;\n }\n\n private static designDocs: DesignDoc[] = [\n {\n _id: '_design/reviewCards',\n views: {\n reviewCards: {\n map: `function (doc) {\n if (doc._id && doc._id.indexOf('card_review') === 0 && doc.courseId && doc.cardId) {\n emit(doc._id, doc.courseId + '-' + doc.cardId);\n }\n }`,\n },\n },\n },\n ];\n\n private async applyDesignDocs() {\n log(`Starting applyDesignDocs for user: ${this._username}`);\n log(`Remote DB name: ${this.remoteDB.name || 'unknown'}`);\n\n if (this._username === 'admin') {\n // Skip admin user\n log('Skipping design docs for admin user');\n return;\n }\n\n log(`Applying ${BaseUser.designDocs.length} design docs`);\n for (const doc of BaseUser.designDocs) {\n log(`Applying design doc: ${doc._id}`);\n try {\n // Try to get existing doc\n try {\n const existingDoc = await this.remoteDB.get(doc._id);\n // Update existing doc\n await this.remoteDB.put({\n ...doc,\n _rev: existingDoc._rev,\n });\n } catch (e: unknown) {\n if (e instanceof Error && e.name === 'not_found') {\n // Create new doc\n await this.remoteDB.put(doc);\n } else {\n throw e; // Re-throw unexpected errors\n }\n }\n } catch (error: unknown) {\n if ((error as any).name && (error as any).name === 'conflict') {\n logger.warn(`Design doc ${doc._id} update conflict - will retry`);\n // Wait a bit and try again\n await new Promise((resolve) => setTimeout(resolve, 1000));\n await this.applyDesignDoc(doc); // Recursive retry\n } else {\n logger.error(`Failed to apply design doc ${doc._id}:`, error);\n throw error;\n }\n }\n }\n }\n\n // Helper method for single doc update with retry\n private async applyDesignDoc(doc: DesignDoc, retries = 3): Promise<void> {\n try {\n const existingDoc = await this.remoteDB.get(doc._id);\n await this.remoteDB.put({\n ...doc,\n _rev: existingDoc._rev,\n });\n } catch (e: unknown) {\n if (e instanceof Error && e.name === 'conflict' && retries > 0) {\n await new Promise((resolve) => setTimeout(resolve, 1000));\n return this.applyDesignDoc(doc, retries - 1);\n }\n throw e;\n }\n }\n\n /**\n * Logs a record of the user's interaction with the card and returns the card's\n * up-to-date history.\n *\n * **Automatic Initialization:**\n * If this is the user's first interaction with the card (CardHistory doesn't exist),\n * this method automatically creates the CardHistory document with initial values\n * (lapses: 0, streak: 0, bestInterval: 0).\n *\n * **Error Handling:**\n * - Handles 404 errors by creating initial CardHistory document\n * - Re-throws all other errors from UpdateQueue\n *\n * // [ ] #db-refactor extract to a smaller scope - eg, UserStudySession\n *\n * @param record - The recent recorded interaction between user and card\n * @returns The updated state of the card's CardHistory data\n * @throws Error if document creation fails or non-404 database error occurs\n */\n\n public async putCardRecord<T extends CardRecord>(\n record: T\n ): Promise<CardHistory<CardRecord> & PouchDB.Core.RevisionIdMeta> {\n const cardHistoryID = getCardHistoryID(record.courseID, record.cardID);\n // stringify the current record to make it writable to couchdb\n record.timeStamp = moment.utc(record.timeStamp).toString() as unknown as Moment;\n\n try {\n const cardHistory = await this.update<CardHistory<T>>(\n cardHistoryID,\n function (h: CardHistory<T>) {\n h.records.push(record);\n h.bestInterval = h.bestInterval || 0;\n h.lapses = h.lapses || 0;\n h.streak = h.streak || 0;\n return h;\n }\n );\n\n // Convert timestamps to moment objects\n cardHistory.records = cardHistory.records.map<T>((record) => {\n const ret: T = {\n ...(record as object),\n } as T;\n ret.timeStamp = moment.utc(record.timeStamp);\n return ret;\n });\n return cardHistory;\n } catch (e) {\n const reason = e as PouchError;\n if (reason.status === 404) {\n try {\n const initCardHistory: CardHistory<T> = {\n _id: cardHistoryID,\n cardID: record.cardID,\n courseID: record.courseID,\n records: [record],\n lapses: 0,\n streak: 0,\n bestInterval: 0,\n };\n const putResult = await this.writeDB.put<CardHistory<T>>(initCardHistory);\n return { ...initCardHistory, _rev: putResult.rev };\n } catch (creationError) {\n throw new Error(\n `Failed to create CardHistory for ${cardHistoryID}. Reason: ${creationError}`\n );\n }\n } else {\n throw new Error(`putCardRecord failed because of:\n name:${reason.name}\n error: ${reason.error}\n message: ${reason.message}`);\n }\n }\n }\n\n private async deduplicateReviews() {\n try {\n log('Starting deduplication of scheduled reviews...');\n log(`Remote DB name: ${this.remoteDB.name || 'unknown'}`);\n log(`Write DB name: ${this.writeDB.name || 'unknown'}`);\n /**\n * Maps the qualified-id of a scheduled review card to\n * the docId of the same scheduled review.\n *\n * EG: {\n * courseId-cardId: 'card_review_2021-06--17:12:165'\n * }\n */\n const reviewsMap: { [index: string]: string } = {};\n const duplicateDocIds: string[] = [];\n\n log(\n `Attempting to query remoteDB for reviewCards/reviewCards. Database: ${this.remoteDB.name || 'unknown'}`\n );\n const scheduledReviews = await this.remoteDB.query<{\n id: string;\n value: string;\n }>('reviewCards/reviewCards');\n\n log(`Found ${scheduledReviews.rows.length} scheduled reviews to process`);\n\n // First pass: identify duplicates\n scheduledReviews.rows.forEach((r) => {\n const qualifiedCardId = r.value; // courseId-cardId\n const docId = r.key; // card_review_2021-06--17:12:165\n\n if (reviewsMap[qualifiedCardId]) {\n // this card is scheduled more than once! mark the earlier one for deletion\n log(`Found duplicate scheduled review for card: ${qualifiedCardId}`);\n log(\n `Marking earlier review ${reviewsMap[qualifiedCardId]} for deletion, keeping ${docId}`\n );\n duplicateDocIds.push(reviewsMap[qualifiedCardId]);\n // replace with the later-dated scheduled review\n reviewsMap[qualifiedCardId] = docId;\n } else {\n // note that this card is scheduled for review\n reviewsMap[qualifiedCardId] = docId;\n }\n });\n\n // Second pass: remove duplicates\n if (duplicateDocIds.length > 0) {\n log(`Removing ${duplicateDocIds.length} duplicate reviews...`);\n const deletePromises = duplicateDocIds.map(async (docId) => {\n try {\n const doc = await this.remoteDB.get(docId);\n await this.writeDB.remove(doc);\n log(`Successfully removed duplicate review: ${docId}`);\n } catch (error) {\n log(`Failed to remove duplicate review ${docId}: ${error}`);\n }\n });\n\n await Promise.all(deletePromises);\n log(`Deduplication complete. Processed ${duplicateDocIds.length} duplicates`);\n } else {\n log('No duplicate reviews found');\n }\n } catch (error) {\n log(`Error during review deduplication: ${error}`);\n if (error && typeof error === 'object' && 'status' in error && error.status === 404) {\n log(\n `Database not found (404) during review deduplication. Database: ${this.remoteDB.name || 'unknown'}`\n );\n log(\n `This might indicate the user database doesn't exist or the reviewCards view isn't available`\n );\n }\n // Log full error details for debugging\n if (error && typeof error === 'object') {\n log(`Full error details: ${JSON.stringify(error)}`);\n }\n }\n }\n\n /**\n * Returns a promise of the card IDs that the user has\n * encountered in the past.\n *\n * @param course_id optional specification of individual course\n */\n async getSeenCards(course_id?: string) {\n let prefix = DocTypePrefixes[DocType.CARDRECORD];\n if (course_id) {\n prefix += course_id;\n }\n const docs = await filterAllDocsByPrefix(this.localDB, prefix, {\n include_docs: false,\n });\n // const docs = await this.localDB.allDocs({});\n const ret: PouchDB.Core.DocumentId[] = [];\n docs.rows.forEach((row) => {\n if (row.id.startsWith(DocTypePrefixes[DocType.CARDRECORD])) {\n ret.push(row.id.substr(DocTypePrefixes[DocType.CARDRECORD].length));\n }\n });\n return ret;\n }\n\n /**\n *\n * @returns A promise of the cards that the user has seen in the past.\n */\n async getHistory() {\n const cards = await filterAllDocsByPrefix<CardHistory<CardRecord>>(\n this.remoteDB,\n DocTypePrefixes[DocType.CARDRECORD],\n {\n include_docs: true,\n attachments: false,\n }\n );\n return cards.rows.map((r) => r.doc);\n }\n\n async updateCourseSettings(course_id: string, settings: UserCourseSetting[]) {\n void this.getCourseRegistrationsDoc().then((doc) => {\n const crs = doc.courses.find((c) => c.courseID === course_id);\n if (crs) {\n if (crs.settings === null || crs.settings === undefined) {\n crs.settings = {};\n }\n settings.forEach((setting) => {\n crs!.settings![setting.key] = setting.value;\n });\n }\n\n return this.localDB.put(doc);\n });\n }\n async getCourseSettings(course_id: string) {\n const regDoc = await this.getCourseRegistrationsDoc();\n const crsDoc = regDoc.courses.find((c) => c.courseID === course_id);\n\n if (crsDoc) {\n return crsDoc.settings;\n } else {\n throw new Error(`getCourseSettings Failed:\n User is not registered for course ${course_id}`);\n }\n }\n\n private async getOrCreateClassroomRegistrationsDoc(): Promise<\n ClassroomRegistrationDoc & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta\n > {\n let ret;\n\n try {\n ret = await this.remoteDB.get<ClassroomRegistrationDoc>(\n BaseUser.DOC_IDS.CLASSROOM_REGISTRATIONS\n );\n } catch (e) {\n const err = e as PouchError;\n\n if (err.status === 404) {\n // doc does not exist. Create it and then run this fcn again.\n await this.writeDB.put<ClassroomRegistrationDoc>({\n _id: BaseUser.DOC_IDS.CLASSROOM_REGISTRATIONS,\n registrations: [],\n });\n ret = await this.getOrCreateClassroomRegistrationsDoc();\n } else {\n // Properly serialize error information\n const errorDetails = {\n name: err.name,\n status: err.status,\n message: err.message,\n reason: err.reason,\n error: err.error,\n };\n\n logger.error(\n 'Database error in getOrCreateClassroomRegistrationsDoc (private method):',\n errorDetails\n );\n\n throw new Error(\n `Database error accessing classroom registrations: ${err.message || err.name || 'Unknown error'} (status: ${err.status})`\n );\n }\n }\n\n logger.debug(`Returning classroom registrations doc: ${JSON.stringify(ret)}`);\n return ret;\n }\n\n /**\n * Retrieves the list of active classroom IDs where the user is registered as a student.\n *\n * @returns Promise<string[]> - Array of classroom IDs, or empty array if classroom\n * registration document is unavailable due to database errors\n *\n * @description This method gracefully handles database connectivity issues by returning\n * an empty array when the classroom registrations document cannot be accessed.\n * This ensures that users can still access other application features even\n * when classroom functionality is temporarily unavailable.\n */\n public async getActiveClasses(): Promise<string[]> {\n try {\n return (await this.getOrCreateClassroomRegistrationsDoc()).registrations\n .filter((c) => c.registeredAs === 'student')\n .map((c) => c.classID);\n } catch (error) {\n logger.warn(\n 'Failed to load classroom registrations, continuing without classroom data:',\n error\n );\n // Return empty array so user can still access other features\n return [];\n }\n }\n\n public async scheduleCardReview(review: {\n user: string;\n course_id: string;\n card_id: PouchDB.Core.DocumentId;\n time: Moment;\n scheduledFor: ScheduledCard['scheduledFor'];\n schedulingAgentId: ScheduledCard['schedulingAgentId'];\n }) {\n return scheduleCardReviewLocal(this.writeDB, review);\n }\n public async removeScheduledCardReview(reviewId: string): Promise<void> {\n return removeScheduledCardReviewLocal(this.writeDB, reviewId);\n }\n\n public async registerForClassroom(\n _classId: string,\n _registerAs: 'student' | 'teacher' | 'aide' | 'admin'\n ): Promise<PouchDB.Core.Response> {\n return registerUserForClassroom(this._username, _classId, _registerAs);\n }\n\n public async dropFromClassroom(classId: string): Promise<PouchDB.Core.Response> {\n return dropUserFromClassroom(this._username, classId);\n }\n public async getUserClassrooms(): Promise<ClassroomRegistrationDoc> {\n return getUserClassrooms(this._username);\n }\n\n public async updateUserElo(courseId: string, elo: CourseElo): Promise<PouchDB.Core.Response> {\n return updateUserElo(this._username, courseId, elo);\n }\n\n public async getStrategyState<T>(courseId: string, strategyKey: string): Promise<T | null> {\n const docId = buildStrategyStateId(courseId, strategyKey);\n try {\n const doc = await this.localDB.get<StrategyStateDoc<T>>(docId);\n return doc.data;\n } catch (e) {\n const err = e as PouchError;\n if (err.status === 404) {\n return null;\n }\n throw e;\n }\n }\n\n public async putStrategyState<T>(courseId: string, strategyKey: string, data: T): Promise<void> {\n const docId = buildStrategyStateId(courseId, strategyKey);\n let existingRev: string | undefined;\n\n try {\n const existing = await this.localDB.get<StrategyStateDoc<T>>(docId);\n existingRev = existing._rev;\n } catch (e) {\n const err = e as PouchError;\n if (err.status !== 404) {\n throw e;\n }\n }\n\n const doc: StrategyStateDoc<T> = {\n _id: docId,\n _rev: existingRev,\n docType: DocType.STRATEGY_STATE,\n courseId,\n strategyKey,\n data,\n updatedAt: new Date().toISOString(),\n };\n\n await this.localDB.put(doc);\n }\n\n public async deleteStrategyState(courseId: string, strategyKey: string): Promise<void> {\n const docId = buildStrategyStateId(courseId, strategyKey);\n try {\n const doc = await this.localDB.get(docId);\n await this.localDB.remove(doc);\n } catch (e) {\n const err = e as PouchError;\n if (err.status === 404) {\n return;\n }\n throw e;\n }\n }\n}\n\nexport function accomodateGuest(): {\n username: string;\n firstVisit: boolean;\n} {\n logger.log('[funnel] accomodateGuest() called');\n\n // Check if localStorage is available (browser environment)\n if (typeof localStorage === 'undefined') {\n logger.log(\n '[funnel] localStorage not available (Node.js environment), returning default guest'\n );\n return {\n username: GuestUsername + 'nodejs-test',\n firstVisit: true,\n };\n }\n\n const dbUUID = 'sk-guest-uuid';\n let firstVisit: boolean;\n\n const existingUUID = localStorage.getItem(dbUUID);\n logger.log('[funnel] Checking localStorage for key:', dbUUID);\n logger.log('[funnel] Existing UUID value:', existingUUID);\n logger.log('[funnel] existingUUID !== null:', existingUUID !== null);\n\n if (existingUUID !== null) {\n firstVisit = false;\n logger.log(`[funnel] Returning guest ${existingUUID} \"logging in\".`);\n } else {\n firstVisit = true;\n logger.log('[funnel] No existing UUID, generating new one...');\n const uuid = generateUUID();\n logger.log('[funnel] Generated UUID:', uuid);\n logger.log('[funnel] UUID length:', uuid.length);\n\n try {\n localStorage.setItem(dbUUID, uuid);\n logger.log('[funnel] Successfully stored UUID in localStorage');\n const verification = localStorage.getItem(dbUUID);\n logger.log('[funnel] Verification read from localStorage:', verification);\n } catch (e) {\n logger.error('[funnel] ERROR storing UUID:', e);\n }\n\n logger.log(`[funnel] Accommodating a new guest with account: ${uuid}`);\n }\n\n const finalUUID = localStorage.getItem(dbUUID);\n const finalUsername = GuestUsername + finalUUID;\n logger.log('[funnel] Final UUID from localStorage:', finalUUID);\n logger.log('[funnel] GuestUsername constant:', GuestUsername);\n logger.log('[funnel] Final username to return:', finalUsername);\n\n return {\n username: finalUsername,\n firstVisit: firstVisit,\n };\n\n // Use cryptographically secure UUID generation\n function generateUUID() {\n logger.log('[funnel] Inside generateUUID()');\n\n // Use crypto.randomUUID() if available (Node 14.17+ / modern browsers)\n if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {\n const uuid = crypto.randomUUID();\n logger.log('[funnel] Generated UUID using crypto.randomUUID():', uuid);\n return uuid;\n }\n\n // Fallback for older environments: use crypto.getRandomValues()\n if (typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function') {\n const bytes = new Uint8Array(16);\n crypto.getRandomValues(bytes);\n\n // Set version (4) and variant bits according to RFC 4122\n bytes[6] = (bytes[6] & 0x0f) | 0x40; // Version 4\n bytes[8] = (bytes[8] & 0x3f) | 0x80; // Variant 10\n\n const uuid = [\n Array.from(bytes.slice(0, 4))\n .map((b) => b.toString(16).padStart(2, '0'))\n .join(''),\n Array.from(bytes.slice(4, 6))\n .map((b) => b.toString(16).padStart(2, '0'))\n .join(''),\n Array.from(bytes.slice(6, 8))\n .map((b) => b.toString(16).padStart(2, '0'))\n .join(''),\n Array.from(bytes.slice(8, 10))\n .map((b) => b.toString(16).padStart(2, '0'))\n .join(''),\n Array.from(bytes.slice(10, 16))\n .map((b) => b.toString(16).padStart(2, '0'))\n .join(''),\n ].join('-');\n\n logger.log('[funnel] Generated UUID using crypto.getRandomValues():', uuid);\n return uuid;\n }\n\n // Last resort fallback (should never happen in modern environments)\n logger.warn('[funnel] crypto API not available, using timestamp-based UUID (NOT SECURE)');\n let d = new Date().getTime();\n if (typeof performance !== 'undefined' && typeof performance.now === 'function') {\n d += performance.now();\n }\n const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {\n const r = (d + Math.random() * 16) % 16 | 0;\n d = Math.floor(d / 16);\n return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);\n });\n logger.log('[funnel] Generated UUID (fallback):', uuid);\n return uuid;\n }\n}\n\nconst userCoursesDoc = 'CourseRegistrations';\nconst userClassroomsDoc = 'ClassroomRegistrations';\n\nasync function getOrCreateClassroomRegistrationsDoc(\n user: string\n): Promise<ClassroomRegistrationDoc & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta> {\n let ret;\n\n try {\n ret = await getLocalUserDB(user).get<ClassroomRegistrationDoc>(userClassroomsDoc);\n } catch (e) {\n const err = e as PouchError;\n\n if (err.status === 404) {\n // doc does not exist. Create it and then run this fcn again.\n await getLocalUserDB(user).put<ClassroomRegistrationDoc>({\n _id: userClassroomsDoc,\n registrations: [],\n });\n ret = await getOrCreateClassroomRegistrationsDoc(user);\n } else {\n // Properly serialize error information\n const errorDetails = {\n name: err.name,\n status: err.status,\n message: err.message,\n reason: err.reason,\n error: err.error,\n };\n\n logger.error(\n 'Database error in getOrCreateClassroomRegistrationsDoc (standalone function):',\n errorDetails\n );\n\n throw new Error(\n `Database error accessing classroom registrations: ${err.message || err.name || 'Unknown error'} (status: ${err.status})`\n );\n }\n }\n\n return ret;\n}\n\nasync function getOrCreateCourseRegistrationsDoc(\n user: string\n): Promise<CourseRegistrationDoc & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta> {\n let ret;\n\n try {\n ret = await getLocalUserDB(user).get<CourseRegistrationDoc>(userCoursesDoc);\n } catch (e) {\n const err = e as PouchError;\n if (err.status === 404) {\n // doc does not exist. Create it and then run this fcn again.\n await getLocalUserDB(user).put<CourseRegistrationDoc>({\n _id: userCoursesDoc,\n courses: [],\n studyWeight: {},\n });\n ret = await getOrCreateCourseRegistrationsDoc(user);\n } else {\n throw new Error(\n `Unexpected error ${JSON.stringify(e)} in getOrCreateCourseRegistrationDoc...`\n );\n }\n }\n\n return ret;\n}\n\nexport async function updateUserElo(user: string, course_id: string, elo: CourseElo) {\n const regDoc = await getOrCreateCourseRegistrationsDoc(user);\n const course = regDoc.courses.find((c) => c.courseID === course_id)!;\n course.elo = elo;\n return getLocalUserDB(user).put(regDoc);\n}\n\nexport async function registerUserForClassroom(\n user: string,\n classID: string,\n registerAs: 'student' | 'teacher' | 'aide' | 'admin'\n) {\n log(`Registering user: ${user} in course: ${classID}`);\n return getOrCreateClassroomRegistrationsDoc(user).then((doc) => {\n const regItem = {\n classID: classID,\n registeredAs: registerAs,\n };\n\n if (\n doc.registrations.filter((reg) => {\n return reg.classID === regItem.classID && reg.registeredAs === regItem.registeredAs;\n }).length === 0\n ) {\n doc.registrations.push(regItem);\n } else {\n log(`User ${user} is already registered for class ${classID}`);\n }\n\n return getLocalUserDB(user).put(doc);\n });\n}\n\nexport async function dropUserFromClassroom(user: string, classID: string) {\n return getOrCreateClassroomRegistrationsDoc(user).then((doc) => {\n let index: number = -1;\n\n for (let i = 0; i < doc.registrations.length; i++) {\n if (doc.registrations[i].classID === classID) {\n index = i;\n }\n }\n\n if (index !== -1) {\n doc.registrations.splice(index, 1);\n }\n return getLocalUserDB(user).put(doc);\n });\n}\n\nexport async function getUserClassrooms(user: string) {\n return getOrCreateClassroomRegistrationsDoc(user);\n}\n","// packages/db/src/impl/common/index.ts\n\nexport type { SyncStrategy } from './SyncStrategy';\nexport { BaseSyncStrategy } from './SyncStrategy';\nexport type {\n AccountCreationResult,\n AuthenticationResult,\n UserSession,\n SyncConfig,\n SyncStatus,\n} from './types';\nexport { BaseUser, accomodateGuest } from './BaseUserDB';\nexport {\n REVIEW_TIME_FORMAT,\n hexEncode,\n filterAllDocsByPrefix,\n getStartAndEndKeys,\n updateGuestAccountExpirationDate,\n getLocalUserDB,\n scheduleCardReviewLocal,\n removeScheduledCardReviewLocal,\n} from './userDBHelpers';\n","// db/src/factory.ts\n\nimport { DataLayerProvider } from './core/interfaces';\nimport { BaseUser } from './impl/common';\nimport { logger } from './util/logger';\nimport { StaticCourseManifest } from './util/packer/types';\n\nconst NOT_SET = 'NOT_SET' as const;\n\ninterface DBEnv {\n COUCHDB_SERVER_URL: string; // URL of CouchDB server\n COUCHDB_SERVER_PROTOCOL: string; // Protocol of CouchDB server (http or https)\n COUCHDB_USERNAME?: string;\n COUCHDB_PASSWORD?: string;\n LOCAL_STORAGE_PREFIX: string; // Prefix for IndexedDB storage names\n}\n\nexport const ENV: DBEnv = {\n COUCHDB_SERVER_PROTOCOL: NOT_SET,\n COUCHDB_SERVER_URL: NOT_SET,\n LOCAL_STORAGE_PREFIX: '',\n};\n\nexport { NOT_SET };\n\n// Configuration type for data layer initialization\nexport interface DataLayerConfig {\n type: 'couch' | 'static';\n options: {\n staticContentPath?: string; // Path to static content JSON files\n localStoragePrefix?: string; // Prefix for IndexedDB storage names\n manifests?: Record<string, StaticCourseManifest>; // Course manifests for static mode\n COUCHDB_SERVER_URL?: string;\n COUCHDB_SERVER_PROTOCOL?: string;\n COUCHDB_USERNAME?: string;\n COUCHDB_PASSWORD?: string;\n\n COURSE_IDS?: string[];\n };\n}\n\n// Singleton instance\nlet dataLayerInstance: DataLayerProvider | null = null;\n\n/**\n * Initialize the data layer with the specified configuration\n */\nexport async function initializeDataLayer(config: DataLayerConfig): Promise<DataLayerProvider> {\n if (dataLayerInstance) {\n logger.warn('Data layer already initialized. Returning existing instance.');\n return dataLayerInstance;\n }\n if (config.options.localStoragePrefix) {\n ENV.LOCAL_STORAGE_PREFIX = config.options.localStoragePrefix;\n }\n\n if (config.type === 'couch') {\n if (!config.options.COUCHDB_SERVER_URL || !config.options.COUCHDB_SERVER_PROTOCOL) {\n throw new Error('Missing CouchDB server URL or protocol');\n }\n ENV.COUCHDB_SERVER_PROTOCOL = config.options.COUCHDB_SERVER_PROTOCOL;\n ENV.COUCHDB_SERVER_URL = config.options.COUCHDB_SERVER_URL;\n ENV.COUCHDB_USERNAME = config.options.COUCHDB_USERNAME;\n ENV.COUCHDB_PASSWORD = config.options.COUCHDB_PASSWORD;\n\n if (\n config.options.COUCHDB_PASSWORD &&\n config.options.COUCHDB_USERNAME &&\n typeof window !== 'undefined'\n ) {\n // Dynamic import to avoid loading both implementations when only one is needed\n const { CouchDBSyncStrategy } = await import('./impl/couch/CouchDBSyncStrategy');\n\n // Create a sync strategy instance and authenticate\n const syncStrategy = new CouchDBSyncStrategy();\n\n const user = await BaseUser.instance(syncStrategy, config.options.COUCHDB_USERNAME);\n const authResult = await user.login(\n config.options.COUCHDB_USERNAME,\n config.options.COUCHDB_PASSWORD\n );\n\n if (authResult.ok) {\n logger.info(`Successfully authenticated as ${config.options.COUCHDB_USERNAME}`);\n } else {\n logger.warn(`Authentication failed: ${authResult.error}`);\n }\n }\n\n // Dynamic import to avoid loading both implementations when only one is needed\n const { CouchDataLayerProvider } = await import('./impl/couch/PouchDataLayerProvider');\n dataLayerInstance = new CouchDataLayerProvider(config.options.COURSE_IDS);\n } else if (config.type === 'static') {\n const { StaticDataLayerProvider } = await import('./impl/static/StaticDataLayerProvider');\n dataLayerInstance = new StaticDataLayerProvider(config.options);\n } else {\n throw new Error(`Unknown data layer type: ${config.type}`);\n }\n\n await dataLayerInstance.initialize();\n return dataLayerInstance;\n}\n\n/**\n * Get the initialized data layer instance\n * @throws Error if not initialized\n */\nexport function getDataLayer(): DataLayerProvider {\n if (!dataLayerInstance) {\n throw new Error('Data layer not initialized. Call initializeDataLayer first.');\n }\n return dataLayerInstance;\n}\n\n/**\n * Reset the data layer (primarily for testing)\n */\nexport async function _resetDataLayer(): Promise<void> {\n if (dataLayerInstance) {\n await dataLayerInstance.teardown();\n }\n dataLayerInstance = null;\n}\n","import pouch from './pouchdb-setup';\nimport { ENV } from '@db/factory';\nimport {\n createPouchDBConfig,\n getStartAndEndKeys,\n getCredentialledCourseConfig,\n updateCredentialledCourseConfig,\n} from '.';\nimport { TeacherClassroomDB, ClassroomLookupDB } from './classroomDB';\nimport { PouchError } from './types';\n\nimport { AdminDBInterface } from '@db/core';\nimport CourseLookup from './courseLookupDB';\nimport { logger } from '@db/util/logger';\n\nexport class AdminDB implements AdminDBInterface {\n private usersDB!: PouchDB.Database;\n\n constructor() {\n // [ ] execute a check here against credentials, and throw an error\n // if the user is not an admin\n this.usersDB = new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + '_users',\n createPouchDBConfig()\n );\n }\n\n public async getUsers() {\n return (\n await this.usersDB.allDocs({\n include_docs: true,\n ...getStartAndEndKeys('org.couchdb.user:'),\n })\n ).rows.map((r) => r.doc!);\n }\n\n public async getCourses() {\n const list = await CourseLookup.allCourseWare();\n return await Promise.all(\n list.map((c) => {\n return getCredentialledCourseConfig(c._id);\n })\n );\n }\n public async removeCourse(id: string) {\n // remove the indexer\n const delResp = await CourseLookup.delete(id);\n\n // set the 'CourseConfig' to 'deleted'\n const cfg = await getCredentialledCourseConfig(id);\n cfg.deleted = true;\n const isDeletedResp = await updateCredentialledCourseConfig(id, cfg);\n\n return {\n ok: delResp.ok && isDeletedResp.ok,\n id: delResp.id,\n rev: delResp.rev,\n };\n }\n\n public async getClassrooms() {\n // const joincodes =\n const uuids = (\n await ClassroomLookupDB().allDocs<{ uuid: string }>({\n include_docs: true,\n })\n ).rows.map((r) => r.doc!.uuid);\n logger.debug(uuids.join(', '));\n\n const promisedCRDbs: TeacherClassroomDB[] = [];\n for (let i = 0; i < uuids.length; i++) {\n try {\n const db = await TeacherClassroomDB.factory(uuids[i]);\n promisedCRDbs.push(db);\n } catch (e) {\n const err = e as PouchError;\n if (err.error && err.error === 'not_found') {\n logger.warn(`db ${uuids[i]} not found`);\n }\n }\n }\n\n return promisedCRDbs.map((db) => {\n return {\n ...db.getConfig(),\n _id: db._id,\n };\n });\n }\n}\n","import { ENV, NOT_SET } from '@db/factory';\nimport { logger } from '@db/util/logger';\nimport fetch from 'cross-fetch';\n\ninterface SessionResponse {\n info: unknown;\n ok: boolean;\n userCtx: {\n name: string;\n roles: string[];\n };\n}\n\nexport async function getCurrentSession(): Promise<SessionResponse> {\n // Legacy XMLHttpRequest implementation\n // return new Promise((resolve, reject) => {\n // const authXML = new XMLHttpRequest();\n // authXML.withCredentials = true;\n //\n // authXML.onerror = (e): void => {\n // reject(new Error('Session check failed:', e));\n // };\n //\n // authXML.addEventListener('load', () => {\n // try {\n // const resp: SessionResponse = JSON.parse(authXML.responseText);\n // resolve(resp);\n // } catch (e) {\n // reject(e);\n // }\n // });\n //\n // const url = `${ENV.COUCHDB_SERVER_PROTOCOL}://${ENV.COUCHDB_SERVER_URL}_session`;\n // authXML.open('GET', url);\n // authXML.send();\n // });\n \n try {\n // Handle case where ENV variables might not be properly set\n if (ENV.COUCHDB_SERVER_URL === NOT_SET || ENV.COUCHDB_SERVER_PROTOCOL === NOT_SET) {\n throw new Error(`CouchDB server configuration not properly initialized. Protocol: \"${ENV.COUCHDB_SERVER_PROTOCOL}\", URL: \"${ENV.COUCHDB_SERVER_URL}\"`);\n }\n \n // Ensure URL has proper slash before _session endpoint\n const baseUrl = ENV.COUCHDB_SERVER_URL.endsWith('/') \n ? ENV.COUCHDB_SERVER_URL.slice(0, -1) \n : ENV.COUCHDB_SERVER_URL;\n const url = `${ENV.COUCHDB_SERVER_PROTOCOL}://${baseUrl}/_session`;\n logger.debug(`Attempting session check at: ${url}`);\n \n const response = await fetch(url, {\n method: 'GET',\n credentials: 'include',\n });\n \n if (!response.ok) {\n throw new Error(`Session check failed: ${response.status}`);\n }\n \n const resp: SessionResponse = await response.json();\n return resp;\n } catch (error) {\n // Use same URL construction logic for error reporting\n const baseUrl = ENV.COUCHDB_SERVER_URL.endsWith('/') \n ? ENV.COUCHDB_SERVER_URL.slice(0, -1) \n : ENV.COUCHDB_SERVER_URL;\n const url = `${ENV.COUCHDB_SERVER_PROTOCOL}://${baseUrl}/_session`;\n logger.error(`Session check error attempting to connect to: ${url} - ${error}`);\n throw new Error(`Session check failed connecting to ${url}: ${error}`);\n }\n}\n\nexport async function getLoggedInUsername(): Promise<string> {\n const session = await getCurrentSession();\n if (session.userCtx.name && session.userCtx.name !== '') {\n return session.userCtx.name;\n }\n // Not logged in - throw so caller can handle guest account\n throw new Error('No logged in user');\n}\n","// packages/db/src/impl/couch/CouchDBSyncStrategy.ts\n\nimport { ENV } from '@db/factory';\nimport { GuestUsername } from '../../core/types/types-legacy';\nimport { logger } from '../../util/logger';\nimport { Status } from '@vue-skuilder/common';\nimport type { SyncStrategy } from '../common/SyncStrategy';\nimport type { AccountCreationResult, AuthenticationResult } from '../common/types';\nimport { getLocalUserDB, hexEncode, updateGuestAccountExpirationDate, accomodateGuest } from '../common';\nimport pouch from './pouchdb-setup';\nimport { createPouchDBConfig } from './index';\nimport { getLoggedInUsername } from './auth';\n\nconst log = (s: any) => {\n logger.info(s);\n};\n\n/**\n * Sync strategy that implements full CouchDB remote synchronization\n * Handles account creation, authentication, and live sync with remote CouchDB server\n */\nexport class CouchDBSyncStrategy implements SyncStrategy {\n private syncHandle?: any; // Handle to cancel sync if needed\n\n setupRemoteDB(username: string): PouchDB.Database {\n if (username === GuestUsername || username.startsWith(GuestUsername)) {\n // For guest users, remote is same as local (no remote sync)\n return getLocalUserDB(username);\n } else {\n // For real users, connect to remote CouchDB\n return this.getUserDB(username);\n }\n }\n\n getWriteDB(username: string): PouchDB.Database {\n if (username === GuestUsername || username.startsWith(GuestUsername)) {\n // Guest users write to local database\n return getLocalUserDB(username);\n } else {\n // Authenticated users write to remote (which will sync to local)\n return this.getUserDB(username);\n }\n }\n\n startSync(localDB: PouchDB.Database, remoteDB: PouchDB.Database): void {\n // Only sync if local and remote are different instances\n if (localDB !== remoteDB) {\n this.syncHandle = pouch.sync(localDB, remoteDB, {\n live: true,\n retry: true,\n });\n }\n // If they're the same (guest mode), no sync needed\n }\n\n stopSync?(): void {\n if (this.syncHandle) {\n this.syncHandle.cancel();\n this.syncHandle = undefined;\n }\n }\n\n canCreateAccount(): boolean {\n return true;\n }\n\n canAuthenticate(): boolean {\n return true;\n }\n\n async createAccount(username: string, password: string): Promise<AccountCreationResult> {\n // IMPORTANT: Capture funnel username BEFORE any operations that might change session state\n const funnelUsername = await this.getCurrentUsername();\n const isFunnelAccount = funnelUsername.startsWith(GuestUsername);\n\n if (isFunnelAccount) {\n logger.info(`Creating account for funnel user ${funnelUsername} -> ${username}`);\n }\n\n try {\n const signupRequest = await this.getRemoteCouchRootDB().signUp(username, password);\n\n if (signupRequest.ok) {\n log(`CREATEACCOUNT: Successfully created account for ${username}`);\n\n // Log out any existing session\n try {\n const logoutResult = await this.getRemoteCouchRootDB().logOut();\n log(`CREATEACCOUNT: logged out: ${logoutResult.ok}`);\n } catch {\n // Ignore logout errors - might not be logged in\n }\n\n // Log in as the new user\n const loginResult = await this.getRemoteCouchRootDB().logIn(username, password);\n log(`CREATEACCOUNT: logged in as new user: ${loginResult.ok}`);\n\n if (loginResult.ok) {\n // Migrate funnel account data if applicable\n if (isFunnelAccount) {\n logger.info(`Migrating data from funnel account ${funnelUsername} to ${username}`);\n const migrationResult = await this.migrateFunnelData(funnelUsername, username);\n if (!migrationResult.success) {\n logger.warn(`Migration failed: ${migrationResult.error}`);\n // Continue anyway - don't block account creation\n }\n }\n\n return {\n status: Status.ok,\n error: undefined,\n };\n } else {\n return {\n status: Status.error,\n error: 'Failed to log in after account creation',\n };\n }\n } else {\n logger.warn(`Signup not OK: ${JSON.stringify(signupRequest)}`);\n return {\n status: Status.error,\n error: 'Account creation failed',\n };\n }\n } catch (e: any) {\n if (e.reason === 'Document update conflict.') {\n return {\n status: Status.error,\n error: 'This username is taken!',\n };\n }\n logger.error(`Error on signup: ${JSON.stringify(e)}`);\n return {\n status: Status.error,\n error: e.message || 'Unknown error during account creation',\n };\n }\n }\n\n async authenticate(username: string, password: string): Promise<AuthenticationResult> {\n try {\n const loginResult = await this.getRemoteCouchRootDB().logIn(username, password);\n\n if (loginResult.ok) {\n log(`Successfully logged in as ${username}`);\n return {\n ok: true,\n };\n } else {\n log(`Login failed for ${username}`);\n return {\n ok: false,\n error: 'Invalid username or password',\n };\n }\n } catch (error: any) {\n logger.error(`Authentication error for ${username}:`, error);\n return {\n ok: false,\n error: error.message || 'Authentication failed',\n };\n }\n }\n\n async logout(): Promise<AuthenticationResult> {\n try {\n const result = await this.getRemoteCouchRootDB().logOut();\n return {\n ok: result.ok,\n error: result.ok ? undefined : 'Logout failed',\n };\n } catch (error: any) {\n logger.error('Logout error:', error);\n return {\n ok: false,\n error: error.message || 'Logout failed',\n };\n }\n }\n\n async getCurrentUsername(): Promise<string> {\n logger.log('[funnel] CouchDBSyncStrategy.getCurrentUsername() called');\n try {\n const loggedInUsername = await getLoggedInUsername();\n logger.log('[funnel] getLoggedInUsername() returned:', loggedInUsername);\n return loggedInUsername;\n } catch (e) {\n // Not logged in - return unique guest account\n logger.log('[funnel] getLoggedInUsername() failed, calling accomodateGuest()');\n logger.log('[funnel] Error was:', e);\n const guestInfo = accomodateGuest();\n logger.log('[funnel] accomodateGuest() returned:', guestInfo);\n return guestInfo.username;\n }\n }\n\n /**\n * Migrate data from funnel account to real account\n */\n private async migrateFunnelData(\n oldUsername: string,\n newUsername: string\n ): Promise<{ success: boolean; error?: string }> {\n try {\n logger.info(`Starting data migration from ${oldUsername} to ${newUsername}`);\n\n const oldLocalDB = getLocalUserDB(oldUsername);\n const newLocalDB = getLocalUserDB(newUsername);\n\n // Get all docs from funnel account\n const allDocs = await oldLocalDB.allDocs({ include_docs: true });\n\n logger.info(`Found ${allDocs.rows.length} documents in funnel account`);\n\n // Filter out design docs and prepare for migration\n const docsToMigrate = allDocs.rows\n .filter(row => !row.id.startsWith('_design/'))\n .map(row => ({\n ...row.doc,\n _rev: undefined, // Remove rev to insert as new\n }));\n\n if (docsToMigrate.length > 0) {\n await newLocalDB.bulkDocs(docsToMigrate);\n logger.info(`Successfully migrated ${docsToMigrate.length} documents from ${oldUsername} to ${newUsername}`);\n } else {\n logger.info('No documents to migrate from funnel account');\n }\n\n return { success: true };\n } catch (error) {\n logger.error('Migration failed:', error);\n return {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error'\n };\n }\n }\n\n /**\n * Get remote CouchDB root database for authentication operations\n */\n private getRemoteCouchRootDB(): PouchDB.Database {\n const remoteStr: string =\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + 'skuilder';\n\n try {\n return new pouch(remoteStr, {\n skip_setup: true,\n });\n } catch (error) {\n logger.error('Failed to initialize remote CouchDB connection:', error);\n throw new Error(`Failed to initialize CouchDB: ${JSON.stringify(error)}`);\n }\n }\n\n /**\n * Get remote user database for a specific user\n */\n private getUserDB(username: string): PouchDB.Database {\n const guestAccount: boolean = false;\n\n const hexName = hexEncode(username);\n const dbName = `userdb-${hexName}`;\n log(`Fetching user database: ${dbName} (${username})`);\n\n // Odd construction here the result of a bug in the\n // interaction between pouch, pouch-auth.\n // see: https://github.com/pouchdb-community/pouchdb-authentication/issues/239\n const ret = new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,\n createPouchDBConfig()\n );\n\n if (guestAccount) {\n updateGuestAccountExpirationDate(ret);\n }\n\n return ret;\n }\n}\n","import { ENV } from '@db/factory';\nimport {\n DocType,\n DocTypePrefixes,\n GuestUsername,\n log,\n SkuilderCourseData,\n} from '../../core/types/types-legacy';\nimport fetch from 'cross-fetch';\n// import { getCurrentUser } from '../../stores/useAuthStore';\nimport moment, { Moment } from 'moment';\nimport { logger } from '@db/util/logger';\n\nimport pouch from './pouchdb-setup';\n\nimport { ScheduledCard } from '@db/core/types/user';\nimport process from 'process';\n\nconst isBrowser = typeof window !== 'undefined';\n\nif (isBrowser) {\n (window as any).process = process; // required as a fix for pouchdb - see #18\n}\n\nconst expiryDocID: string = 'GuestAccountExpirationDate';\n\nconst GUEST_LOCAL_DB = `userdb-${GuestUsername}`;\nexport const localUserDB: PouchDB.Database = new pouch(GUEST_LOCAL_DB);\n\nexport function hexEncode(str: string): string {\n let hex: string;\n let returnStr: string = '';\n\n for (let i = 0; i < str.length; i++) {\n hex = str.charCodeAt(i).toString(16);\n returnStr += ('000' + hex).slice(3);\n }\n\n return returnStr;\n}\nconst pouchDBincludeCredentialsConfig: PouchDB.Configuration.RemoteDatabaseConfiguration = {\n fetch(url: string | Request, opts: RequestInit): Promise<Response> {\n opts.credentials = 'include';\n\n return (pouch as any).fetch(url, opts);\n },\n} as PouchDB.Configuration.RemoteDatabaseConfiguration;\n\n/**\n * Creates PouchDB configuration with appropriate authentication method\n * - Uses HTTP Basic Auth when credentials are available (Node.js/MCP)\n * - Falls back to cookie auth for browser environments\n */\nexport function createPouchDBConfig(): PouchDB.Configuration.RemoteDatabaseConfiguration {\n // Check if running in Node.js with explicit credentials\n const hasExplicitCredentials = ENV.COUCHDB_USERNAME && ENV.COUCHDB_PASSWORD;\n const isNodeEnvironment = typeof window === 'undefined';\n \n if (hasExplicitCredentials && isNodeEnvironment) {\n // Use HTTP Basic Auth for Node.js environments (MCP server)\n return {\n fetch(url: string | Request, opts: RequestInit = {}): Promise<Response> {\n const basicAuth = btoa(`${ENV.COUCHDB_USERNAME}:${ENV.COUCHDB_PASSWORD}`);\n const headers = new Headers(opts.headers || {});\n headers.set('Authorization', `Basic ${basicAuth}`);\n \n const newOpts = {\n ...opts,\n headers: headers\n };\n \n return (pouch as any).fetch(url, newOpts);\n }\n } as PouchDB.Configuration.RemoteDatabaseConfiguration;\n }\n \n // Use cookie-based auth for browser environments or when no explicit credentials\n return pouchDBincludeCredentialsConfig;\n}\n\nfunction getCouchDB(dbName: string): PouchDB.Database {\n return new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,\n createPouchDBConfig()\n );\n}\n\nexport function getCourseDB(courseID: string): PouchDB.Database {\n // todo: keep a cache of opened courseDBs? need to benchmark this somehow\n return new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + 'coursedb-' + courseID,\n createPouchDBConfig()\n );\n}\n\nexport async function getLatestVersion() {\n try {\n const docs = await getCouchDB('version').allDocs({\n descending: true,\n limit: 1,\n });\n if (docs && docs.rows && docs.rows[0]) {\n return docs.rows[0].id;\n } else {\n return '0.0.0';\n }\n } catch {\n return '-1';\n }\n}\n\n/**\n * Checks the remote couchdb to see if a given username is available\n * @param username The username to be checked\n */\nexport async function usernameIsAvailable(username: string): Promise<boolean> {\n log(`Checking availability of ${username}`);\n \n // Legacy XMLHttpRequest implementation (browser sync)\n // const req = new XMLHttpRequest();\n // const url = ENV.COUCHDB_SERVER_URL + 'userdb-' + hexEncode(username);\n // req.open('HEAD', url, false);\n // req.send();\n // return req.status === 404;\n \n try {\n const url = ENV.COUCHDB_SERVER_URL + 'userdb-' + hexEncode(username);\n const response = await fetch(url, { method: 'HEAD' });\n return response.status === 404;\n } catch (error) {\n log(`Error checking username availability: ${error}`);\n return false;\n }\n}\n\nexport function updateGuestAccountExpirationDate(guestDB: PouchDB.Database<object>) {\n const currentTime = moment.utc();\n const expirationDate: string = currentTime.add(2, 'months').toISOString();\n\n void guestDB\n .get(expiryDocID)\n .then((doc) => {\n return guestDB.put({\n _id: expiryDocID,\n _rev: doc._rev,\n date: expirationDate,\n });\n })\n .catch(() => {\n return guestDB.put({\n _id: expiryDocID,\n date: expirationDate,\n });\n });\n}\n\nexport function getCourseDocs<T extends SkuilderCourseData>(\n courseID: string,\n docIDs: string[],\n options: PouchDB.Core.AllDocsOptions = {}\n) {\n return getCourseDB(courseID).allDocs<T>({\n ...options,\n keys: docIDs,\n });\n}\n\nexport function getCourseDoc<T extends SkuilderCourseData>(\n courseID: string,\n docID: PouchDB.Core.DocumentId,\n options: PouchDB.Core.GetOptions = {}\n): Promise<T> {\n return getCourseDB(courseID).get<T>(docID, options);\n}\n\n/**\n * Returns *all* cards from the parameter courses, in\n * 'qualified' card format (\"courseid-cardid\")\n *\n * @param courseIDs A list of all course_ids to get cards from\n */\nexport async function getRandomCards(courseIDs: string[]) {\n if (courseIDs.length === 0) {\n throw new Error(`getRandomCards:\\n\\tAttempted to get all cards from no courses!`);\n } else {\n const courseResults = await Promise.all(\n courseIDs.map((course) => {\n return getCourseDB(course).find({\n selector: {\n docType: DocType.CARD,\n },\n limit: 1000,\n });\n })\n );\n\n const ret: string[] = [];\n courseResults.forEach((courseCards, index) => {\n courseCards.docs.forEach((doc) => {\n ret.push(`${courseIDs[index]}-${doc._id}`);\n });\n });\n\n return ret;\n }\n}\n\nexport const REVIEW_TIME_FORMAT: string = 'YYYY-MM-DD--kk:mm:ss-SSS';\n\nexport function getCouchUserDB(username: string): PouchDB.Database {\n const guestAccount: boolean = false;\n // console.log(`Getting user db: ${username}`);\n\n const hexName = hexEncode(username);\n const dbName = `userdb-${hexName}`;\n log(`Fetching user database: ${dbName} (${username})`);\n\n // odd construction here the result of a bug in the\n // interaction between pouch, pouch-auth.\n // see: https://github.com/pouchdb-community/pouchdb-authentication/issues/239\n const ret = new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,\n createPouchDBConfig()\n );\n if (guestAccount) {\n updateGuestAccountExpirationDate(ret);\n }\n\n return ret;\n}\n\nexport function scheduleCardReview(review: {\n user: string;\n course_id: string;\n card_id: PouchDB.Core.DocumentId;\n time: Moment;\n scheduledFor: ScheduledCard['scheduledFor'];\n schedulingAgentId: ScheduledCard['schedulingAgentId'];\n}) {\n const now = moment.utc();\n logger.info(`Scheduling for review in: ${review.time.diff(now, 'h') / 24} days`);\n void getCouchUserDB(review.user).put<ScheduledCard>({\n _id: DocTypePrefixes[DocType.SCHEDULED_CARD] + review.time.format(REVIEW_TIME_FORMAT),\n cardId: review.card_id,\n reviewTime: review.time.toISOString(),\n courseId: review.course_id,\n scheduledAt: now.toISOString(),\n scheduledFor: review.scheduledFor,\n schedulingAgentId: review.schedulingAgentId,\n });\n}\n\nexport function filterAllDocsByPrefix<T>(\n db: PouchDB.Database,\n prefix: string,\n opts?: PouchDB.Core.AllDocsOptions\n) {\n // see couchdb docs 6.2.2:\n // Guide to Views -> Views Collation -> String Ranges\n const options: PouchDB.Core.AllDocsWithinRangeOptions = {\n startkey: prefix,\n endkey: prefix + '\\ufff0',\n include_docs: true,\n };\n\n if (opts) {\n Object.assign(options, opts);\n }\n return db.allDocs<T>(options);\n}\n\nexport function getStartAndEndKeys(key: string): {\n startkey: string;\n endkey: string;\n} {\n return {\n startkey: key,\n endkey: key + '\\ufff0',\n };\n}\n\n//////////////////////\n// Package exports\n//////////////////////\n\nexport * from '../../core/interfaces/contentSource';\nexport * from './adminDB';\nexport * from './classroomDB';\nexport * from './courseAPI';\nexport * from './courseDB';\nexport * from './CouchDBSyncStrategy';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAOM,eAEO;AATb;AAAA;AAAA;AAOA,IAAM,gBAAgB,OAAO,YAAY,eAAe,QAAQ,IAAI,aAAa;AAE1E,IAAM,SAAS;AAAA;AAAA;AAAA;AAAA,MAIpB,OAAO,CAAC,YAAoB,SAAsB;AAChD,YAAI,eAAe;AAEjB,kBAAQ,MAAM,cAAc,OAAO,IAAI,GAAG,IAAI;AAAA,QAChD;AAAA,MACF;AAAA;AAAA;AAAA;AAAA,MAKA,MA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B;AAEA;AAGA,qBAAoB;AA6QpB;AACA,IAAAC;AACA,IAAAC;AACA;AACA;AACA;AAhRA,IAAM,YAAY,OAAO,WAAW;AAEpC,QAAI,WAAW;AACb,MAAC,OAAe,UAAU,eAAAC;AAAA,IAC5B;AAEA,IAAM,cAAsB;AAE5B,IAAM,iBAAiB,UAAU,aAAa;AACvC,IAAM,cAAgC,IAAI,sBAAM,cAAc;AAarE,IAAM,kCAAqF;AAAA,MACzF,MAAM,KAAuB,MAAsC;AACjE,aAAK,cAAc;AAEnB,eAAQ,sBAAc,MAAM,KAAK,IAAI;AAAA,MACvC;AAAA,IACF;AAiKO,IAAM,qBAA6B;AAAA;AAAA;","names":["PouchDB","PouchDBFind","PouchDBAuth","uuidv4","import_common","AggregationMode","import_common","import_common","types_exports","init_types","import_common","import_common","DEFAULT_MIN_COUNT","moment","Navigators","NavigatorRole","module","getCourseDB","updateCardElo","import_common","import_moment","init_classroomDB","moment","getCourseDB","import_common","import_common","init_classroomDB","init_courseDB","init_courseDB","import_common","init_types","init_types","filterAllDocsByPrefix","getStartAndEndKeys","moment","expiryDocID","REVIEW_TIME_FORMAT","log","import_moment","import_moment","moment","uuid","log","import_common","import_moment","getStartAndEndKeys","moment","REVIEW_TIME_FORMAT","record","filterAllDocsByPrefix","init_adminDB","init_classroomDB","fetch","import_common","log","getCourseDB","hexEncode","updateCardElo","updateGuestAccountExpirationDate","fetch","moment","import_cross_fetch","import_moment","init_adminDB","init_classroomDB","process"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/impl/common/SyncStrategy.ts","../../../src/core/interfaces/adminDB.ts","../../../src/core/interfaces/classroomDB.ts","../../../src/util/logger.ts","../../../src/impl/couch/pouchdb-setup.ts","../../../src/util/Loggable.ts","../../../src/impl/couch/updateQueue.ts","../../../src/core/types/types-legacy.ts","../../../src/impl/couch/clientCache.ts","../../../src/impl/couch/courseAPI.ts","../../../src/impl/couch/courseLookupDB.ts","../../../src/core/navigators/index.ts","../../../src/core/navigators/Pipeline.ts","../../../src/core/navigators/generators/CompositeGenerator.ts","../../../src/core/navigators/PipelineAssembler.ts","../../../src/core/navigators/generators/elo.ts","../../../src/core/navigators/generators/srs.ts","../../../src/core/navigators/filters/eloDistance.ts","../../../src/core/navigators/defaults.ts","../../../src/impl/couch/courseDB.ts","../../../src/impl/couch/classroomDB.ts","../../../src/study/TagFilteredContentSource.ts","../../../src/core/interfaces/contentSource.ts","../../../src/core/interfaces/courseDB.ts","../../../src/core/interfaces/dataLayerProvider.ts","../../../src/core/interfaces/userDB.ts","../../../src/core/interfaces/index.ts","../../../src/core/types/user.ts","../../../src/core/types/strategyState.ts","../../../src/core/util/index.ts","../../../src/core/bulkImport/cardProcessor.ts","../../../src/core/bulkImport/types.ts","../../../src/core/bulkImport/index.ts","../../../src/core/index.ts","../../../src/util/dataDirectory.ts","../../../src/impl/common/userDBHelpers.ts","../../../src/impl/couch/user-course-relDB.ts","../../../src/impl/common/BaseUserDB.ts","../../../src/impl/common/index.ts","../../../src/factory.ts","../../../src/impl/couch/adminDB.ts","../../../src/impl/couch/auth.ts","../../../src/impl/couch/CouchDBSyncStrategy.ts","../../../src/impl/couch/index.ts"],"sourcesContent":["// packages/db/src/impl/common/SyncStrategy.ts\n\nimport type { AccountCreationResult, AuthenticationResult } from './types';\n\n/**\n * Strategy interface for handling user data synchronization\n * Different implementations handle remote sync vs local-only storage\n */\nexport interface SyncStrategy {\n /**\n * Set up the remote database for a user\n * @param username The username to set up remote DB for\n * @returns PouchDB database instance (may be same as local for no-op)\n */\n setupRemoteDB(username: string): PouchDB.Database;\n\n /**\n * Get the database to use for write operations (local-first approach)\n * @param username The username to get write DB for\n * @returns PouchDB database instance for write operations\n */\n getWriteDB?(username: string): PouchDB.Database;\n\n /**\n * Start synchronization between local and remote databases\n * @param localDB The local PouchDB instance\n * @param remoteDB The remote PouchDB instance\n */\n startSync(localDB: PouchDB.Database, remoteDB: PouchDB.Database): void;\n\n /**\n * Stop synchronization (optional - for cleanup)\n */\n stopSync?(): void;\n\n /**\n * Whether this strategy supports account creation\n */\n canCreateAccount(): boolean;\n\n /**\n * Whether this strategy supports authentication\n */\n canAuthenticate(): boolean;\n\n /**\n * Create a new user account (if supported)\n * @param username The username for the new account\n * @param password The password for the new account\n */\n createAccount?(username: string, password: string): Promise<AccountCreationResult>;\n\n /**\n * Authenticate a user (if supported)\n * @param username The username to authenticate\n * @param password The password to authenticate with\n */\n authenticate?(username: string, password: string): Promise<AuthenticationResult>;\n\n /**\n * Log out the current user (if supported)\n */\n logout?(): Promise<AuthenticationResult>;\n\n /**\n * Get the current logged-in username\n * Returns the username if logged in, or a default guest username\n */\n getCurrentUsername(): Promise<string>;\n}\n\n/**\n * Base class for sync strategies with common functionality\n */\nexport abstract class BaseSyncStrategy implements SyncStrategy {\n abstract setupRemoteDB(username: string): PouchDB.Database;\n abstract startSync(localDB: PouchDB.Database, remoteDB: PouchDB.Database): void;\n abstract canCreateAccount(): boolean;\n abstract canAuthenticate(): boolean;\n abstract getCurrentUsername(): Promise<string>;\n\n stopSync?(): void {\n // Default no-op implementation\n }\n\n async createAccount(_username: string, _password: string): Promise<AccountCreationResult> {\n throw new Error('Account creation not supported by this sync strategy');\n }\n\n async authenticate(_username: string, _password: string): Promise<AuthenticationResult> {\n throw new Error('Authentication not supported by this sync strategy');\n }\n\n async logout(): Promise<AuthenticationResult> {\n throw new Error('Logout not supported by this sync strategy');\n }\n}\n","import { ClassroomConfig, CourseConfig } from '@vue-skuilder/common';\n\n/**\n * Admin functionality\n */\nexport interface AdminDBInterface {\n /**\n * Get all users\n */\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n getUsers(): Promise<PouchDB.Core.Document<{}>[]>;\n\n /**\n * Get all courses\n */\n getCourses(): Promise<CourseConfig[]>;\n\n /**\n * Remove a course\n */\n removeCourse(id: string): Promise<PouchDB.Core.Response>;\n\n /**\n * Get all classrooms\n */\n getClassrooms(): Promise<(ClassroomConfig & { _id: string })[]>;\n}\n","import { ClassroomConfig } from '@vue-skuilder/common';\n\n/**\n * Classroom management\n */\nexport interface ClassroomDBInterface {\n /**\n * Get classroom config\n */\n getConfig(): ClassroomConfig;\n\n /**\n * Get assigned content\n */\n getAssignedContent(): Promise<AssignedContent[]>;\n}\n\nexport interface TeacherClassroomDBInterface extends ClassroomDBInterface {\n /**\n * For teacher interfaces: assign content\n */\n assignContent?(content: AssignedContent): Promise<boolean>;\n\n /**\n * For teacher interfaces: remove content\n */\n removeContent?(content: AssignedContent): Promise<void>;\n}\n\n/**\n * Student-facing classroom interface.\n * Content is accessed via StudyContentSource.getWeightedCards().\n */\nexport type StudentClassroomDBInterface = ClassroomDBInterface;\n\nexport type AssignedContent = AssignedCourse | AssignedTag | AssignedCard;\n\nexport interface AssignedTag extends ContentBase {\n type: 'tag';\n tagID: string;\n}\nexport interface AssignedCourse extends ContentBase {\n type: 'course';\n}\nexport interface AssignedCard extends ContentBase {\n type: 'card';\n cardID: string;\n}\n\ninterface ContentBase {\n type: 'course' | 'tag' | 'card';\n /**\n * Username of the assigning teacher.\n */\n assignedBy: string;\n /**\n * Date the content was assigned.\n */\n assignedOn: moment.Moment;\n /**\n * A 'due' date for this assigned content, for scheduling content\n * in advance. Content will not be actively pushed to students until\n * this date.\n */\n activeOn: moment.Moment;\n courseID: string;\n}\n","/**\n * Simple logging utility for @vue-skuilder/db package\n *\n * This utility provides environment-aware logging with ESLint suppressions\n * to resolve console statement violations while maintaining logging functionality.\n */\n\nconst isDevelopment = typeof process !== 'undefined' && process.env.NODE_ENV === 'development';\n\nexport const logger = {\n /**\n * Debug-level logging - only shown in development\n */\n debug: (message: string, ...args: any[]): void => {\n if (isDevelopment) {\n // eslint-disable-next-line no-console\n console.debug(`[DB:DEBUG] ${message}`, ...args);\n }\n },\n\n /**\n * Info-level logging - general information\n */\n info: (message: string, ...args: any[]): void => {\n // eslint-disable-next-line no-console\n console.info(`[DB:INFO] ${message}`, ...args);\n },\n\n /**\n * Warning-level logging - potential issues\n */\n warn: (message: string, ...args: any[]): void => {\n // eslint-disable-next-line no-console\n console.warn(`[DB:WARN] ${message}`, ...args);\n },\n\n /**\n * Error-level logging - serious problems\n */\n error: (message: string, ...args: any[]): void => {\n // eslint-disable-next-line no-console\n console.error(`[DB:ERROR] ${message}`, ...args);\n },\n\n /**\n * Log function for backward compatibility with existing log() usage\n */\n log: (message: string, ...args: any[]): void => {\n if (isDevelopment) {\n // eslint-disable-next-line no-console\n console.log(`[DB:LOG] ${message}`, ...args);\n }\n },\n};\n","import PouchDB from 'pouchdb';\nimport PouchDBFind from 'pouchdb-find';\nimport PouchDBAuth from '@nilock2/pouchdb-authentication';\n\n// Register plugins\nPouchDB.plugin(PouchDBFind);\nPouchDB.plugin(PouchDBAuth);\n\n// Disable PouchDB debug logging to prevent interference with CLI prompts\n// Debug logging (like DerivedLogger.emit) will still go to the TUI log file\n// if initializeTuiLogging() has been called, but won't clutter terminal output\nif (typeof PouchDB.debug !== 'undefined') {\n PouchDB.debug.disable();\n}\n\n// Configure PouchDB globally\nPouchDB.defaults({\n // ajax: {\n // timeout: 60000,\n // },\n});\n\nexport default PouchDB;\n","export abstract class Loggable {\n protected abstract readonly _className: string;\n protected log(...args: unknown[]): void {\n // eslint-disable-next-line no-console\n console.log(`LOG-${this._className}@${new Date()}:`, ...args);\n }\n protected error(...args: unknown[]): void {\n // eslint-disable-next-line no-console\n console.error(`ERROR-${this._className}@${new Date()}:`, ...args);\n }\n}\n","import { Loggable } from '../../util/Loggable';\nimport { logger } from '../../util/logger';\n\nexport type Update<T> = Partial<T> | ((x: T) => T);\n\nexport default class UpdateQueue extends Loggable {\n _className: string = 'UpdateQueue';\n private pendingUpdates: {\n [index: string]: Update<unknown>[];\n } = {};\n private inprogressUpdates: {\n [index: string]: boolean;\n } = {};\n\n private readDB: PouchDB.Database; // Database for read operations\n private writeDB: PouchDB.Database; // Database for write operations (local-first)\n\n /**\n * Queues an update for a document and applies it with conflict resolution.\n *\n * @param id - Document ID to update\n * @param update - Partial object or function that transforms the document\n * @returns Promise resolving to the updated document\n *\n * @throws {PouchError} with status 404 if document doesn't exist\n *\n * @remarks\n * **Error Handling Pattern:**\n * - This method does NOT create documents if they don't exist\n * - Callers are responsible for handling 404 errors and creating documents\n * - This design maintains separation of concerns (UpdateQueue handles conflicts, callers handle lifecycle)\n *\n * @example\n * ```typescript\n * try {\n * await updateQueue.update(docId, (doc) => ({ ...doc, field: newValue }));\n * } catch (e) {\n * if ((e as PouchError).status === 404) {\n * // Create the document with initial values\n * await db.put({ _id: docId, field: newValue, ...initialFields });\n * }\n * }\n * ```\n */\n public update<T extends PouchDB.Core.Document<object>>(\n id: PouchDB.Core.DocumentId,\n update: Update<T>\n ) {\n logger.debug(`Update requested on doc: ${id}`);\n if (this.pendingUpdates[id]) {\n this.pendingUpdates[id].push(update);\n } else {\n this.pendingUpdates[id] = [update];\n }\n return this.applyUpdates<T>(id);\n }\n\n constructor(readDB: PouchDB.Database, writeDB?: PouchDB.Database) {\n super();\n // PouchDB.debug.enable('*');\n this.readDB = readDB;\n this.writeDB = writeDB || readDB; // Default to readDB if writeDB not provided\n logger.debug(`UpdateQ initialized...`);\n void this.readDB.info().then((i) => {\n logger.debug(`db info: ${JSON.stringify(i)}`);\n });\n }\n\n private async applyUpdates<T extends PouchDB.Core.Document<object>>(\n id: string\n ): Promise<T & PouchDB.Core.GetMeta & PouchDB.Core.RevisionIdMeta> {\n logger.debug(`Applying updates on doc: ${id}`);\n if (this.inprogressUpdates[id]) {\n // Poll instead of recursing to avoid infinite recursion\n while (this.inprogressUpdates[id]) {\n await new Promise(resolve => setTimeout(resolve, Math.random() * 50));\n }\n return this.applyUpdates<T>(id);\n } else {\n if (this.pendingUpdates[id] && this.pendingUpdates[id].length > 0) {\n this.inprogressUpdates[id] = true;\n\n const MAX_RETRIES = 5;\n for (let i = 0; i < MAX_RETRIES; i++) {\n try {\n const doc = await this.readDB.get<T>(id);\n\n // Create a new doc object to apply updates to for this attempt\n let updatedDoc = { ...doc };\n\n // Note: This loop is not fully safe if updates are functions that depend on a specific doc state\n // that might change between retries. But for simple object merges, it's okay.\n const updatesToApply = [...this.pendingUpdates[id]];\n for (const update of updatesToApply) {\n if (typeof update === 'function') {\n updatedDoc = { ...updatedDoc, ...update(updatedDoc) };\n } else {\n updatedDoc = {\n ...updatedDoc,\n ...update,\n };\n }\n }\n\n await this.writeDB.put<T>(updatedDoc);\n\n // Success! Remove the updates we just applied.\n this.pendingUpdates[id].splice(0, updatesToApply.length);\n\n if (this.pendingUpdates[id].length === 0) {\n this.inprogressUpdates[id] = false;\n delete this.inprogressUpdates[id];\n } else {\n // More updates came in, run again.\n return this.applyUpdates<T>(id);\n }\n return updatedDoc as any; // success, exit loop and function\n } catch (e: any) {\n if (e.name === 'conflict' && i < MAX_RETRIES - 1) {\n logger.warn(`Conflict on update for doc ${id}, retry #${i + 1}`);\n await new Promise((res) => setTimeout(res, 50 * Math.random()));\n // continue to next iteration of the loop\n } else if (e.name === 'not_found' && i === 0) {\n // Document not present - throw to caller for initialization\n logger.warn(`Update failed for ${id} - does not exist. Throwing to caller.`);\n delete this.inprogressUpdates[id];\n throw e; // Let caller handle\n } else {\n // Max retries reached or a non-conflict error\n delete this.inprogressUpdates[id];\n if (this.pendingUpdates[id]) {\n delete this.pendingUpdates[id];\n }\n logger.error(`Error on attemped update (retry ${i}): ${JSON.stringify(e)}`);\n throw e; // Let caller handle\n }\n }\n }\n // This should be unreachable, but it satisfies the compiler that a value is always returned or an error thrown.\n throw new Error(`UpdateQueue failed for doc ${id} after ${MAX_RETRIES} retries.`);\n } else {\n throw new Error(`Empty Updates Queue Triggered`);\n }\n }\n }\n}\n","import { CourseElo, Answer, Evaluation } from '@vue-skuilder/common';\nimport { Moment } from 'moment';\nimport { logger } from '../../util/logger';\n\nexport const GuestUsername: string = 'sk-guest-';\n\nexport const log = (message: string): void => {\n logger.log(message);\n};\n\nexport enum DocType {\n DISPLAYABLE_DATA = 'DISPLAYABLE_DATA',\n CARD = 'CARD',\n DATASHAPE = 'DATASHAPE',\n QUESTIONTYPE = 'QUESTION',\n VIEW = 'VIEW',\n PEDAGOGY = 'PEDAGOGY',\n CARDRECORD = 'CARDRECORD',\n SCHEDULED_CARD = 'SCHEDULED_CARD',\n TAG = 'TAG',\n NAVIGATION_STRATEGY = 'NAVIGATION_STRATEGY',\n STRATEGY_STATE = 'STRATEGY_STATE',\n}\n\nexport interface QualifiedCardID {\n courseID: string;\n cardID: string;\n}\n\n/**\n * Interface for all data on course content and pedagogy stored\n * in the c/pouch database.\n */\nexport interface SkuilderCourseData {\n course: string;\n docType: DocType;\n}\n\nexport interface Tag extends SkuilderCourseData {\n docType: DocType.TAG;\n name: string;\n snippet: string; // 200 char description of the tag\n wiki: string; // 3000 char md-friendly description\n taggedCards: PouchDB.Core.DocumentId[];\n author: string;\n}\nexport interface TagStub {\n name: string;\n snippet: string;\n count: number; // the number of cards that have this tag applied\n}\n\nexport interface CardData extends SkuilderCourseData {\n docType: DocType.CARD;\n id_displayable_data: PouchDB.Core.DocumentId[];\n id_view: PouchDB.Core.DocumentId;\n elo: CourseElo;\n author: string;\n}\n\n/** A list of populated courses in the DB */\nexport interface CourseListData extends PouchDB.Core.Response {\n courses: string[];\n}\n\n/**\n * The data used to hydrate viewable components (questions, info, etc)\n */\nexport interface DisplayableData extends SkuilderCourseData {\n docType: DocType.DISPLAYABLE_DATA;\n author?: string;\n id_datashape: PouchDB.Core.DocumentId;\n data: Field[];\n _attachments?: { [index: string]: PouchDB.Core.FullAttachment };\n}\n\nexport interface Field {\n data: unknown;\n name: string;\n}\n\nexport interface DataShapeData extends SkuilderCourseData {\n docType: DocType.DATASHAPE;\n _id: PouchDB.Core.DocumentId;\n questionTypes: PouchDB.Core.DocumentId[];\n}\n\nexport interface QuestionData extends SkuilderCourseData {\n docType: DocType.QUESTIONTYPE;\n _id: PouchDB.Core.DocumentId;\n viewList: string[];\n dataShapeList: PouchDB.Core.DocumentId[];\n}\n\nexport const DocTypePrefixes = {\n [DocType.CARD]: 'c',\n [DocType.DISPLAYABLE_DATA]: 'dd',\n [DocType.TAG]: 'TAG',\n [DocType.CARDRECORD]: 'cardH',\n [DocType.SCHEDULED_CARD]: 'card_review_',\n // Add other doctypes here as they get prefixed IDs\n [DocType.DATASHAPE]: 'DATASHAPE',\n [DocType.QUESTIONTYPE]: 'QUESTION',\n [DocType.VIEW]: 'VIEW',\n [DocType.PEDAGOGY]: 'PEDAGOGY',\n [DocType.NAVIGATION_STRATEGY]: 'NAVIGATION_STRATEGY',\n [DocType.STRATEGY_STATE]: 'STRATEGY_STATE',\n} as const;\n\nexport interface CardHistory<T extends CardRecord> {\n _id: PouchDB.Core.DocumentId;\n /**\n * The CouchDB id of the card\n */\n cardID: PouchDB.Core.DocumentId;\n\n /**\n * The ID of the course\n */\n courseID: string;\n\n /**\n * The to-date largest interval between successful\n * card reviews. `0` indicates no successful reviews.\n */\n bestInterval: number;\n\n /**\n * The number of times that a card has been\n * failed in review\n */\n lapses: number;\n\n /**\n * The number of consecutive successful impressions\n * on this card\n */\n streak: number;\n\n records: T[];\n}\n\nexport interface CardRecord {\n /**\n * The CouchDB id of the card\n */\n cardID: string;\n /**\n * The ID of the course\n */\n courseID: string;\n /**\n * Number of milliseconds that the user spent before dismissing\n * the card (ie, \"I've read this\" or \"here is my answer\")\n *\n * //TODO: this (sometimes?) wants to be replaced with a rich\n * recording of user activity in working the question\n */\n timeSpent: number;\n /**\n * The date-time that the card was rendered. timeStamp + timeSpent will give the\n * time of user submission.\n */\n timeStamp: Moment;\n}\n\nexport interface QuestionRecord extends CardRecord, Evaluation {\n userAnswer: Answer;\n /**\n * The number of incorrect user submissions prededing this submisstion.\n *\n * eg, if a user is asked 7*6=__, submitting 46, 48, 42 will result in three\n * records being created having 0, 1, and 2 as their recorded 'priorAttempts' values\n */\n priorAttemps: number;\n}\n","// todo: something good here instead\n\nconst CLIENT_CACHE: {\n [k: string]: unknown;\n} = {};\n\nexport async function GET_CACHED<K>(k: string, f?: (x: string) => Promise<K>): Promise<K> {\n if (CLIENT_CACHE[k]) {\n // console.log('returning a cached item');\n return CLIENT_CACHE[k] as K;\n }\n\n CLIENT_CACHE[k] = f ? await f(k) : await GET_ITEM(k);\n return GET_CACHED(k);\n}\n\nasync function GET_ITEM(k: string): Promise<unknown> {\n throw new Error(`No implementation found for GET_CACHED(${k})`);\n}\n","import pouch from './pouchdb-setup';\nimport { createPouchDBConfig } from '.';\nimport { ENV } from '@db/factory';\n// import { DataShape } from '../..base-course/Interfaces/DataShape';\nimport { NameSpacer, ShapeDescriptor } from '@vue-skuilder/common';\nimport { CourseConfig, DataShape } from '@vue-skuilder/common';\nimport { CourseElo, blankCourseElo, toCourseElo } from '@vue-skuilder/common';\nimport { CourseDB, createTag } from './courseDB';\nimport { CardData, DisplayableData, DocType, Tag, DocTypePrefixes } from '../../core/types/types-legacy';\nimport { prepareNote55 } from '@vue-skuilder/common';\nimport { BaseUser } from '../common';\nimport { logger } from '@db/util/logger';\nimport { v4 as uuidv4 } from 'uuid';\n\n/**\n *\n * @param courseID id of the course (quilt) being added to\n * @param codeCourse\n * @param shape\n * @param data the datashape data - data required for this shape\n * @param author\n * @param uploads optional additional media uploads: img0, img1, ..., aud0, aud1,...\n * @returns\n */\nexport async function addNote55(\n courseID: string,\n codeCourse: string,\n shape: DataShape,\n data: unknown,\n author: string,\n tags: string[],\n uploads?: { [x: string]: PouchDB.Core.FullAttachment },\n elo: CourseElo = blankCourseElo()\n): Promise<PouchDB.Core.Response> {\n const db = getCourseDB(courseID);\n const payload = prepareNote55(courseID, codeCourse, shape, data, author, tags, uploads);\n const _id = `${DocTypePrefixes[DocType.DISPLAYABLE_DATA]}-${uuidv4()}`;\n const result = await db.put<DisplayableData>({ ...payload, _id });\n\n const dataShapeId = NameSpacer.getDataShapeString({\n course: codeCourse,\n dataShape: shape.name,\n });\n\n if (result.ok) {\n try {\n // create cards\n await createCards(courseID, dataShapeId, result.id, tags, elo, author);\n } catch (error) {\n // Handle CouchDB errors which often have a 'reason' property\n let errorMessage = 'Unknown error';\n if (error instanceof Error) {\n errorMessage = error.message;\n } else if (error && typeof error === 'object' && 'reason' in error) {\n errorMessage = error.reason as string;\n } else if (error && typeof error === 'object' && 'message' in error) {\n errorMessage = error.message as string;\n } else {\n errorMessage = String(error);\n }\n\n logger.error(`[addNote55] Failed to create cards for note ${result.id}: ${errorMessage}`);\n // Add info to result to indicate card creation failed\n (result as any).cardCreationFailed = true;\n (result as any).cardCreationError = errorMessage;\n }\n } else {\n logger.error(`[addNote55] Error adding note. Result: ${JSON.stringify(result)}`);\n }\n\n return result;\n}\n\nasync function createCards(\n courseID: string,\n datashapeID: PouchDB.Core.DocumentId,\n noteID: PouchDB.Core.DocumentId,\n tags: string[],\n elo: CourseElo = blankCourseElo(),\n author: string\n): Promise<void> {\n const cfg = await getCredentialledCourseConfig(courseID);\n const dsDescriptor = NameSpacer.getDataShapeDescriptor(datashapeID);\n let questionViewTypes: string[] = [];\n\n for (const ds of cfg.dataShapes) {\n if (ds.name === datashapeID) {\n questionViewTypes = ds.questionTypes;\n }\n }\n\n if (questionViewTypes.length === 0) {\n const errorMsg = `No questionViewTypes found for datashapeID: ${datashapeID} in course config. Cards cannot be created.`;\n logger.error(errorMsg);\n throw new Error(errorMsg);\n }\n\n for (const questionView of questionViewTypes) {\n await createCard(questionView, courseID, dsDescriptor, noteID, tags, elo, author);\n }\n}\n\nasync function createCard(\n questionViewName: string,\n courseID: string,\n dsDescriptor: ShapeDescriptor,\n noteID: string,\n tags: string[],\n elo: CourseElo = blankCourseElo(),\n author: string\n): Promise<void> {\n const qDescriptor = NameSpacer.getQuestionDescriptor(questionViewName);\n const cfg = await getCredentialledCourseConfig(courseID);\n\n for (const rQ of cfg.questionTypes) {\n if (rQ.name === questionViewName) {\n for (const view of rQ.viewList) {\n await addCard(\n courseID,\n dsDescriptor.course,\n [noteID],\n NameSpacer.getViewString({\n course: qDescriptor.course,\n questionType: qDescriptor.questionType,\n view,\n }),\n elo,\n tags,\n author\n );\n }\n }\n }\n}\n\n/**\n *\n * Adds a card to the DB. This function is called\n * as a side effect of adding either a View or\n * DisplayableData item.\n * @param course The name of the course that the card belongs to\n * @param id_displayable_data C/PouchDB ID of the data used to hydrate the view\n * @param id_view C/PouchDB ID of the view used to display the card\n *\n * @package\n */\nasync function addCard(\n courseID: string,\n course: string,\n id_displayable_data: PouchDB.Core.DocumentId[],\n id_view: PouchDB.Core.DocumentId,\n elo: CourseElo,\n tags: string[],\n author: string\n): Promise<PouchDB.Core.Response> {\n const db = getCourseDB(courseID);\n const _id = `${DocTypePrefixes[DocType.CARD]}-${uuidv4()}`;\n const card = await db.put<CardData>({\n _id,\n course,\n id_displayable_data,\n id_view,\n docType: DocType.CARD,\n elo: elo || toCourseElo(990 + Math.round(20 * Math.random())),\n author,\n });\n for (const tag of tags) {\n logger.info(`adding tag: ${tag} to card ${card.id}`);\n await addTagToCard(courseID, card.id, tag, author, false);\n }\n return card;\n}\n\nexport async function getCredentialledCourseConfig(courseID: string): Promise<CourseConfig> {\n try {\n const db = getCourseDB(courseID);\n const ret = await db.get<CourseConfig>('CourseConfig');\n ret.courseID = courseID;\n logger.info(`Returning course config: ${JSON.stringify(ret)}`);\n return ret;\n } catch (e) {\n logger.error(`Error fetching config for ${courseID}:`, e);\n throw e;\n }\n}\n\n/**\n Assciates a tag with a card.\n\n NB: DB stores tags as separate documents, with a list of card IDs.\n Consider renaming to `addCardToTag` to reflect this.\n\n NB: tags are created if they don't already exist\n\n @param updateELO whether to update the ELO of the card with the new tag. Default true.\n @package\n*/\nexport async function addTagToCard(\n courseID: string,\n cardID: string,\n tagID: string,\n author: string,\n updateELO: boolean = true\n): Promise<PouchDB.Core.Response> {\n // todo: possible future perf. hit if tags have large #s of taggedCards.\n // In this case, should be converted to a server-request\n const prefixedTagID = getTagID(tagID);\n const courseDB = getCourseDB(courseID);\n const courseApi = new CourseDB(courseID, async () => {\n const dummySyncStrategy = {\n setupRemoteDB: () => null as any,\n startSync: () => {},\n canCreateAccount: () => false,\n canAuthenticate: () => false,\n getCurrentUsername: async () => 'DummyUser',\n };\n return BaseUser.Dummy(dummySyncStrategy);\n });\n try {\n logger.info(`Applying tag ${tagID} to card ${courseID + '-' + cardID}...`);\n const tag = await courseDB.get<Tag>(prefixedTagID);\n if (!tag.taggedCards.includes(cardID)) {\n tag.taggedCards.push(cardID);\n\n if (updateELO) {\n try {\n const eloData = await courseApi.getCardEloData([cardID]);\n const elo = eloData[0];\n elo.tags[tagID] = {\n count: 0,\n score: elo.global.score, // todo: or 1000?\n };\n await updateCardElo(courseID, cardID, elo);\n } catch (error) {\n logger.error('Failed to update ELO data for card:', cardID, error);\n }\n }\n\n return courseDB.put<Tag>(tag);\n } else throw new AlreadyTaggedErr(`Card ${cardID} is already tagged with ${tagID}`);\n } catch (e) {\n if (e instanceof AlreadyTaggedErr) {\n throw e;\n }\n\n await createTag(courseID, tagID, author);\n return addTagToCard(courseID, cardID, tagID, author, updateELO);\n }\n}\n\nasync function updateCardElo(courseID: string, cardID: string, elo: CourseElo) {\n if (elo) {\n // checking against null, undefined, NaN\n const cDB = getCourseDB(courseID);\n const card = await cDB.get<CardData>(cardID);\n logger.debug(`Replacing ${JSON.stringify(card.elo)} with ${JSON.stringify(elo)}`);\n card.elo = elo;\n return cDB.put(card); // race conditions - is it important? probably not (net-zero effect)\n }\n}\n\nclass AlreadyTaggedErr extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'AlreadyTaggedErr';\n }\n}\n\nexport function getTagID(tagName: string): string {\n const tagPrefix = DocType.TAG.valueOf() + '-';\n if (tagName.indexOf(tagPrefix) === 0) {\n return tagName;\n } else {\n return tagPrefix + tagName;\n }\n}\n\nexport function getCourseDB(courseID: string): PouchDB.Database {\n const dbName = `coursedb-${courseID}`;\n return new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,\n createPouchDBConfig()\n );\n}\n","import pouch from './pouchdb-setup';\nimport { ENV } from '@db/factory';\nimport { logger } from '../../util/logger';\n\nconst courseLookupDBTitle = 'coursedb-lookup';\n\ninterface CourseLookupDoc {\n _id: string;\n _rev: string;\n name: string;\n disambiguator?: string;\n}\n\nlogger.debug(`COURSELOOKUP FILE RUNNING`);\n\n/**\n * A Lookup table of existant courses. Each docID in this DB correspondes to a\n * course database whose name is `coursedb-{docID}`\n */\nexport default class CourseLookup {\n // [ ] this db should be read only for public, admin-only for write\n // Cache for the PouchDB instance\n private static _dbInstance: PouchDB.Database | null = null;\n\n /**\n * Static getter for the PouchDB database instance.\n * Connects using ENV variables and caches the instance.\n * Throws an error if required ENV variables are not set.\n */\n private static get _db(): PouchDB.Database {\n // Return cached instance if available\n if (this._dbInstance) {\n return this._dbInstance;\n }\n\n // --- Check required environment variables ---\n if (ENV.COUCHDB_SERVER_URL === 'NOT_SET' || !ENV.COUCHDB_SERVER_URL) {\n throw new Error(\n 'CourseLookup.db: COUCHDB_SERVER_URL is not set. Ensure initializeDataLayer has been called with valid configuration.'\n );\n }\n if (ENV.COUCHDB_SERVER_PROTOCOL === 'NOT_SET' || !ENV.COUCHDB_SERVER_PROTOCOL) {\n throw new Error(\n 'CourseLookup.db: COUCHDB_SERVER_PROTOCOL is not set. Ensure initializeDataLayer has been called with valid configuration.'\n );\n }\n\n // --- Construct connection options ---\n const dbUrl = `${ENV.COUCHDB_SERVER_PROTOCOL}://${ENV.COUCHDB_SERVER_URL}/${courseLookupDBTitle}`;\n const options: PouchDB.Configuration.RemoteDatabaseConfiguration = {\n // fetch: (url, opts) => { // Optional: Add for debugging network requests\n // console.log('PouchDB fetch:', url, opts);\n // return pouch.fetch(url, opts);\n // }\n };\n\n // Add authentication if both username and password are provided\n if (ENV.COUCHDB_USERNAME && ENV.COUCHDB_PASSWORD) {\n options.auth = {\n username: ENV.COUCHDB_USERNAME,\n password: ENV.COUCHDB_PASSWORD,\n };\n logger.info(`CourseLookup: Connecting to ${dbUrl} with authentication.`);\n } else {\n logger.info(`CourseLookup: Connecting to ${dbUrl} without authentication.`);\n }\n\n // --- Create and cache the PouchDB instance ---\n try {\n this._dbInstance = new pouch(dbUrl, options);\n logger.info(`CourseLookup: Database instance created for ${courseLookupDBTitle}.`);\n return this._dbInstance;\n } catch (error) {\n logger.error(`CourseLookup: Failed to create PouchDB instance for ${dbUrl}`, error);\n // Reset cache attempt on failure\n this._dbInstance = null;\n // Re-throw the error to indicate connection failure\n throw new Error(\n `CourseLookup: Failed to initialize database connection: ${\n error instanceof Error ? error.message : String(error)\n }`\n );\n }\n }\n\n /**\n * Adds a new course to the lookup database, and returns the courseID\n * @param courseName\n * @returns\n */\n static async add(courseName: string): Promise<string> {\n const resp = await CourseLookup._db.post({\n name: courseName,\n });\n\n return resp.id;\n }\n\n /**\n * Adds a new course to the lookup database with a specific courseID\n * @param courseId The specific course ID to use\n * @param courseName The course name\n * @param disambiguator Optional disambiguator\n * @returns Promise<void>\n */\n static async addWithId(\n courseId: string,\n courseName: string,\n disambiguator?: string\n ): Promise<void> {\n const doc: Omit<CourseLookupDoc, '_rev'> = {\n _id: courseId,\n name: courseName,\n };\n\n if (disambiguator) {\n doc.disambiguator = disambiguator;\n }\n\n await CourseLookup._db.put(doc);\n }\n\n /**\n * Removes a course from the index\n * @param courseID\n */\n static async delete(courseID: string): Promise<PouchDB.Core.Response> {\n const doc = await CourseLookup._db.get(courseID);\n return await CourseLookup._db.remove(doc);\n }\n\n // [ ] rename to allCourses()\n static async allCourseWare(): Promise<CourseLookupDoc[]> {\n const resp = await CourseLookup._db.allDocs<CourseLookupDoc>({\n include_docs: true,\n });\n\n return resp.rows.map((row) => row.doc!);\n }\n\n static async updateDisambiguator(\n courseID: string,\n disambiguator?: string\n ): Promise<PouchDB.Core.Response> {\n const doc = await CourseLookup._db.get<CourseLookupDoc>(courseID);\n doc.disambiguator = disambiguator;\n return await CourseLookup._db.put(doc);\n }\n\n static async isCourse(courseID: string): Promise<boolean> {\n try {\n await CourseLookup._db.get(courseID);\n return true;\n } catch (error) {\n logger.info(`Courselookup failed:`, error);\n return false;\n }\n }\n}\n","import { StudyContentSource, UserDBInterface, CourseDBInterface } from '..';\n\n// Re-export filter types\nexport type { CardFilter, FilterContext, CardFilterFactory } from './filters/types';\n\n// Re-export generator types\nexport type { CardGenerator, GeneratorContext, CardGeneratorFactory } from './generators/types';\n\nimport { ContentNavigationStrategyData } from '../types/contentNavigationStrategy';\nimport { logger } from '../../util/logger';\n\n// ============================================================================\n// NAVIGATION STRATEGY API\n// ============================================================================\n//\n// This module defines the ContentNavigator base class and the WeightedCard type,\n// which form the foundation of the pluggable navigation strategy system.\n//\n// KEY CONCEPTS:\n//\n// 1. WeightedCard - A card with a suitability score (0-1) and provenance trail.\n// The provenance tracks how each strategy in the pipeline contributed to\n// the card's final score, ensuring transparency and debuggability.\n//\n// 2. ContentNavigator - Abstract base class for backward compatibility.\n// New code should use CardGenerator or CardFilter interfaces directly.\n//\n// 3. CardGenerator vs CardFilter:\n// - Generators (ELO, SRS) produce candidate cards with scores\n// - Filters (Hierarchy, Interference, Priority, EloDistance) transform scores\n//\n// 4. Pipeline architecture:\n// Pipeline(generator, [filter1, filter2, ...]) executes:\n// cards = generator.getWeightedCards()\n// cards = filter1.transform(cards, context)\n// cards = filter2.transform(cards, context)\n// return sorted(cards)\n//\n// 5. Provenance tracking - Each strategy adds an entry explaining its contribution.\n// This makes the system transparent and debuggable.\n//\n// ============================================================================\n\n/**\n * Tracks a single strategy's contribution to a card's final score.\n *\n * Each strategy in the pipeline adds a StrategyContribution entry to the\n * card's provenance array, creating an audit trail of scoring decisions.\n */\nexport interface StrategyContribution {\n /**\n * Strategy type (implementing class name).\n * Examples: 'elo', 'hierarchyDefinition', 'interferenceMitigator'\n */\n strategy: string;\n\n /**\n * Human-readable name identifying this specific strategy instance.\n * Extracted from ContentNavigationStrategyData.name.\n * Courses may have multiple instances of the same strategy type with\n * different configurations.\n *\n * Examples:\n * - \"ELO (default)\"\n * - \"Interference: b/d/p confusion\"\n * - \"Interference: phonetic confusables\"\n * - \"Priority: Common letters first\"\n */\n strategyName: string;\n\n /**\n * Unique database document ID for this strategy instance.\n * Extracted from ContentNavigationStrategyData._id.\n * Use this to fetch the full strategy configuration document.\n *\n * Examples:\n * - \"NAVIGATION_STRATEGY-ELO-default\"\n * - \"NAVIGATION_STRATEGY-interference-bdp\"\n * - \"NAVIGATION_STRATEGY-priority-common-letters\"\n */\n strategyId: string;\n\n /**\n * What the strategy did:\n * - 'generated': Strategy produced this card (generators only)\n * - 'passed': Strategy evaluated but didn't change score (transparent pass-through)\n * - 'boosted': Strategy increased the score\n * - 'penalized': Strategy decreased the score\n */\n action: 'generated' | 'passed' | 'boosted' | 'penalized';\n\n /** Score after this strategy's processing */\n score: number;\n\n /**\n * Human-readable explanation of the strategy's decision.\n *\n * Examples:\n * - \"ELO distance 75, new card\"\n * - \"Prerequisites met: letter-sounds\"\n * - \"Interferes with immature tag 'd' (decay 0.8)\"\n * - \"High-priority tag 's' (0.95) → boost 1.15x\"\n *\n * Required for transparency - silent adjusters are anti-patterns.\n */\n reason: string;\n}\n\n/**\n * A card with a suitability score and provenance trail.\n *\n * Scores range from 0-1:\n * - 1.0 = fully suitable\n * - 0.0 = hard filter (e.g., prerequisite not met)\n * - 0.5 = neutral\n * - Intermediate values = soft preference\n *\n * Provenance tracks the scoring pipeline:\n * - First entry: Generator that produced the card\n * - Subsequent entries: Filters that transformed the score\n * - Each entry includes action and human-readable reason\n */\nexport interface WeightedCard {\n cardId: string;\n courseId: string;\n /** Suitability score from 0-1 */\n score: number;\n /**\n * Audit trail of strategy contributions.\n * First entry is from the generator, subsequent entries from filters.\n */\n provenance: StrategyContribution[];\n /**\n * Pre-fetched tags. Populated by Pipeline before filters run.\n * Filters should use this instead of querying getAppliedTags() individually.\n */\n tags?: string[];\n /**\n * Review document ID (_id from ScheduledCard).\n * Present when this card originated from SRS review scheduling.\n * Used by SessionController to track review outcomes and maintain review state.\n */\n reviewID?: string;\n}\n\n/**\n * Extract card origin from provenance trail.\n *\n * The first provenance entry (from the generator) indicates whether\n * this is a new card, review, or failed card. We parse the reason\n * string to extract this information.\n *\n * @param card - Card with provenance trail\n * @returns Card origin ('new', 'review', or 'failed')\n */\nexport function getCardOrigin(card: WeightedCard): 'new' | 'review' | 'failed' {\n if (card.provenance.length === 0) {\n throw new Error('Card has no provenance - cannot determine origin');\n }\n\n const firstEntry = card.provenance[0];\n const reason = firstEntry.reason.toLowerCase();\n\n if (reason.includes('failed')) {\n return 'failed';\n }\n if (reason.includes('review')) {\n return 'review';\n }\n return 'new';\n}\n\nexport enum Navigators {\n ELO = 'elo',\n SRS = 'srs',\n HIERARCHY = 'hierarchyDefinition',\n INTERFERENCE = 'interferenceMitigator',\n RELATIVE_PRIORITY = 'relativePriority',\n USER_TAG_PREFERENCE = 'userTagPreference',\n}\n\n// ============================================================================\n// NAVIGATOR ROLE CLASSIFICATION\n// ============================================================================\n//\n// Navigators are classified as either generators or filters:\n// - Generators: Produce candidate cards (ELO, SRS)\n// - Filters: Transform/score candidates (Hierarchy, Interference, RelativePriority)\n//\n// This classification is used by PipelineAssembler to build pipelines:\n// 1. Instantiate generators (possibly into a CompositeGenerator)\n// 2. Instantiate filters\n// 3. Create Pipeline(generator, filters)\n//\n// ============================================================================\n\n/**\n * Role classification for navigation strategies.\n *\n * - GENERATOR: Produces candidate cards with initial scores\n * - FILTER: Transforms cards with score multipliers\n */\nexport enum NavigatorRole {\n GENERATOR = 'generator',\n FILTER = 'filter',\n}\n\n/**\n * Registry mapping navigator implementations to their roles.\n */\nexport const NavigatorRoles: Record<Navigators, NavigatorRole> = {\n [Navigators.ELO]: NavigatorRole.GENERATOR,\n [Navigators.SRS]: NavigatorRole.GENERATOR,\n [Navigators.HIERARCHY]: NavigatorRole.FILTER,\n [Navigators.INTERFERENCE]: NavigatorRole.FILTER,\n [Navigators.RELATIVE_PRIORITY]: NavigatorRole.FILTER,\n [Navigators.USER_TAG_PREFERENCE]: NavigatorRole.FILTER,\n};\n\n/**\n * Check if a navigator implementation is a generator.\n *\n * @param impl - Navigator implementation name (e.g., 'elo', 'hierarchyDefinition')\n * @returns true if the navigator is a generator, false otherwise\n */\nexport function isGenerator(impl: string): boolean {\n return NavigatorRoles[impl as Navigators] === NavigatorRole.GENERATOR;\n}\n\n/**\n * Check if a navigator implementation is a filter.\n *\n * @param impl - Navigator implementation name (e.g., 'elo', 'hierarchyDefinition')\n * @returns true if the navigator is a filter, false otherwise\n */\nexport function isFilter(impl: string): boolean {\n return NavigatorRoles[impl as Navigators] === NavigatorRole.FILTER;\n}\n\n/**\n * Abstract base class for navigation strategies.\n *\n * This class exists primarily for backward compatibility with legacy code.\n * New code should use CardGenerator or CardFilter interfaces directly.\n *\n * The class implements StudyContentSource for compatibility with SessionController.\n * Once SessionController migrates to use getWeightedCards() exclusively,\n * the legacy methods can be removed.\n */\nexport abstract class ContentNavigator implements StudyContentSource {\n /** User interface for this navigation session */\n protected user: UserDBInterface;\n\n /** Course interface for this navigation session */\n protected course: CourseDBInterface;\n\n /** Human-readable name for this strategy instance (from ContentNavigationStrategyData.name) */\n protected strategyName?: string;\n\n /** Unique document ID for this strategy instance (from ContentNavigationStrategyData._id) */\n protected strategyId?: string;\n\n /**\n * Constructor for standard navigators.\n * Call this from subclass constructors to initialize common fields.\n *\n * Note: CompositeGenerator and Pipeline call super() without args, then set\n * user/course fields directly if needed.\n */\n constructor(\n user?: UserDBInterface,\n course?: CourseDBInterface,\n strategyData?: ContentNavigationStrategyData\n ) {\n this.user = user!;\n this.course = course!;\n if (strategyData) {\n this.strategyName = strategyData.name;\n this.strategyId = strategyData._id;\n }\n }\n\n // ============================================================================\n // STRATEGY STATE HELPERS\n // ============================================================================\n //\n // These methods allow strategies to persist their own state (user preferences,\n // learned patterns, temporal tracking) in the user database.\n //\n // ============================================================================\n\n /**\n * Unique key identifying this strategy for state storage.\n *\n * Defaults to the constructor name (e.g., \"UserTagPreferenceFilter\").\n * Override in subclasses if multiple instances of the same strategy type\n * need separate state storage.\n */\n protected get strategyKey(): string {\n return this.constructor.name;\n }\n\n /**\n * Get this strategy's persisted state for the current course.\n *\n * @returns The strategy's data payload, or null if no state exists\n * @throws Error if user or course is not initialized\n */\n protected async getStrategyState<T>(): Promise<T | null> {\n if (!this.user || !this.course) {\n throw new Error(\n `Cannot get strategy state: navigator not properly initialized. ` +\n `Ensure user and course are provided to constructor.`\n );\n }\n return this.user.getStrategyState<T>(this.course.getCourseID(), this.strategyKey);\n }\n\n /**\n * Persist this strategy's state for the current course.\n *\n * @param data - The strategy's data payload to store\n * @throws Error if user or course is not initialized\n */\n protected async putStrategyState<T>(data: T): Promise<void> {\n if (!this.user || !this.course) {\n throw new Error(\n `Cannot put strategy state: navigator not properly initialized. ` +\n `Ensure user and course are provided to constructor.`\n );\n }\n return this.user.putStrategyState<T>(this.course.getCourseID(), this.strategyKey, data);\n }\n\n /**\n * Factory method to create navigator instances dynamically.\n *\n * @param user - User interface\n * @param course - Course interface\n * @param strategyData - Strategy configuration document\n * @returns the runtime object used to steer a study session.\n */\n static async create(\n user: UserDBInterface,\n course: CourseDBInterface,\n strategyData: ContentNavigationStrategyData\n ): Promise<ContentNavigator> {\n const implementingClass = strategyData.implementingClass;\n let NavigatorImpl;\n\n // Try different extension variations\n const variations = ['.ts', '.js', ''];\n const dirs = ['filters', 'generators'];\n\n for (const ext of variations) {\n for (const dir of dirs) {\n const loadFrom = `./${dir}/${implementingClass}${ext}`;\n try {\n const module = await import(loadFrom);\n NavigatorImpl = module.default;\n break; // Break the loop if loading succeeds\n } catch (e) {\n // Continue to next variation if this one fails\n logger.debug(`Failed to load extension from ${loadFrom}:`, e);\n }\n }\n }\n\n if (!NavigatorImpl) {\n throw new Error(`Could not load navigator implementation for: ${implementingClass}`);\n }\n\n return new NavigatorImpl(user, course, strategyData);\n }\n\n /**\n * Get cards with suitability scores and provenance trails.\n *\n * **This is the PRIMARY API for navigation strategies.**\n *\n * Returns cards ranked by suitability score (0-1). Higher scores indicate\n * better candidates for presentation. Each card includes a provenance trail\n * documenting how strategies contributed to the final score.\n *\n * ## Implementation Required\n * All navigation strategies MUST override this method. The base class does\n * not provide a default implementation.\n *\n * ## For Generators\n * Override this method to generate candidates and compute scores based on\n * your strategy's logic (e.g., ELO proximity, review urgency). Create the\n * initial provenance entry with action='generated'.\n *\n * ## For Filters\n * Filters should implement the CardFilter interface instead and be composed\n * via Pipeline. Filters do not directly implement getWeightedCards().\n *\n * @param limit - Maximum cards to return\n * @returns Cards sorted by score descending, with provenance trails\n */\n async getWeightedCards(_limit: number): Promise<WeightedCard[]> {\n throw new Error(`${this.constructor.name} must implement getWeightedCards(). `);\n }\n}\n","import { toCourseElo } from '@vue-skuilder/common';\nimport type { CourseDBInterface } from '../interfaces/courseDB';\nimport type { UserDBInterface } from '../interfaces/userDB';\nimport { ContentNavigator } from './index';\nimport type { WeightedCard } from './index';\nimport type { CardFilter, FilterContext } from './filters/types';\nimport type { CardGenerator, GeneratorContext } from './generators/types';\nimport { logger } from '../../util/logger';\n\n// ============================================================================\n// PIPELINE LOGGING HELPERS\n// ============================================================================\n//\n// Focused logging functions that can be toggled by commenting single lines.\n// Use these to inspect pipeline behavior in development/production.\n//\n\n/**\n * Log pipeline configuration on construction.\n * Shows generator and filter chain structure.\n */\nfunction logPipelineConfig(generator: CardGenerator, filters: CardFilter[]): void {\n const filterList =\n filters.length > 0 ? '\\n - ' + filters.map((f) => f.name).join('\\n - ') : ' none';\n\n logger.info(\n `[Pipeline] Configuration:\\n` + ` Generator: ${generator.name}\\n` + ` Filters:${filterList}`\n );\n}\n\n/**\n * Log tag hydration results.\n * Shows effectiveness of batch query (how many cards/tags were hydrated).\n */\nfunction logTagHydration(cards: WeightedCard[], tagsByCard: Map<string, string[]>): void {\n const totalTags = Array.from(tagsByCard.values()).reduce((sum, tags) => sum + tags.length, 0);\n const cardsWithTags = Array.from(tagsByCard.values()).filter((tags) => tags.length > 0).length;\n\n logger.debug(\n `[Pipeline] Tag hydration: ${cards.length} cards, ` +\n `${cardsWithTags} have tags (${totalTags} total tags) - single batch query`\n );\n}\n\n/**\n * Log pipeline execution summary.\n * Shows complete flow from generator through filters to final results.\n */\nfunction logExecutionSummary(\n generatorName: string,\n generatedCount: number,\n filterCount: number,\n finalCount: number,\n topScores: number[]\n): void {\n const scoreDisplay =\n topScores.length > 0 ? topScores.map((s) => s.toFixed(2)).join(', ') : 'none';\n\n logger.info(\n `[Pipeline] Execution: ${generatorName} produced ${generatedCount} → ` +\n `${filterCount} filters → ${finalCount} results (top scores: ${scoreDisplay})`\n );\n}\n\n/**\n * Log provenance trails for cards.\n * Shows the complete scoring history for each card through the pipeline.\n * Useful for debugging why cards scored the way they did.\n */\nfunction logCardProvenance(cards: WeightedCard[], maxCards: number = 3): void {\n const cardsToLog = cards.slice(0, maxCards);\n\n logger.debug(`[Pipeline] Provenance for top ${cardsToLog.length} cards:`);\n\n for (const card of cardsToLog) {\n logger.debug(`[Pipeline] ${card.cardId} (final score: ${card.score.toFixed(3)}):`);\n\n for (const entry of card.provenance) {\n const scoreChange = entry.score.toFixed(3);\n const action = entry.action.padEnd(9); // Align columns\n logger.debug(\n `[Pipeline] ${action} ${scoreChange} - ${entry.strategyName}: ${entry.reason}`\n );\n }\n }\n}\n\n// ============================================================================\n// PIPELINE\n// ============================================================================\n//\n// Executes a navigation pipeline: generator → filters → sorted results.\n//\n// Architecture:\n// cards = generator.getWeightedCards(limit, context)\n// cards = filter1.transform(cards, context)\n// cards = filter2.transform(cards, context)\n// cards = filter3.transform(cards, context)\n// return sorted(cards).slice(0, limit)\n//\n// Benefits:\n// - Clear separation: generators produce, filters transform\n// - No nested instantiation complexity\n// - Filters don't need to know about each other\n// - Shared context built once, passed to all stages\n//\n// ============================================================================\n\n/**\n * A navigation pipeline that runs a generator and applies filters sequentially.\n *\n * Implements StudyContentSource for backward compatibility with SessionController.\n *\n * ## Usage\n *\n * ```typescript\n * const pipeline = new Pipeline(\n * compositeGenerator, // or single generator\n * [eloDistanceFilter, interferenceFilter],\n * user,\n * course\n * );\n *\n * const cards = await pipeline.getWeightedCards(20);\n * ```\n */\nexport class Pipeline extends ContentNavigator {\n private generator: CardGenerator;\n private filters: CardFilter[];\n\n /**\n * Create a new pipeline.\n *\n * @param generator - The generator (or CompositeGenerator) that produces candidates\n * @param filters - Filters to apply sequentially (order doesn't matter for multipliers)\n * @param user - User database interface\n * @param course - Course database interface\n */\n constructor(\n generator: CardGenerator,\n filters: CardFilter[],\n user: UserDBInterface,\n course: CourseDBInterface\n ) {\n super();\n this.generator = generator;\n this.filters = filters;\n this.user = user;\n this.course = course;\n\n course\n .getCourseConfig()\n .then((cfg) => {\n logger.debug(`[pipeline] Crated pipeline for ${cfg.name}`);\n })\n .catch((e) => {\n logger.error(`[pipeline] Failed to lookup courseCfg: ${e}`);\n });\n // Toggle pipeline configuration logging:\n logPipelineConfig(generator, filters);\n }\n\n /**\n * Get weighted cards by running generator and applying filters.\n *\n * 1. Build shared context (user ELO, etc.)\n * 2. Get candidates from generator (passing context)\n * 3. Batch hydrate tags for all candidates\n * 4. Apply each filter sequentially\n * 5. Remove zero-score cards\n * 6. Sort by score descending\n * 7. Return top N\n *\n * @param limit - Maximum number of cards to return\n * @returns Cards sorted by score descending\n */\n async getWeightedCards(limit: number): Promise<WeightedCard[]> {\n // Build shared context once\n const context = await this.buildContext();\n\n // Over-fetch from generator to account for filtering\n const overFetchMultiplier = 2 + this.filters.length * 0.5;\n const fetchLimit = Math.ceil(limit * overFetchMultiplier);\n\n logger.debug(\n `[Pipeline] Fetching ${fetchLimit} candidates from generator '${this.generator.name}'`\n );\n\n // Get candidates from generator, passing context\n let cards = await this.generator.getWeightedCards(fetchLimit, context);\n const generatedCount = cards.length;\n\n logger.debug(`[Pipeline] Generator returned ${generatedCount} candidates`);\n\n // Batch hydrate tags before filters run\n cards = await this.hydrateTags(cards);\n\n // Apply filters sequentially\n for (const filter of this.filters) {\n const beforeCount = cards.length;\n cards = await filter.transform(cards, context);\n logger.debug(`[Pipeline] Filter '${filter.name}': ${beforeCount} → ${cards.length} cards`);\n }\n\n // Remove zero-score cards (hard filtered)\n cards = cards.filter((c) => c.score > 0);\n\n // Sort by score descending\n cards.sort((a, b) => b.score - a.score);\n\n // Return top N\n const result = cards.slice(0, limit);\n\n // Toggle execution summary logging:\n const topScores = result.slice(0, 3).map((c) => c.score);\n logExecutionSummary(\n this.generator.name,\n generatedCount,\n this.filters.length,\n result.length,\n topScores\n );\n\n // Toggle provenance logging (shows scoring history for top cards):\n logCardProvenance(result, 3);\n\n return result;\n }\n\n /**\n * Batch hydrate tags for all cards.\n *\n * Fetches tags for all cards in a single database query and attaches them\n * to the WeightedCard objects. Filters can then use card.tags instead of\n * making individual getAppliedTags() calls.\n *\n * @param cards - Cards to hydrate\n * @returns Cards with tags populated\n */\n private async hydrateTags(cards: WeightedCard[]): Promise<WeightedCard[]> {\n if (cards.length === 0) {\n return cards;\n }\n\n const cardIds = cards.map((c) => c.cardId);\n const tagsByCard = await this.course!.getAppliedTagsBatch(cardIds);\n\n // Toggle tag hydration logging:\n logTagHydration(cards, tagsByCard);\n\n return cards.map((card) => ({\n ...card,\n tags: tagsByCard.get(card.cardId) ?? [],\n }));\n }\n\n /**\n * Build shared context for generator and filters.\n *\n * Called once per getWeightedCards() invocation.\n * Contains data that the generator and multiple filters might need.\n *\n * The context satisfies both GeneratorContext and FilterContext interfaces.\n */\n private async buildContext(): Promise<GeneratorContext & FilterContext> {\n let userElo = 1000; // Default ELO\n\n try {\n const courseReg = await this.user!.getCourseRegDoc(this.course!.getCourseID());\n const courseElo = toCourseElo(courseReg.elo);\n userElo = courseElo.global.score;\n } catch (e) {\n logger.debug(`[Pipeline] Could not get user ELO, using default: ${e}`);\n }\n\n return {\n user: this.user!,\n course: this.course!,\n userElo,\n };\n }\n\n /**\n * Get the course ID for this pipeline.\n */\n getCourseID(): string {\n return this.course!.getCourseID();\n }\n}\n","import { ContentNavigator } from '../index';\nimport type { WeightedCard } from '../index';\nimport type { ContentNavigationStrategyData } from '../../types/contentNavigationStrategy';\nimport type { CourseDBInterface } from '../../interfaces/courseDB';\nimport type { UserDBInterface } from '../../interfaces/userDB';\nimport type { CardGenerator, GeneratorContext } from './types';\nimport { logger } from '../../../util/logger';\n\n// ============================================================================\n// COMPOSITE GENERATOR\n// ============================================================================\n//\n// Composes multiple generator strategies into a single generator.\n//\n// Use case: When a course has multiple generators (e.g., ELO + SRS), this\n// class merges their outputs into a unified candidate list.\n//\n// Aggregation strategy:\n// - Cards appearing in multiple generators get a frequency boost\n// - Score = average(scores) * (1 + 0.1 * (appearances - 1))\n// - This rewards cards that multiple generators agree on\n//\n// ============================================================================\n\n/**\n * Aggregation modes for combining scores from multiple generators.\n */\nexport enum AggregationMode {\n /** Use the maximum score from any generator */\n MAX = 'max',\n /** Average all scores */\n AVERAGE = 'average',\n /** Average with frequency boost: avg * (1 + 0.1 * (n-1)) */\n FREQUENCY_BOOST = 'frequencyBoost',\n}\n\nconst DEFAULT_AGGREGATION_MODE = AggregationMode.FREQUENCY_BOOST;\nconst FREQUENCY_BOOST_FACTOR = 0.1;\n\n/**\n * Composes multiple generators into a single generator.\n *\n * Implements CardGenerator for use in Pipeline architecture.\n * Also extends ContentNavigator for backward compatibility.\n *\n * Fetches candidates from all generators, deduplicates by cardId,\n * and aggregates scores based on the configured mode.\n */\nexport default class CompositeGenerator extends ContentNavigator implements CardGenerator {\n /** Human-readable name for CardGenerator interface */\n name: string = 'Composite Generator';\n\n private generators: CardGenerator[];\n private aggregationMode: AggregationMode;\n\n constructor(\n generators: CardGenerator[],\n aggregationMode: AggregationMode = DEFAULT_AGGREGATION_MODE\n ) {\n super();\n this.generators = generators;\n this.aggregationMode = aggregationMode;\n\n if (generators.length === 0) {\n throw new Error('CompositeGenerator requires at least one generator');\n }\n\n logger.debug(\n `[CompositeGenerator] Created with ${generators.length} generators, mode: ${aggregationMode}`\n );\n }\n\n /**\n * Creates a CompositeGenerator from strategy data.\n *\n * This is a convenience factory for use by PipelineAssembler.\n */\n static async fromStrategies(\n user: UserDBInterface,\n course: CourseDBInterface,\n strategies: ContentNavigationStrategyData[],\n aggregationMode: AggregationMode = DEFAULT_AGGREGATION_MODE\n ): Promise<CompositeGenerator> {\n const generators = await Promise.all(\n strategies.map((s) => ContentNavigator.create(user, course, s))\n );\n // Cast is safe because we know these are generators\n return new CompositeGenerator(generators as unknown as CardGenerator[], aggregationMode);\n }\n\n /**\n * Get weighted cards from all generators, merge and deduplicate.\n *\n * Cards appearing in multiple generators receive a score boost.\n * Provenance tracks which generators produced each card and how scores were aggregated.\n *\n * This method supports both the legacy signature (limit only) and the\n * CardGenerator interface signature (limit, context).\n *\n * @param limit - Maximum number of cards to return\n * @param context - GeneratorContext passed to child generators (required when called via Pipeline)\n */\n async getWeightedCards(limit: number, context?: GeneratorContext): Promise<WeightedCard[]> {\n if (!context) {\n throw new Error(\n 'CompositeGenerator.getWeightedCards requires a GeneratorContext. ' +\n 'It should be called via Pipeline, not directly.'\n );\n }\n\n // Fetch from all generators in parallel\n const results = await Promise.all(\n this.generators.map((g) => g.getWeightedCards(limit, context))\n );\n\n // Group by cardId\n const byCardId = new Map<string, WeightedCard[]>();\n for (const cards of results) {\n for (const card of cards) {\n const existing = byCardId.get(card.cardId) || [];\n existing.push(card);\n byCardId.set(card.cardId, existing);\n }\n }\n\n // Aggregate scores\n const merged: WeightedCard[] = [];\n for (const [, cards] of byCardId) {\n const aggregatedScore = this.aggregateScores(cards);\n const finalScore = Math.min(1.0, aggregatedScore); // Clamp to [0, 1]\n\n // Merge provenance from all generators that produced this card\n const mergedProvenance = cards.flatMap((c) => c.provenance);\n\n // Determine action based on whether score changed\n const initialScore = cards[0].score;\n const action =\n finalScore > initialScore ? 'boosted' : finalScore < initialScore ? 'penalized' : 'passed';\n\n // Build reason explaining the aggregation\n const reason = this.buildAggregationReason(cards, finalScore);\n\n // Append composite provenance entry\n merged.push({\n ...cards[0],\n score: finalScore,\n provenance: [\n ...mergedProvenance,\n {\n strategy: 'composite',\n strategyName: 'Composite Generator',\n strategyId: 'COMPOSITE_GENERATOR',\n action,\n score: finalScore,\n reason,\n },\n ],\n });\n }\n\n // Sort by score descending and limit\n return merged.sort((a, b) => b.score - a.score).slice(0, limit);\n }\n\n /**\n * Build human-readable reason for score aggregation.\n */\n private buildAggregationReason(cards: WeightedCard[], finalScore: number): string {\n const count = cards.length;\n const scores = cards.map((c) => c.score.toFixed(2)).join(', ');\n\n if (count === 1) {\n return `Single generator, score ${finalScore.toFixed(2)}`;\n }\n\n const strategies = cards.map((c) => c.provenance[0]?.strategy || 'unknown').join(', ');\n\n switch (this.aggregationMode) {\n case AggregationMode.MAX:\n return `Max of ${count} generators (${strategies}): scores [${scores}] → ${finalScore.toFixed(2)}`;\n\n case AggregationMode.AVERAGE:\n return `Average of ${count} generators (${strategies}): scores [${scores}] → ${finalScore.toFixed(2)}`;\n\n case AggregationMode.FREQUENCY_BOOST: {\n const avg = cards.reduce((sum, c) => sum + c.score, 0) / count;\n const boost = 1 + FREQUENCY_BOOST_FACTOR * (count - 1);\n return `Frequency boost from ${count} generators (${strategies}): avg ${avg.toFixed(2)} × ${boost.toFixed(2)} → ${finalScore.toFixed(2)}`;\n }\n\n default:\n return `Aggregated from ${count} generators: ${finalScore.toFixed(2)}`;\n }\n }\n\n /**\n * Aggregate scores from multiple generators for the same card.\n */\n private aggregateScores(cards: WeightedCard[]): number {\n const scores = cards.map((c) => c.score);\n\n switch (this.aggregationMode) {\n case AggregationMode.MAX:\n return Math.max(...scores);\n\n case AggregationMode.AVERAGE:\n return scores.reduce((sum, s) => sum + s, 0) / scores.length;\n\n case AggregationMode.FREQUENCY_BOOST: {\n const avg = scores.reduce((sum, s) => sum + s, 0) / scores.length;\n const frequencyBoost = 1 + FREQUENCY_BOOST_FACTOR * (cards.length - 1);\n return avg * frequencyBoost;\n }\n\n default:\n return scores[0];\n }\n }\n}\n","import type { ContentNavigationStrategyData } from '../types/contentNavigationStrategy';\nimport { ContentNavigator, isGenerator, isFilter, Navigators } from './index';\nimport type { CardFilter } from './filters/types';\nimport type { CardGenerator } from './generators/types';\nimport { Pipeline } from './Pipeline';\nimport { DocType } from '../types/types-legacy';\nimport { logger } from '../../util/logger';\nimport type { CourseDBInterface } from '../interfaces/courseDB';\nimport type { UserDBInterface } from '../interfaces/userDB';\nimport CompositeGenerator from './generators/CompositeGenerator';\n\n// ============================================================================\n// PIPELINE ASSEMBLER\n// ============================================================================\n//\n// Assembles navigation strategies into a Pipeline instance.\n//\n// This class is DB-agnostic: it receives strategy documents and returns an\n// assembled, ready-to-use Pipeline. This separation enables:\n// 1. Use with different DB implementations (Couch, Static, etc.)\n// 2. Future dynamic/evolutionary strategy selection\n// 3. Easy unit testing without DB mocking\n//\n// Pipeline assembly:\n// 1. Separate strategies into generators and filters by role\n// 2. Instantiate generator(s) - wrap multiple in CompositeGenerator\n// 3. Instantiate filters\n// 4. Return Pipeline(generator, filters)\n//\n// ============================================================================\n\n/**\n * Input for pipeline assembly.\n */\nexport interface PipelineAssemblerInput {\n /** All strategy documents to assemble into a pipeline */\n strategies: ContentNavigationStrategyData[];\n /** User database interface (required for instantiation) */\n user: UserDBInterface;\n /** Course database interface (required for instantiation) */\n course: CourseDBInterface;\n}\n\n/**\n * Result of pipeline assembly.\n */\nexport interface PipelineAssemblyResult {\n /** The assembled pipeline, or null if assembly failed */\n pipeline: Pipeline | null;\n /** Generator strategies found (for informational purposes) */\n generatorStrategies: ContentNavigationStrategyData[];\n /** Filter strategies found (for informational purposes) */\n filterStrategies: ContentNavigationStrategyData[];\n /** Warnings encountered during assembly (logged but non-fatal) */\n warnings: string[];\n}\n\n/**\n * Assembles navigation strategies into a Pipeline.\n *\n * Instantiates generators and filters from strategy documents and\n * composes them into a ready-to-use Pipeline instance.\n */\nexport class PipelineAssembler {\n /**\n * Assembles a navigation pipeline from strategy documents.\n *\n * 1. Separates into generators and filters by role\n * 2. Validates at least one generator exists (or creates default ELO)\n * 3. Instantiates generators - wraps multiple in CompositeGenerator\n * 4. Instantiates filters\n * 5. Returns Pipeline(generator, filters)\n *\n * @param input - Strategy documents plus user/course interfaces\n * @returns Assembled pipeline and any warnings\n */\n async assemble(input: PipelineAssemblerInput): Promise<PipelineAssemblyResult> {\n const { strategies, user, course } = input;\n const warnings: string[] = [];\n\n if (strategies.length === 0) {\n return {\n pipeline: null,\n generatorStrategies: [],\n filterStrategies: [],\n warnings,\n };\n }\n\n // Separate generators from filters\n const generatorStrategies: ContentNavigationStrategyData[] = [];\n const filterStrategies: ContentNavigationStrategyData[] = [];\n\n for (const s of strategies) {\n if (isGenerator(s.implementingClass)) {\n generatorStrategies.push(s);\n } else if (isFilter(s.implementingClass)) {\n filterStrategies.push(s);\n } else {\n // Unknown strategy type — skip with warning\n warnings.push(`Unknown strategy type '${s.implementingClass}', skipping: ${s.name}`);\n }\n }\n\n // If no generator but filters exist, use default ELO generator\n if (generatorStrategies.length === 0) {\n if (filterStrategies.length > 0) {\n logger.debug(\n '[PipelineAssembler] No generator found, using default ELO with configured filters'\n );\n generatorStrategies.push(this.makeDefaultEloStrategy(course.getCourseID()));\n } else {\n warnings.push('No generator strategy found');\n return {\n pipeline: null,\n generatorStrategies: [],\n filterStrategies: [],\n warnings,\n };\n }\n }\n\n // Instantiate generators\n let generator: CardGenerator;\n\n if (generatorStrategies.length === 1) {\n // Single generator\n const nav = await ContentNavigator.create(user, course, generatorStrategies[0]);\n generator = nav as unknown as CardGenerator;\n logger.debug(`[PipelineAssembler] Using single generator: ${generatorStrategies[0].name}`);\n } else {\n // Multiple generators - wrap in CompositeGenerator\n logger.debug(\n `[PipelineAssembler] Using CompositeGenerator for ${generatorStrategies.length} generators: ${generatorStrategies.map((g) => g.name).join(', ')}`\n );\n generator = await CompositeGenerator.fromStrategies(user, course, generatorStrategies);\n }\n\n // Instantiate filters\n const filters: CardFilter[] = [];\n\n // Sort filters alphabetically for deterministic ordering\n const sortedFilterStrategies = [...filterStrategies].sort((a, b) =>\n a.name.localeCompare(b.name)\n );\n\n for (const filterStrategy of sortedFilterStrategies) {\n try {\n const nav = await ContentNavigator.create(user, course, filterStrategy);\n // The navigator implements CardFilter\n if ('transform' in nav && typeof nav.transform === 'function') {\n filters.push(nav as unknown as CardFilter);\n logger.debug(`[PipelineAssembler] Added filter: ${filterStrategy.name}`);\n } else {\n warnings.push(\n `Filter '${filterStrategy.name}' does not implement CardFilter.transform(), skipping`\n );\n }\n } catch (e) {\n warnings.push(`Failed to instantiate filter '${filterStrategy.name}': ${e}`);\n }\n }\n\n // Build pipeline\n const pipeline = new Pipeline(generator, filters, user, course);\n\n logger.debug(\n `[PipelineAssembler] Assembled pipeline with ${generatorStrategies.length} generator(s) and ${filters.length} filter(s)`\n );\n\n return {\n pipeline,\n generatorStrategies,\n filterStrategies: sortedFilterStrategies,\n warnings,\n };\n }\n\n /**\n * Creates a default ELO generator strategy.\n * Used when filters are configured but no generator is specified.\n */\n private makeDefaultEloStrategy(courseId: string): ContentNavigationStrategyData {\n return {\n _id: 'NAVIGATION_STRATEGY-ELO-default',\n course: courseId,\n docType: DocType.NAVIGATION_STRATEGY,\n name: 'ELO (default)',\n description: 'Default ELO-based generator',\n implementingClass: Navigators.ELO,\n serializedData: '',\n };\n }\n}\n","import type { CourseDBInterface } from '../../interfaces/courseDB';\nimport type { UserDBInterface } from '../../interfaces/userDB';\nimport { ContentNavigator } from '../index';\nimport type { WeightedCard } from '../index';\nimport { toCourseElo } from '@vue-skuilder/common';\nimport type { QualifiedCardID } from '../..';\nimport type { CardGenerator, GeneratorContext } from './types';\n\n// ============================================================================\n// ELO NAVIGATOR\n// ============================================================================\n//\n// A generator strategy that selects new cards based on ELO proximity.\n//\n// Cards closer to the user's skill level (ELO) receive higher scores.\n// This ensures learners see content matched to their current ability.\n//\n// NOTE: This generator only handles NEW cards. Reviews are handled by\n// SRSNavigator. Use CompositeGenerator to combine both.\n//\n// ============================================================================\n\n/**\n * A navigation strategy that scores new cards by ELO proximity.\n *\n * Implements CardGenerator for use in Pipeline architecture.\n * Also extends ContentNavigator for backward compatibility with legacy code.\n *\n * Higher scores indicate better ELO match:\n * - Cards at user's ELO level score highest\n * - Score decreases with ELO distance\n *\n * Only returns new cards - use SRSNavigator for reviews.\n */\nexport default class ELONavigator extends ContentNavigator implements CardGenerator {\n /** Human-readable name for CardGenerator interface */\n name: string;\n\n constructor(\n user: UserDBInterface,\n course: CourseDBInterface,\n strategyData?: { name: string; _id: string }\n // The ELO strategy is non-parameterized.\n //\n // It instead relies on existing meta data from the course and user with respect to\n // ELO scores - it uses those to select cards matched to user skill level.\n ) {\n super(user, course, strategyData as any);\n this.name = strategyData?.name || 'ELO';\n }\n\n /**\n * Get new cards with suitability scores based on ELO distance.\n *\n * Cards closer to user's ELO get higher scores.\n * Score formula: max(0, 1 - distance / 500)\n *\n * NOTE: This generator only handles NEW cards. Reviews are handled by\n * SRSNavigator. Use CompositeGenerator to combine both.\n *\n * This method supports both the legacy signature (limit only) and the\n * CardGenerator interface signature (limit, context).\n *\n * @param limit - Maximum number of cards to return\n * @param context - Optional GeneratorContext (used when called via Pipeline)\n */\n async getWeightedCards(limit: number, context?: GeneratorContext): Promise<WeightedCard[]> {\n // Determine user ELO - from context if available, otherwise fetch\n let userGlobalElo: number;\n if (context?.userElo !== undefined) {\n userGlobalElo = context.userElo;\n } else {\n const courseReg = await this.user.getCourseRegDoc(this.course.getCourseID());\n const userElo = toCourseElo(courseReg.elo);\n userGlobalElo = userElo.global.score;\n }\n\n const activeCards = await this.user.getActiveCards();\n const newCards = (\n await this.course.getCardsCenteredAtELO(\n { limit, elo: 'user' },\n (c: QualifiedCardID) => !activeCards.some((ac) => c.cardID === ac.cardID)\n )\n ).map((c) => ({ ...c, status: 'new' as const }));\n\n // Get ELO data for all cards in one batch\n const cardIds = newCards.map((c) => c.cardID);\n const cardEloData = await this.course.getCardEloData(cardIds);\n\n // Score new cards by ELO distance\n const scored: WeightedCard[] = newCards.map((c, i) => {\n const cardElo = cardEloData[i]?.global?.score ?? 1000;\n const distance = Math.abs(cardElo - userGlobalElo);\n const score = Math.max(0, 1 - distance / 500);\n\n return {\n cardId: c.cardID,\n courseId: c.courseID,\n score,\n provenance: [\n {\n strategy: 'elo',\n strategyName: this.strategyName || this.name,\n strategyId: this.strategyId || 'NAVIGATION_STRATEGY-ELO-default',\n action: 'generated',\n score,\n reason: `ELO distance ${Math.round(distance)} (card: ${Math.round(cardElo)}, user: ${Math.round(userGlobalElo)}), new card`,\n },\n ],\n };\n });\n\n // Sort by score descending\n scored.sort((a, b) => b.score - a.score);\n\n return scored.slice(0, limit);\n }\n}\n","import moment from 'moment';\nimport type { ScheduledCard } from '../../types/user';\nimport type { CourseDBInterface } from '../../interfaces/courseDB';\nimport type { UserDBInterface } from '../../interfaces/userDB';\nimport { ContentNavigator } from '../index';\nimport type { WeightedCard } from '../index';\nimport type { ContentNavigationStrategyData } from '../../types/contentNavigationStrategy';\nimport type { CardGenerator, GeneratorContext } from './types';\nimport { logger } from '@db/util/logger';\n\n// ============================================================================\n// SRS NAVIGATOR\n// ============================================================================\n//\n// A generator strategy that scores review cards by urgency.\n//\n// Urgency is determined by two factors:\n// 1. Overdueness - how far past the scheduled review time\n// 2. Interval recency - shorter scheduled intervals indicate \"novel content in progress\"\n//\n// A card with a 3-day interval that's 2 days overdue is more urgent than a card\n// with a 6-month interval that's 2 days overdue. The shorter interval represents\n// active learning at higher resolution.\n//\n// This navigator only handles reviews - it does not generate new cards.\n// For new cards, use ELONavigator or another generator via CompositeGenerator.\n//\n// ============================================================================\n\n/**\n * Configuration for the SRS strategy.\n * Currently minimal - the algorithm is not parameterized.\n */\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type\nexport interface SRSConfig {\n // Future: configurable urgency curves, thresholds, etc.\n}\n\n/**\n * A navigation strategy that scores review cards by urgency.\n *\n * Implements CardGenerator for use in Pipeline architecture.\n * Also extends ContentNavigator for backward compatibility with legacy code.\n *\n * Higher scores indicate more urgent reviews:\n * - Cards that are more overdue (relative to their interval) score higher\n * - Cards with shorter intervals (recent learning) score higher\n *\n * Only returns cards that are actually due (reviewTime has passed).\n * Does not generate new cards - use with CompositeGenerator for mixed content.\n */\nexport default class SRSNavigator extends ContentNavigator implements CardGenerator {\n /** Human-readable name for CardGenerator interface */\n name: string;\n\n constructor(\n user: UserDBInterface,\n course: CourseDBInterface,\n strategyData?: ContentNavigationStrategyData\n ) {\n super(user, course, strategyData as ContentNavigationStrategyData);\n this.name = strategyData?.name || 'SRS';\n }\n\n /**\n * Get review cards scored by urgency.\n *\n * Score formula combines:\n * - Relative overdueness: hoursOverdue / intervalHours\n * - Interval recency: exponential decay favoring shorter intervals\n *\n * Cards not yet due are excluded (not scored as 0).\n *\n * This method supports both the legacy signature (limit only) and the\n * CardGenerator interface signature (limit, context).\n *\n * @param limit - Maximum number of cards to return\n * @param _context - Optional GeneratorContext (currently unused, but required for interface)\n */\n async getWeightedCards(limit: number, _context?: GeneratorContext): Promise<WeightedCard[]> {\n if (!this.user || !this.course) {\n throw new Error('SRSNavigator requires user and course to be set');\n }\n\n const reviews = await this.user.getPendingReviews(this.course.getCourseID());\n const now = moment.utc();\n\n // Filter to only cards that are actually due\n const dueReviews = reviews.filter((r) => now.isAfter(moment.utc(r.reviewTime)));\n\n const scored = dueReviews.map((review) => {\n const { score, reason } = this.computeUrgencyScore(review, now);\n\n return {\n cardId: review.cardId,\n courseId: review.courseId,\n score,\n reviewID: review._id,\n provenance: [\n {\n strategy: 'srs',\n strategyName: this.strategyName || this.name,\n strategyId: this.strategyId || 'NAVIGATION_STRATEGY-SRS-default',\n action: 'generated' as const,\n score,\n reason,\n },\n ],\n };\n });\n\n logger.debug(`[srsNav] got ${scored.length} weighted cards`);\n\n // Sort by score descending and limit\n return scored.sort((a, b) => b.score - a.score).slice(0, limit);\n }\n\n /**\n * Compute urgency score for a review card.\n *\n * Two factors:\n * 1. Relative overdueness = hoursOverdue / intervalHours\n * - 2 days overdue on 3-day interval = 0.67 (urgent)\n * - 2 days overdue on 180-day interval = 0.01 (not urgent)\n *\n * 2. Interval recency factor = 0.3 + 0.7 * exp(-intervalHours / 720)\n * - 24h interval → ~1.0 (very recent learning)\n * - 30 days (720h) → ~0.56\n * - 180 days → ~0.30\n *\n * Combined: base 0.5 + weighted average of factors * 0.45\n * Result range: approximately 0.5 to 0.95\n */\n private computeUrgencyScore(\n review: ScheduledCard,\n now: moment.Moment\n ): { score: number; reason: string } {\n const scheduledAt = moment.utc(review.scheduledAt);\n const due = moment.utc(review.reviewTime);\n\n // Interval = time between scheduling and due date (minimum 1 hour to avoid division issues)\n const intervalHours = Math.max(1, due.diff(scheduledAt, 'hours'));\n const hoursOverdue = now.diff(due, 'hours');\n\n // Relative overdueness: how late relative to the interval\n const relativeOverdue = hoursOverdue / intervalHours;\n\n // Interval recency factor: shorter intervals = more urgent\n // Exponential decay with 720h (30 days) as the characteristic time\n const recencyFactor = 0.3 + 0.7 * Math.exp(-intervalHours / 720);\n\n // Combined urgency: weighted average of relative overdue and recency\n // Clamp relative overdue contribution to [0, 1] to avoid runaway scores\n const overdueContribution = Math.min(1.0, Math.max(0, relativeOverdue));\n const urgency = overdueContribution * 0.5 + recencyFactor * 0.5;\n\n // Final score: base 0.5 + urgency contribution, capped at 0.95\n const score = Math.min(0.95, 0.5 + urgency * 0.45);\n\n const reason =\n `${Math.round(hoursOverdue)}h overdue (interval: ${Math.round(intervalHours)}h, ` +\n `relative: ${relativeOverdue.toFixed(2)}), recency: ${recencyFactor.toFixed(2)}, review`;\n\n return { score, reason };\n }\n}\n","import type { WeightedCard } from '../index';\nimport type { CardFilter, FilterContext } from './types';\n\n// ============================================================================\n// ELO DISTANCE FILTER\n// ============================================================================\n//\n// Penalizes cards that are far from the user's current ELO using a smooth curve.\n//\n// This filter addresses cross-strategy coordination:\n// - SRS generates reviews based on scheduling\n// - But some scheduled cards may be \"below\" the user's current level\n// - Or \"above\" (shouldn't happen often, but possible)\n//\n// By applying ELO distance penalties, we can:\n// - Deprioritize reviews the user has \"moved beyond\"\n// - Deprioritize cards that are too hard for current skill level\n//\n// The penalty curve is smooth (no discontinuities) using a Gaussian-like decay.\n//\n// ============================================================================\n\n/**\n * Configuration for the ELO distance filter.\n */\nexport interface EloDistanceConfig {\n /**\n * The ELO distance at which the multiplier is ~0.6 (one standard deviation).\n * Default: 200 ELO points.\n *\n * - At distance 0: multiplier ≈ 1.0\n * - At distance = halfLife: multiplier ≈ 0.6\n * - At distance = 2 * halfLife: multiplier ≈ 0.37\n * - At distance = 3 * halfLife: multiplier ≈ 0.22\n */\n halfLife?: number;\n\n /**\n * Minimum multiplier (floor) to prevent scores from going too low.\n * Default: 0.3\n */\n minMultiplier?: number;\n\n /**\n * Maximum multiplier (ceiling). Usually 1.0 (no boost for close cards).\n * Default: 1.0\n */\n maxMultiplier?: number;\n}\n\nconst DEFAULT_HALF_LIFE = 200;\nconst DEFAULT_MIN_MULTIPLIER = 0.3;\nconst DEFAULT_MAX_MULTIPLIER = 1.0;\n\n/**\n * Compute the multiplier for a given ELO distance using Gaussian decay.\n *\n * Formula: minMultiplier + (maxMultiplier - minMultiplier) * exp(-(distance/halfLife)^2)\n *\n * This produces a smooth bell curve centered at distance=0:\n * - At distance 0: multiplier = maxMultiplier (1.0)\n * - As distance increases: multiplier smoothly decays toward minMultiplier\n * - No discontinuities or sudden jumps\n */\nfunction computeMultiplier(\n distance: number,\n halfLife: number,\n minMultiplier: number,\n maxMultiplier: number\n): number {\n // Gaussian decay: exp(-(d/h)^2)\n const normalizedDistance = distance / halfLife;\n const decay = Math.exp(-(normalizedDistance * normalizedDistance));\n\n // Scale between min and max\n return minMultiplier + (maxMultiplier - minMultiplier) * decay;\n}\n\n/**\n * Create an ELO distance filter.\n *\n * Penalizes cards that are far from the user's current ELO level\n * using a smooth Gaussian decay curve. No discontinuities.\n *\n * @param config - Optional configuration for the decay curve\n * @returns A CardFilter that applies ELO distance penalties\n */\nexport function createEloDistanceFilter(config?: EloDistanceConfig): CardFilter {\n const halfLife = config?.halfLife ?? DEFAULT_HALF_LIFE;\n const minMultiplier = config?.minMultiplier ?? DEFAULT_MIN_MULTIPLIER;\n const maxMultiplier = config?.maxMultiplier ?? DEFAULT_MAX_MULTIPLIER;\n\n return {\n name: 'ELO Distance Filter',\n\n async transform(cards: WeightedCard[], context: FilterContext): Promise<WeightedCard[]> {\n const { course, userElo } = context;\n\n // Batch fetch ELO data for all cards\n const cardIds = cards.map((c) => c.cardId);\n const cardElos = await course.getCardEloData(cardIds);\n\n return cards.map((card, i) => {\n const cardElo = cardElos[i]?.global?.score ?? 1000;\n const distance = Math.abs(cardElo - userElo);\n const multiplier = computeMultiplier(distance, halfLife, minMultiplier, maxMultiplier);\n const newScore = card.score * multiplier;\n\n const action = multiplier < maxMultiplier - 0.01 ? 'penalized' : 'passed';\n\n return {\n ...card,\n score: newScore,\n provenance: [\n ...card.provenance,\n {\n strategy: 'eloDistance',\n strategyName: 'ELO Distance Filter',\n strategyId: 'ELO_DISTANCE_FILTER',\n action,\n score: newScore,\n reason: `ELO distance ${Math.round(distance)} (card: ${Math.round(cardElo)}, user: ${Math.round(userElo)}) → ${multiplier.toFixed(2)}x`,\n },\n ],\n };\n });\n },\n };\n}\n\n// Export defaults for testing\nexport { DEFAULT_HALF_LIFE, DEFAULT_MIN_MULTIPLIER, DEFAULT_MAX_MULTIPLIER };\n","import { Navigators } from './index';\nimport { Pipeline } from './Pipeline';\nimport CompositeGenerator from './generators/CompositeGenerator';\nimport ELONavigator from './generators/elo';\nimport SRSNavigator from './generators/srs';\nimport { createEloDistanceFilter } from './filters/eloDistance';\nimport type { ContentNavigationStrategyData } from '../types/contentNavigationStrategy';\nimport { DocType } from '../types/types-legacy';\nimport type { CourseDBInterface, UserDBInterface } from '../interfaces';\n\n/**\n * Default navigation pipeline configuration.\n *\n * This module provides factory functions for creating the canonical default\n * navigation pipeline used by both CouchDB and static course implementations.\n */\n\n/**\n * Create default ELO navigation strategy data.\n * Used when no custom strategies are configured.\n *\n * @param courseId - The course ID to associate with this strategy\n * @returns Strategy data for default ELO navigation\n */\nexport function createDefaultEloStrategy(courseId: string): ContentNavigationStrategyData {\n return {\n _id: 'NAVIGATION_STRATEGY-ELO-default',\n docType: DocType.NAVIGATION_STRATEGY,\n name: 'ELO (default)',\n description: 'Default ELO-based navigation strategy for new cards',\n implementingClass: Navigators.ELO,\n course: courseId,\n serializedData: '',\n };\n}\n\n/**\n * Create default SRS navigation strategy data.\n * Used when no custom strategies are configured.\n *\n * @param courseId - The course ID to associate with this strategy\n * @returns Strategy data for default SRS navigation\n */\nexport function createDefaultSrsStrategy(courseId: string): ContentNavigationStrategyData {\n return {\n _id: 'NAVIGATION_STRATEGY-SRS-default',\n docType: DocType.NAVIGATION_STRATEGY,\n name: 'SRS (default)',\n description: 'Default SRS-based navigation strategy for reviews',\n implementingClass: Navigators.SRS,\n course: courseId,\n serializedData: '',\n };\n}\n\n/**\n * Creates the default navigation pipeline for courses with no configured strategies.\n *\n * Default: Pipeline(Composite(ELO, SRS), [eloDistanceFilter])\n * - ELO generator: scores new cards by skill proximity\n * - SRS generator: scores reviews by overdueness and interval recency\n * - ELO distance filter: penalizes cards far from user's current level\n *\n * This is the canonical default configuration used when:\n * - No navigation strategy documents exist in the course\n * - PipelineAssembler fails to build from strategy documents\n *\n * @param user - User database interface for accessing user state\n * @param course - Course database interface for accessing course data\n * @returns Configured Pipeline ready for use\n */\nexport function createDefaultPipeline(\n user: UserDBInterface,\n course: CourseDBInterface\n): Pipeline {\n const courseId = course.getCourseID();\n const eloNavigator = new ELONavigator(user, course, createDefaultEloStrategy(courseId));\n const srsNavigator = new SRSNavigator(user, course, createDefaultSrsStrategy(courseId));\n\n const compositeGenerator = new CompositeGenerator([eloNavigator, srsNavigator]);\n const eloDistanceFilter = createEloDistanceFilter();\n\n return new Pipeline(compositeGenerator, [eloDistanceFilter], user, course);\n}\n","import { CourseDBInterface, CourseInfo, CoursesDBInterface, UserDBInterface } from '@db/core';\nimport {\n CourseConfig,\n CourseElo,\n DataShape,\n EloToNumber,\n Status,\n blankCourseElo,\n toCourseElo,\n} from '@vue-skuilder/common';\n\nimport { filterAllDocsByPrefix, getCourseDB, getCourseDoc, getCourseDocs } from '.';\nimport UpdateQueue from './updateQueue';\nimport { StudySessionItem } from '../../core/interfaces/contentSource';\nimport {\n CardData,\n DocType,\n QualifiedCardID,\n SkuilderCourseData,\n Tag,\n TagStub,\n DocTypePrefixes,\n} from '../../core/types/types-legacy';\nimport { logger } from '../../util/logger';\nimport { GET_CACHED } from './clientCache';\nimport { addNote55, addTagToCard, getCredentialledCourseConfig, getTagID } from './courseAPI';\nimport { DataLayerResult } from '@db/core/types/db';\nimport { PouchError } from './types';\nimport CourseLookup from './courseLookupDB';\nimport { ContentNavigationStrategyData } from '@db/core/types/contentNavigationStrategy';\nimport { ContentNavigator, Navigators, WeightedCard } from '@db/core/navigators';\nimport { PipelineAssembler } from '@db/core/navigators/PipelineAssembler';\nimport { createDefaultPipeline } from '@db/core/navigators/defaults';\n\nexport class CoursesDB implements CoursesDBInterface {\n _courseIDs: string[] | undefined;\n\n constructor(courseIDs?: string[]) {\n if (courseIDs && courseIDs.length > 0) {\n this._courseIDs = courseIDs;\n } else {\n this._courseIDs = undefined;\n }\n }\n\n public async getCourseList(): Promise<CourseConfig[]> {\n let crsList = await CourseLookup.allCourseWare();\n logger.debug(`AllCourses: ${crsList.map((c) => c.name + ', ' + c._id + '\\n\\t')}`);\n if (this._courseIDs) {\n crsList = crsList.filter((c) => this._courseIDs!.includes(c._id));\n }\n\n logger.debug(`AllCourses.filtered: ${crsList.map((c) => c.name + ', ' + c._id + '\\n\\t')}`);\n\n const cfgs = await Promise.all(\n crsList.map(async (c) => {\n try {\n const cfg = await getCredentialledCourseConfig(c._id);\n logger.debug(`Found cfg: ${JSON.stringify(cfg)}`);\n return cfg;\n } catch (e) {\n logger.warn(`Error fetching cfg for course ${c.name}, ${c._id}: ${e}`);\n return undefined;\n }\n })\n );\n return cfgs.filter((c) => !!c);\n }\n\n async getCourseConfig(courseId: string): Promise<CourseConfig> {\n if (this._courseIDs && this._courseIDs.length && !this._courseIDs.includes(courseId)) {\n throw new Error(`Course ${courseId} not in course list`);\n }\n\n const cfg = await getCredentialledCourseConfig(courseId);\n if (cfg === undefined) {\n throw new Error(`Error fetching cfg for course ${courseId}`);\n } else {\n return cfg;\n }\n }\n\n public async disambiguateCourse(courseId: string, disambiguator: string): Promise<void> {\n await CourseLookup.updateDisambiguator(courseId, disambiguator);\n }\n}\n\nfunction randIntWeightedTowardZero(n: number) {\n return Math.floor(Math.random() * Math.random() * Math.random() * n);\n}\n\nexport class CourseDB implements CourseDBInterface {\n // private log(msg: string): void {\n // log(`CourseLog: ${this.id}\\n ${msg}`);\n // }\n\n private db: PouchDB.Database;\n private id: string;\n private _getCurrentUser: () => Promise<UserDBInterface>;\n private updateQueue: UpdateQueue;\n\n constructor(id: string, userLookup: () => Promise<UserDBInterface>) {\n this.id = id;\n this.db = getCourseDB(this.id);\n this._getCurrentUser = userLookup;\n this.updateQueue = new UpdateQueue(this.db);\n }\n\n public getCourseID(): string {\n return this.id;\n }\n\n public async getCourseInfo(): Promise<CourseInfo> {\n const cardCount = (\n await this.db.find({\n selector: {\n docType: DocType.CARD,\n },\n limit: 1000,\n })\n ).docs.length;\n\n return {\n cardCount,\n registeredUsers: 0,\n };\n }\n\n public async getInexperiencedCards(limit: number = 2) {\n return (\n await this.db.query('cardsByInexperience', {\n limit,\n })\n ).rows.map((r) => {\n const ret = {\n courseId: this.id,\n cardId: r.id,\n count: r.key,\n elo: r.value,\n };\n return ret;\n });\n }\n\n public async getCardsByEloLimits(\n options: {\n low: number;\n high: number;\n limit: number;\n page: number;\n } = {\n low: 0,\n high: Number.MIN_SAFE_INTEGER,\n limit: 25,\n page: 0,\n }\n ) {\n return (\n await this.db.query('elo', {\n startkey: options.low,\n endkey: options.high,\n limit: options.limit,\n skip: options.limit * options.page,\n })\n ).rows.map((r) => {\n return `${this.id}-${r.id}-${r.key}`;\n });\n }\n public async getCardEloData(id: string[]): Promise<CourseElo[]> {\n const docs = await this.db.allDocs<CardData>({\n keys: id,\n include_docs: true,\n });\n const ret: CourseElo[] = [];\n docs.rows.forEach((r) => {\n // [ ] remove these ts-ignore directives.\n if (isSuccessRow(r)) {\n if (r.doc && r.doc.elo) {\n ret.push(toCourseElo(r.doc.elo));\n } else {\n logger.warn('no elo data for card: ' + r.id);\n ret.push(blankCourseElo());\n }\n } else {\n logger.warn('no elo data for card: ' + JSON.stringify(r));\n ret.push(blankCourseElo());\n }\n });\n return ret;\n }\n\n /**\n * Returns the lowest and highest `global` ELO ratings in the course\n */\n public async getELOBounds() {\n const [low, high] = await Promise.all([\n (\n await this.db.query('elo', {\n startkey: 0,\n limit: 1,\n include_docs: false,\n })\n ).rows[0].key,\n (\n await this.db.query('elo', {\n limit: 1,\n descending: true,\n startkey: 100_000,\n })\n ).rows[0].key,\n ]);\n\n return {\n low: low,\n high: high,\n };\n }\n\n public async removeCard(id: string) {\n const doc = await this.db.get<CardData>(id);\n if (!doc.docType || !(doc.docType === DocType.CARD)) {\n throw new Error(`failed to remove ${id} from course ${this.id}. id does not point to a card`);\n }\n\n // Remove card from all associated tags before deleting the card\n try {\n const appliedTags = await this.getAppliedTags(id);\n const results = await Promise.allSettled(\n appliedTags.rows.map(async (tagRow) => {\n const tagId = tagRow.id;\n await this.removeTagFromCard(id, tagId);\n })\n );\n\n // Log any individual tag cleanup failures\n results.forEach((result, index) => {\n if (result.status === 'rejected') {\n const tagId = appliedTags.rows[index].id;\n logger.error(`Failed to remove card ${id} from tag ${tagId}: ${result.reason}`);\n }\n });\n } catch (error) {\n logger.error(`Error removing card ${id} from tags: ${error}`);\n // Continue with card deletion even if tag cleanup fails\n }\n\n return this.db.remove(doc);\n }\n\n public async getCardDisplayableDataIDs(id: string[]) {\n logger.debug(id.join(', '));\n const cards = await this.db.allDocs<CardData>({\n keys: id,\n include_docs: true,\n });\n const ret: { [card: string]: string[] } = {};\n cards.rows.forEach((r) => {\n if (isSuccessRow(r)) {\n ret[r.id] = r.doc!.id_displayable_data;\n }\n });\n\n return ret;\n }\n\n async getCardsByELO(elo: number, cardLimit?: number) {\n elo = parseInt(elo as any);\n const limit = cardLimit ? cardLimit : 25;\n\n const below: PouchDB.Query.Response<object> = await this.db.query('elo', {\n limit: Math.ceil(limit / 2),\n startkey: elo,\n descending: true,\n });\n\n const aboveLimit = limit - below.rows.length;\n\n const above: PouchDB.Query.Response<object> = await this.db.query('elo', {\n limit: aboveLimit,\n startkey: elo + 1,\n });\n // logger.log(JSON.stringify(below));\n\n let cards = below.rows;\n cards = cards.concat(above.rows);\n\n const ret = cards\n .sort((a, b) => {\n const s = Math.abs(a.key - elo) - Math.abs(b.key - elo);\n if (s === 0) {\n return Math.random() - 0.5;\n } else {\n return s;\n }\n })\n .map((c) => {\n return {\n courseID: this.id,\n cardID: c.id,\n elo: c.key,\n };\n });\n\n const str = `below:\\n${below.rows.map((r) => `\\t${r.id}-${r.key}\\n`)}\n\nabove:\\n${above.rows.map((r) => `\\t${r.id}-${r.key}\\n`)}`;\n\n logger.debug(`Getting ${limit} cards centered around elo: ${elo}:\\n\\n` + str);\n\n return ret;\n }\n\n async getCourseConfig(): Promise<CourseConfig> {\n const ret = await getCredentialledCourseConfig(this.id);\n if (ret) {\n return ret;\n } else {\n throw new Error(`Course config not found for course ID: ${this.id}`);\n }\n }\n\n async updateCourseConfig(cfg: CourseConfig): Promise<PouchDB.Core.Response> {\n logger.debug(`Updating: ${JSON.stringify(cfg)}`);\n // write both to the course DB:\n try {\n return await updateCredentialledCourseConfig(this.id, cfg);\n } catch (error) {\n logger.error(`Error updating course config in course DB: ${error}`);\n throw error;\n }\n }\n\n async updateCardElo(cardId: string, elo: CourseElo): Promise<PouchDB.Core.Response> {\n if (!elo) {\n throw new Error(`Cannot update card elo with null or undefined value for card ID: ${cardId}`);\n }\n\n try {\n const result = await this.updateQueue.update<\n CardData & PouchDB.Core.GetMeta & PouchDB.Core.IdMeta\n >(cardId, (card) => {\n logger.debug(`Replacing ${JSON.stringify(card.elo)} with ${JSON.stringify(elo)}`);\n card.elo = elo;\n return card;\n });\n return { ok: true, id: cardId, rev: result._rev };\n } catch (error) {\n logger.error(`Failed to update card elo for card ID: ${cardId}`, error);\n throw new Error(`Failed to update card elo for card ID: ${cardId}`);\n }\n }\n\n async getAppliedTags(cardId: string): Promise<PouchDB.Query.Response<TagStub>> {\n const ret = await getAppliedTags(this.id, cardId);\n if (ret) {\n return ret;\n } else {\n throw new Error(`Failed to find tags for card ${this.id}-${cardId}`);\n }\n }\n\n async getAppliedTagsBatch(cardIds: string[]): Promise<Map<string, string[]>> {\n if (cardIds.length === 0) {\n return new Map();\n }\n\n const db = getCourseDB(this.id);\n const result = await db.query<TagStub>('getTags', {\n keys: cardIds,\n include_docs: false,\n });\n\n const tagsByCard = new Map<string, string[]>();\n\n // Initialize all requested cards with empty arrays\n for (const cardId of cardIds) {\n tagsByCard.set(cardId, []);\n }\n\n // Populate from query results\n for (const row of result.rows) {\n const cardId = row.key as string;\n const tagName = row.value?.name;\n if (tagName && tagsByCard.has(cardId)) {\n tagsByCard.get(cardId)!.push(tagName);\n }\n }\n\n return tagsByCard;\n }\n\n async addTagToCard(\n cardId: string,\n tagId: string,\n updateELO?: boolean\n ): Promise<PouchDB.Core.Response> {\n return await addTagToCard(\n this.id,\n cardId,\n tagId,\n (await this._getCurrentUser()).getUsername(),\n updateELO\n );\n }\n\n async removeTagFromCard(cardId: string, tagId: string): Promise<PouchDB.Core.Response> {\n return await removeTagFromCard(this.id, cardId, tagId);\n }\n\n async createTag(name: string, author: string): Promise<PouchDB.Core.Response> {\n return await createTag(this.id, name, author);\n }\n\n async getTag(tagId: string): Promise<PouchDB.Core.GetMeta & PouchDB.Core.Document<Tag>> {\n return await getTag(this.id, tagId);\n }\n\n async updateTag(tag: Tag): Promise<PouchDB.Core.Response> {\n if (tag.course !== this.id) {\n throw new Error(`Tag ${JSON.stringify(tag)} does not belong to course ${this.id}`);\n }\n\n return await updateTag(tag);\n }\n\n async getCourseTagStubs(): Promise<PouchDB.Core.AllDocsResponse<Tag>> {\n return getCourseTagStubs(this.id);\n }\n\n async addNote(\n codeCourse: string,\n shape: DataShape,\n data: unknown,\n author: string,\n tags: string[],\n uploads?: { [key: string]: PouchDB.Core.FullAttachment },\n elo: CourseElo = blankCourseElo()\n ): Promise<DataLayerResult> {\n try {\n const resp = await addNote55(this.id, codeCourse, shape, data, author, tags, uploads, elo);\n if (resp.ok) {\n // Check if card creation failed (property added by addNote55)\n if ((resp as any).cardCreationFailed) {\n logger.warn(\n `[courseDB.addNote] Note added but card creation failed: ${\n (resp as any).cardCreationError\n }`\n );\n return {\n status: Status.error,\n message: `Note was added but no cards were created: ${(resp as any).cardCreationError}`,\n id: resp.id,\n };\n }\n return {\n status: Status.ok,\n message: '',\n id: resp.id,\n };\n } else {\n return {\n status: Status.error,\n message: 'Unexpected error adding note',\n };\n }\n } catch (e) {\n const err = e as PouchDB.Core.Error;\n logger.error(\n `[addNote] error ${err.name}\\n\\treason: ${err.reason}\\n\\tmessage: ${err.message}`\n );\n return {\n status: Status.error,\n message: `Error adding note to course. ${(e as PouchError).reason || err.message}`,\n };\n }\n }\n\n async getCourseDoc<T extends SkuilderCourseData>(\n id: string,\n options?: PouchDB.Core.GetOptions\n ): Promise<PouchDB.Core.GetMeta & PouchDB.Core.Document<T>> {\n return await getCourseDoc(this.id, id, options);\n }\n\n async getCourseDocs<T extends SkuilderCourseData>(\n ids: string[],\n options: PouchDB.Core.AllDocsOptions = {}\n ): Promise<PouchDB.Core.AllDocsWithKeysResponse<{} & T>> {\n return await getCourseDocs(this.id, ids, options);\n }\n\n ////////////////////////////////////\n // NavigationStrategyManager implementation\n ////////////////////////////////////\n\n getNavigationStrategy(id: string): Promise<ContentNavigationStrategyData> {\n logger.debug(`[courseDB] Getting navigation strategy: ${id}`);\n\n if (id == '') {\n const strategy: ContentNavigationStrategyData = {\n _id: 'NAVIGATION_STRATEGY-ELO',\n docType: DocType.NAVIGATION_STRATEGY,\n name: 'ELO',\n description: 'ELO-based navigation strategy for ordering content by difficulty',\n implementingClass: Navigators.ELO,\n course: this.id,\n serializedData: '', // serde is a noop for ELO navigator.\n };\n return Promise.resolve(strategy);\n } else {\n return this.db.get(id);\n }\n }\n\n async getAllNavigationStrategies(): Promise<ContentNavigationStrategyData[]> {\n const prefix = DocTypePrefixes[DocType.NAVIGATION_STRATEGY];\n const result = await this.db.allDocs<ContentNavigationStrategyData>({\n startkey: prefix,\n endkey: `${prefix}\\ufff0`,\n include_docs: true,\n });\n return result.rows.map((row) => row.doc!);\n }\n\n async addNavigationStrategy(data: ContentNavigationStrategyData): Promise<void> {\n logger.debug(`[courseDB] Adding navigation strategy: ${data._id}`);\n // // For now, just log the data and return success\n // logger.debug(JSON.stringify(data));\n return this.db.put(data).then(() => {});\n }\n updateNavigationStrategy(id: string, data: ContentNavigationStrategyData): Promise<void> {\n logger.debug(`[courseDB] Updating navigation strategy: ${id}`);\n // For now, just log the data and return success\n logger.debug(JSON.stringify(data));\n return Promise.resolve();\n }\n\n /**\n * Creates an instantiated navigator for this course.\n *\n * Handles multiple generators by wrapping them in CompositeGenerator.\n * This is the preferred method for getting a ready-to-use navigator.\n *\n * @param user - User database interface\n * @returns Instantiated ContentNavigator ready for use\n */\n async createNavigator(user: UserDBInterface): Promise<ContentNavigator> {\n try {\n const allStrategies = await this.getAllNavigationStrategies();\n\n if (allStrategies.length === 0) {\n // No strategies configured: use default Pipeline(Composite(ELO, SRS), [eloDistanceFilter])\n logger.debug(\n '[courseDB] No strategy documents found, using default Pipeline(Composite(ELO, SRS), [eloDistanceFilter])'\n );\n return createDefaultPipeline(user, this);\n }\n\n // Use PipelineAssembler to build a Pipeline from strategy documents\n const assembler = new PipelineAssembler();\n const { pipeline, generatorStrategies, filterStrategies, warnings } =\n await assembler.assemble({\n strategies: allStrategies,\n user,\n course: this,\n });\n\n // Log any warnings from assembly\n for (const warning of warnings) {\n logger.warn(`[PipelineAssembler] ${warning}`);\n }\n\n if (!pipeline) {\n // Assembly failed - fall back to default\n logger.debug('[courseDB] Pipeline assembly failed, using default pipeline');\n return createDefaultPipeline(user, this);\n }\n\n logger.debug(\n `[courseDB] Using assembled pipeline with ${generatorStrategies.length} generator(s) and ${filterStrategies.length} filter(s)`\n );\n return pipeline;\n } catch (e) {\n logger.error(`[courseDB] Error creating navigator: ${e}`);\n throw e;\n }\n }\n\n ////////////////////////////////////\n // END NavigationStrategyManager implementation\n ////////////////////////////////////\n\n ////////////////////////////////////\n // StudyContentSource implementation\n ////////////////////////////////////\n\n /**\n * Get cards with suitability scores for presentation.\n *\n * This is the PRIMARY API for content sources going forward. Delegates to the\n * course's configured NavigationStrategy to get scored candidates.\n *\n * @param limit - Maximum number of cards to return\n * @returns Cards sorted by score descending\n */\n public async getWeightedCards(limit: number): Promise<WeightedCard[]> {\n const u = await this._getCurrentUser();\n\n try {\n const navigator = await this.createNavigator(u);\n return navigator.getWeightedCards(limit);\n } catch (e) {\n logger.error(`[courseDB] Error getting weighted cards: ${e}`);\n throw e;\n }\n }\n\n public async getCardsCenteredAtELO(\n options: {\n limit: number;\n elo: 'user' | 'random' | number;\n } = {\n limit: 99,\n elo: 'user',\n },\n filter?: (a: QualifiedCardID) => boolean\n ): Promise<StudySessionItem[]> {\n let targetElo: number;\n\n if (options.elo === 'user') {\n const u = await this._getCurrentUser();\n\n targetElo = -1;\n try {\n const courseDoc = (await u.getCourseRegistrationsDoc()).courses.find((c) => {\n return c.courseID === this.id;\n })!;\n targetElo = EloToNumber(courseDoc.elo);\n } catch {\n targetElo = 1000;\n }\n } else if (options.elo === 'random') {\n const bounds = await GET_CACHED(`elo-bounds-${this.id}`, () => this.getELOBounds());\n targetElo = Math.round(bounds.low + Math.random() * (bounds.high - bounds.low));\n // logger.log(`Picked ${targetElo} from [${bounds.low}, ${bounds.high}]`);\n } else {\n targetElo = options.elo;\n }\n\n let cards: (QualifiedCardID & { elo?: number })[] = [];\n let mult: number = 4;\n let previousCount: number = -1;\n let newCount: number = 0;\n\n while (cards.length < options.limit && newCount !== previousCount) {\n cards = await this.getCardsByELO(targetElo, mult * options.limit);\n previousCount = newCount;\n newCount = cards.length;\n\n logger.debug(`Found ${cards.length} elo neighbor cards...`);\n\n if (filter) {\n cards = cards.filter(filter);\n logger.debug(`Filtered to ${cards.length} cards...`);\n }\n\n mult *= 2;\n }\n\n const selectedCards: {\n courseID: string;\n cardID: string;\n elo?: number;\n }[] = [];\n\n while (selectedCards.length < options.limit && cards.length > 0) {\n const index = randIntWeightedTowardZero(cards.length);\n const card = cards.splice(index, 1)[0];\n selectedCards.push(card);\n }\n\n return selectedCards.map((c) => {\n return {\n courseID: this.id,\n cardID: c.cardID,\n contentSourceType: 'course',\n contentSourceID: this.id,\n elo: c.elo,\n status: 'new',\n };\n });\n }\n\n // Admin search methods\n public async searchCards(query: string): Promise<any[]> {\n logger.log(`[CourseDB ${this.id}] Searching for: \"${query}\"`);\n\n // Try multiple search approaches\n let displayableData;\n\n try {\n // Try regex search on the correct data structure: data[0].data\n displayableData = await this.db.find({\n selector: {\n docType: 'DISPLAYABLE_DATA',\n 'data.0.data': { $regex: `.*${query}.*` },\n },\n });\n logger.log(`[CourseDB ${this.id}] Regex search on data[0].data successful`);\n } catch (regexError) {\n logger.log(\n `[CourseDB ${this.id}] Regex search failed, falling back to manual search:`,\n regexError\n );\n\n // Fallback: get all displayable data and filter manually\n const allDisplayable = await this.db.find({\n selector: {\n docType: 'DISPLAYABLE_DATA',\n },\n });\n\n logger.log(\n `[CourseDB ${this.id}] Retrieved ${allDisplayable.docs.length} documents for manual filtering`\n );\n\n displayableData = {\n docs: allDisplayable.docs.filter((doc) => {\n // Search entire document as JSON string - inclusive approach for admin tool\n const docString = JSON.stringify(doc).toLowerCase();\n const match = docString.includes(query.toLowerCase());\n if (match) {\n logger.log(`[CourseDB ${this.id}] Manual match found in document: ${doc._id}`);\n }\n return match;\n }),\n };\n }\n\n logger.log(\n `[CourseDB ${this.id}] Found ${displayableData.docs.length} displayable data documents`\n );\n\n if (displayableData.docs.length === 0) {\n // Debug: Let's see what displayable data exists\n const allDisplayableData = await this.db.find({\n selector: {\n docType: 'DISPLAYABLE_DATA',\n },\n limit: 5, // Just sample a few\n });\n\n logger.log(\n `[CourseDB ${this.id}] Sample displayable data:`,\n allDisplayableData.docs.map((d) => ({\n id: d._id,\n docType: (d as any).docType,\n dataStructure: (d as any).data ? Object.keys((d as any).data) : 'no data field',\n dataContent: (d as any).data,\n fullDoc: d,\n }))\n );\n }\n\n const allResults: any[] = [];\n\n for (const dd of displayableData.docs) {\n const cards = await this.db.find({\n selector: {\n docType: 'CARD',\n id_displayable_data: { $in: [dd._id] },\n },\n });\n\n logger.log(\n `[CourseDB ${this.id}] Displayable data ${dd._id} linked to ${cards.docs.length} cards`\n );\n allResults.push(...cards.docs);\n }\n\n logger.log(`[CourseDB ${this.id}] Total cards found: ${allResults.length}`);\n return allResults;\n }\n\n public async find(\n request: PouchDB.Find.FindRequest<any>\n ): Promise<PouchDB.Find.FindResponse<any>> {\n return this.db.find(request);\n }\n}\n\n/**\n * Returns a list of registered datashapes for the specified\n * course.\n * @param courseID The ID of the course\n */\nexport async function getCourseDataShapes(courseID: string) {\n const cfg = await getCredentialledCourseConfig(courseID);\n return cfg!.dataShapes;\n}\n\nexport async function getCredentialledDataShapes(courseID: string) {\n const cfg = await getCredentialledCourseConfig(courseID);\n\n return cfg.dataShapes;\n}\n\nexport async function getCourseQuestionTypes(courseID: string) {\n const cfg = await getCredentialledCourseConfig(courseID);\n return cfg!.questionTypes;\n}\n\n// todo: this is actually returning full tag docs now.\n// - performance issue when tags have lots of\n// applied docs\n// - will require a computed couch DB view\nexport async function getCourseTagStubs(\n courseID: string\n): Promise<PouchDB.Core.AllDocsResponse<Tag>> {\n logger.debug(`Getting tag stubs for course: ${courseID}`);\n const stubs = await filterAllDocsByPrefix<Tag>(\n getCourseDB(courseID),\n DocType.TAG.valueOf() + '-'\n );\n\n stubs.rows.forEach((row) => {\n logger.debug(`\\tTag stub for doc: ${row.id}`);\n });\n\n return stubs;\n}\n\nexport async function deleteTag(courseID: string, tagName: string) {\n tagName = getTagID(tagName);\n const courseDB = getCourseDB(courseID);\n const doc = await courseDB.get<Tag>(DocType.TAG.valueOf() + '-' + tagName);\n const resp = await courseDB.remove(doc);\n return resp;\n}\n\nexport async function createTag(courseID: string, tagName: string, author: string) {\n logger.debug(`Creating tag: ${tagName}...`);\n const tagID = getTagID(tagName);\n const courseDB = getCourseDB(courseID);\n const resp = await courseDB.put<Tag>({\n course: courseID,\n docType: DocType.TAG,\n name: tagName,\n snippet: '',\n taggedCards: [],\n wiki: '',\n author,\n _id: tagID,\n });\n return resp;\n}\n\nexport async function updateTag(tag: Tag) {\n const prior = await getTag(tag.course, tag.name);\n return await getCourseDB(tag.course).put<Tag>({\n ...tag,\n _rev: prior._rev,\n });\n}\n\nexport async function getTag(courseID: string, tagName: string) {\n const tagID = getTagID(tagName);\n const courseDB = getCourseDB(courseID);\n return courseDB.get<Tag>(tagID);\n}\n\nexport async function removeTagFromCard(courseID: string, cardID: string, tagID: string) {\n // todo: possible future perf. hit if tags have large #s of taggedCards.\n // In this case, should be converted to a server-request\n tagID = getTagID(tagID);\n const courseDB = getCourseDB(courseID);\n const tag = await courseDB.get<Tag>(tagID);\n tag.taggedCards = tag.taggedCards.filter((taggedID) => {\n return cardID !== taggedID;\n });\n return courseDB.put<Tag>(tag);\n}\n\n/**\n * Returns an array of ancestor tag IDs, where:\n * return[0] = parent,\n * return[1] = grandparent,\n * return[2] = great grandparent,\n * etc.\n *\n * If ret is empty, the tag itself is a root\n */\nexport function getAncestorTagIDs(courseID: string, tagID: string): string[] {\n tagID = getTagID(tagID);\n const split = tagID.split('>');\n if (split.length === 1) {\n return [];\n } else {\n split.pop();\n const parent = split.join('>');\n return [parent].concat(getAncestorTagIDs(courseID, parent));\n }\n}\n\nexport async function getChildTagStubs(courseID: string, tagID: string) {\n return await filterAllDocsByPrefix(getCourseDB(courseID), tagID + '>');\n}\n\nexport async function getAppliedTags(id_course: string, id_card: string) {\n const db = getCourseDB(id_course);\n\n const result = await db.query<TagStub>('getTags', {\n startkey: id_card,\n endkey: id_card,\n // include_docs: true\n });\n\n // log(`getAppliedTags looked up: ${id_card}`);\n // log(`getAppliedTags returning: ${JSON.stringify(result)}`);\n\n return result;\n}\n\nexport async function updateCardElo(courseID: string, cardID: string, elo: CourseElo) {\n if (elo) {\n // checking against null, undefined, NaN\n const cDB = getCourseDB(courseID);\n const card = await cDB.get<CardData>(cardID);\n logger.debug(`Replacing ${JSON.stringify(card.elo)} with ${JSON.stringify(elo)}`);\n card.elo = elo;\n return cDB.put(card); // race conditions - is it important? probably not (net-zero effect)\n }\n}\n\nexport async function updateCredentialledCourseConfig(courseID: string, config: CourseConfig) {\n logger.debug(`Updating course config:\n\n${JSON.stringify(config)}\n`);\n\n const db = getCourseDB(courseID);\n const old = await getCredentialledCourseConfig(courseID);\n\n return await db.put<CourseConfig>({\n ...config,\n _rev: (old as any)._rev,\n });\n}\n\nfunction isSuccessRow<T>(\n row:\n | {\n key: PouchDB.Core.DocumentKey;\n error: 'not_found';\n }\n | {\n doc?: PouchDB.Core.ExistingDocument<PouchDB.Core.AllDocsMeta & T> | null | undefined;\n id: PouchDB.Core.DocumentId;\n key: PouchDB.Core.DocumentKey;\n value: {\n rev: PouchDB.Core.RevisionId;\n deleted?: boolean | undefined;\n };\n }\n): row is {\n doc?: PouchDB.Core.ExistingDocument<PouchDB.Core.AllDocsMeta & T> | null | undefined;\n id: PouchDB.Core.DocumentId;\n key: PouchDB.Core.DocumentKey;\n value: {\n rev: PouchDB.Core.RevisionId;\n deleted?: boolean | undefined;\n };\n} {\n return 'doc' in row && row.doc !== null && row.doc !== undefined;\n}\n","import { StudyContentSource } from '@db/core/interfaces/contentSource';\nimport { WeightedCard } from '@db/core/navigators';\nimport { ClassroomConfig } from '@vue-skuilder/common';\nimport { ENV } from '@db/factory';\nimport { logger } from '@db/util/logger';\nimport moment from 'moment';\nimport pouch from './pouchdb-setup';\nimport { getStartAndEndKeys, createPouchDBConfig, REVIEW_TIME_FORMAT } from '.';\nimport { CourseDB, getTag } from './courseDB';\n\nimport { UserDBInterface } from '@db/core';\nimport {\n AssignedContent,\n AssignedCourse,\n AssignedTag,\n StudentClassroomDBInterface,\n TeacherClassroomDBInterface,\n} from '@db/core/interfaces/classroomDB';\n\nconst classroomLookupDBTitle = 'classdb-lookup';\nexport const CLASSROOM_CONFIG = 'ClassroomConfig';\n\nexport type ClassroomMessage = object;\n\nabstract class ClassroomDBBase {\n public _id!: string;\n protected _db!: PouchDB.Database;\n protected _cfg!: ClassroomConfig;\n protected _initComplete: boolean = false;\n\n protected readonly _content_prefix: string = 'content';\n protected get _content_searchkeys() {\n return getStartAndEndKeys(this._content_prefix);\n }\n\n protected abstract init(): Promise<void>;\n\n public async getAssignedContent(): Promise<AssignedContent[]> {\n logger.info(`Getting assigned content...`);\n // see couchdb docs 6.2.2:\n // Guide to Views -> Views Collation -> String Ranges\n const docRows = await this._db.allDocs<AssignedContent>({\n startkey: this._content_prefix,\n endkey: this._content_prefix + `\\ufff0`,\n include_docs: true,\n });\n\n const ret = docRows.rows.map((row) => {\n return row.doc!;\n });\n // logger.info(`Assigned content: ${JSON.stringify(ret)}`);\n\n return ret;\n }\n\n protected getContentId(content: AssignedContent): string {\n if (content.type === 'tag') {\n return `${this._content_prefix}-${content.courseID}-${content.tagID}`;\n } else {\n return `${this._content_prefix}-${content.courseID}`;\n }\n }\n\n public get ready(): boolean {\n return this._initComplete;\n }\n public getConfig(): ClassroomConfig {\n return this._cfg;\n }\n}\n\nexport class StudentClassroomDB\n extends ClassroomDBBase\n implements StudyContentSource, StudentClassroomDBInterface\n{\n // private readonly _prefix: string = 'content';\n private userMessages!: PouchDB.Core.Changes<object>;\n private _user: UserDBInterface;\n\n private constructor(classID: string, user: UserDBInterface) {\n super();\n this._id = classID;\n this._user = user;\n // init() is called explicitly in factory method, not in constructor\n }\n\n async init(): Promise<void> {\n const dbName = `classdb-student-${this._id}`;\n this._db = new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,\n createPouchDBConfig()\n );\n try {\n const cfg = await this._db.get<ClassroomConfig>(CLASSROOM_CONFIG);\n this._cfg = cfg;\n this.userMessages = this._db.changes({\n since: 'now',\n live: true,\n include_docs: true,\n });\n this._initComplete = true;\n return;\n } catch (e) {\n throw new Error(`Error in StudentClassroomDB constructor: ${JSON.stringify(e)}`);\n }\n }\n\n public static async factory(classID: string, user: UserDBInterface): Promise<StudentClassroomDB> {\n const ret = new StudentClassroomDB(classID, user);\n await ret.init();\n return ret;\n }\n\n public setChangeFcn(f: (value: unknown) => object): void {\n // todo: make this into a view request, w/ the user's name attached\n // todo: requires creating the view doc on classroom create in /express\n void this.userMessages.on('change', f);\n }\n\n /**\n * Get cards with suitability scores for presentation.\n *\n * Gathers new cards from assigned content (courses, tags, cards) and\n * pending reviews scheduled for this classroom. Assigns score=1.0 to all.\n *\n * @param limit - Maximum number of cards to return\n * @returns Cards sorted by score descending (all scores = 1.0)\n */\n public async getWeightedCards(limit: number): Promise<WeightedCard[]> {\n const weighted: WeightedCard[] = [];\n\n // Get pending reviews for this classroom\n const allUserReviews = await this._user.getPendingReviews();\n const classroomReviews = allUserReviews.filter(\n (r) => r.scheduledFor === 'classroom' && r.schedulingAgentId === this._id\n );\n\n for (const r of classroomReviews) {\n weighted.push({\n cardId: r.cardId,\n courseId: r.courseId,\n score: 1.0,\n reviewID: r._id,\n provenance: [\n {\n strategy: 'classroom',\n strategyName: 'Classroom',\n strategyId: 'CLASSROOM',\n action: 'generated' as const,\n score: 1.0,\n reason: 'Classroom scheduled review',\n },\n ],\n });\n }\n\n // Get new cards from assigned content\n const activeCards = await this._user.getActiveCards();\n const activeCardIds = new Set(activeCards.map((ac) => ac.cardID));\n const now = moment.utc();\n const assigned = await this.getAssignedContent();\n const due = assigned.filter((c) => now.isAfter(moment.utc(c.activeOn, REVIEW_TIME_FORMAT)));\n\n logger.info(`[StudentClassroomDB] Due content: ${JSON.stringify(due)}`);\n\n for (const content of due) {\n if (content.type === 'course') {\n // Get weighted cards from the course directly\n const db = new CourseDB(content.courseID, async () => this._user);\n const courseCards = await db.getWeightedCards(limit);\n for (const card of courseCards) {\n if (!activeCardIds.has(card.cardId)) {\n weighted.push({\n ...card,\n provenance: [\n ...card.provenance,\n {\n strategy: 'classroom',\n strategyName: 'Classroom',\n strategyId: 'CLASSROOM',\n action: 'passed' as const,\n score: card.score,\n reason: `Assigned via classroom from course ${content.courseID}`,\n },\n ],\n });\n }\n }\n } else if (content.type === 'tag') {\n const tagDoc = await getTag(content.courseID, content.tagID);\n\n for (const cardId of tagDoc.taggedCards) {\n if (!activeCardIds.has(cardId)) {\n weighted.push({\n cardId,\n courseId: content.courseID,\n score: 1.0,\n provenance: [\n {\n strategy: 'classroom',\n strategyName: 'Classroom',\n strategyId: 'CLASSROOM',\n action: 'generated' as const,\n score: 1.0,\n reason: `Classroom assigned tag: ${content.tagID}, new card`,\n },\n ],\n });\n }\n }\n } else if (content.type === 'card') {\n if (!activeCardIds.has(content.cardID)) {\n weighted.push({\n cardId: content.cardID,\n courseId: content.courseID,\n score: 1.0,\n provenance: [\n {\n strategy: 'classroom',\n strategyName: 'Classroom',\n strategyId: 'CLASSROOM',\n action: 'generated' as const,\n score: 1.0,\n reason: 'Classroom assigned card, new card',\n },\n ],\n });\n }\n }\n }\n\n logger.info(\n `[StudentClassroomDB] New cards from classroom ${this._cfg.name}: ` +\n `${weighted.length} total (reviews + new)`\n );\n\n // Sort by score descending and limit\n return weighted.sort((a, b) => b.score - a.score).slice(0, limit);\n }\n}\n\n/**\n * Interface for managing a classroom.\n */\nexport class TeacherClassroomDB extends ClassroomDBBase implements TeacherClassroomDBInterface {\n private _stuDb!: PouchDB.Database;\n\n private constructor(classID: string) {\n super();\n this._id = classID;\n }\n\n async init(): Promise<void> {\n const dbName = `classdb-teacher-${this._id}`;\n const stuDbName = `classdb-student-${this._id}`;\n this._db = new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,\n createPouchDBConfig()\n );\n this._stuDb = new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + stuDbName,\n createPouchDBConfig()\n );\n try {\n return this._db\n .get<ClassroomConfig>(CLASSROOM_CONFIG)\n .then((cfg) => {\n this._cfg = cfg;\n this._initComplete = true;\n })\n .then(() => {\n return;\n });\n } catch (e) {\n throw new Error(`Error in TeacherClassroomDB constructor: ${JSON.stringify(e)}`);\n }\n }\n\n public static async factory(classID: string): Promise<TeacherClassroomDB> {\n const ret = new TeacherClassroomDB(classID);\n await ret.init();\n return ret;\n }\n\n public async removeContent(content: AssignedContent): Promise<void> {\n const contentID = this.getContentId(content);\n\n try {\n const doc = await this._db.get(contentID);\n await this._db.remove(doc);\n void this._db.replicate.to(this._stuDb, {\n doc_ids: [contentID],\n });\n } catch (error) {\n logger.error('Failed to remove content:', contentID, error);\n }\n }\n\n public async assignContent(content: AssignedContent): Promise<boolean> {\n let put: PouchDB.Core.Response;\n const id: string = this.getContentId(content);\n\n if (content.type === 'tag') {\n put = await this._db.put<AssignedTag>({\n courseID: content.courseID,\n tagID: content.tagID,\n type: 'tag',\n _id: id,\n assignedBy: content.assignedBy,\n assignedOn: moment.utc(),\n activeOn: content.activeOn || moment.utc(),\n });\n } else {\n put = await this._db.put<AssignedCourse>({\n courseID: content.courseID,\n type: 'course',\n _id: id,\n assignedBy: content.assignedBy,\n assignedOn: moment.utc(),\n activeOn: content.activeOn || moment.utc(),\n });\n }\n\n if (put.ok) {\n void this._db.replicate.to(this._stuDb, {\n doc_ids: [id],\n });\n return true;\n } else {\n return false;\n }\n }\n}\n\nexport const ClassroomLookupDB: () => PouchDB.Database = () =>\n new pouch(ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + classroomLookupDBTitle, {\n skip_setup: true,\n });\n\nexport function getClassroomDB(classID: string, version: 'student' | 'teacher'): PouchDB.Database {\n const dbName = `classdb-${version}-${classID}`;\n logger.info(`Retrieving classroom db: ${dbName}`);\n\n return new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,\n createPouchDBConfig()\n );\n}\n\nexport async function getClassroomConfig(classID: string): Promise<ClassroomConfig> {\n return await getClassroomDB(classID, 'student').get<ClassroomConfig>(CLASSROOM_CONFIG);\n}\n","import { StudyContentSource } from '@db/core/interfaces/contentSource';\nimport { WeightedCard } from '@db/core/navigators';\nimport { UserDBInterface } from '@db/core';\nimport { TagFilter, hasActiveFilter } from '@vue-skuilder/common';\nimport { getTag } from '../impl/couch/courseDB';\nimport { logger } from '@db/util/logger';\n\n/**\n * A StudyContentSource that filters cards based on tag inclusion/exclusion.\n *\n * This enables ephemeral, tag-scoped study sessions where users can focus\n * on specific topics within a course without permanent configuration.\n *\n * Filter logic:\n * - If `include` is non-empty: card must have at least one of the included tags\n * - If `exclude` is non-empty: card must not have any of the excluded tags\n * - Both filters are applied (include first, then exclude)\n */\nexport class TagFilteredContentSource implements StudyContentSource {\n private courseId: string;\n private filter: TagFilter;\n private user: UserDBInterface;\n\n // Cache resolved card IDs to avoid repeated lookups within a session\n private resolvedCardIds: Set<string> | null = null;\n\n constructor(courseId: string, filter: TagFilter, user: UserDBInterface) {\n this.courseId = courseId;\n this.filter = filter;\n this.user = user;\n\n logger.info(\n `[TagFilteredContentSource] Created for course \"${courseId}\" with filter:`,\n JSON.stringify(filter)\n );\n }\n\n /**\n * Resolves the TagFilter to a set of eligible card IDs.\n *\n * - Cards in `include` tags are OR'd together (card needs at least one)\n * - Cards in `exclude` tags are removed from the result\n */\n private async resolveFilteredCardIds(): Promise<Set<string>> {\n // Return cached result if available\n if (this.resolvedCardIds !== null) {\n return this.resolvedCardIds;\n }\n\n const includedCardIds = new Set<string>();\n\n // Build inclusion set (OR of all include tags)\n if (this.filter.include.length > 0) {\n for (const tagName of this.filter.include) {\n try {\n const tagDoc = await getTag(this.courseId, tagName);\n tagDoc.taggedCards.forEach((cardId) => includedCardIds.add(cardId));\n } catch (error) {\n logger.warn(\n `[TagFilteredContentSource] Could not resolve tag \"${tagName}\" for inclusion:`,\n error\n );\n }\n }\n }\n\n // If no include tags specified or none resolved, return empty set\n // (requiring explicit inclusion prevents \"study everything\" on empty filter)\n if (includedCardIds.size === 0 && this.filter.include.length > 0) {\n logger.warn(\n `[TagFilteredContentSource] No cards found for include tags: ${this.filter.include.join(', ')}`\n );\n this.resolvedCardIds = new Set();\n return this.resolvedCardIds;\n }\n\n // Build exclusion set\n const excludedCardIds = new Set<string>();\n if (this.filter.exclude.length > 0) {\n for (const tagName of this.filter.exclude) {\n try {\n const tagDoc = await getTag(this.courseId, tagName);\n tagDoc.taggedCards.forEach((cardId) => excludedCardIds.add(cardId));\n } catch (error) {\n logger.warn(\n `[TagFilteredContentSource] Could not resolve tag \"${tagName}\" for exclusion:`,\n error\n );\n }\n }\n }\n\n // Apply exclusion filter\n const finalCardIds = new Set<string>();\n for (const cardId of includedCardIds) {\n if (!excludedCardIds.has(cardId)) {\n finalCardIds.add(cardId);\n }\n }\n\n logger.info(\n `[TagFilteredContentSource] Resolved ${finalCardIds.size} cards ` +\n `(included: ${includedCardIds.size}, excluded: ${excludedCardIds.size})`\n );\n\n this.resolvedCardIds = finalCardIds;\n return finalCardIds;\n }\n\n /**\n * Get cards with suitability scores for presentation.\n *\n * Filters cards by tag inclusion/exclusion and assigns score=1.0 to all.\n * TagFilteredContentSource does not currently support pluggable navigation\n * strategies - it returns flat-scored candidates.\n *\n * @param limit - Maximum number of cards to return\n * @returns Cards sorted by score descending (all scores = 1.0)\n */\n public async getWeightedCards(limit: number): Promise<WeightedCard[]> {\n if (!hasActiveFilter(this.filter)) {\n logger.warn('[TagFilteredContentSource] getWeightedCards called with no active filter');\n return [];\n }\n\n const eligibleCardIds = await this.resolveFilteredCardIds();\n\n // Get new cards: eligible cards that are not already active\n const activeCards = await this.user.getActiveCards();\n const activeCardIds = new Set(activeCards.map((c) => c.cardID));\n\n const newCardWeighted: WeightedCard[] = [];\n for (const cardId of eligibleCardIds) {\n if (!activeCardIds.has(cardId)) {\n newCardWeighted.push({\n cardId,\n courseId: this.courseId,\n score: 1.0,\n provenance: [\n {\n strategy: 'tagFilter',\n strategyName: 'Tag Filter',\n strategyId: 'TAG_FILTER',\n action: 'generated' as const,\n score: 1.0,\n reason: `Tag-filtered new card (tags: ${this.filter.include.join(', ')})`,\n },\n ],\n });\n }\n\n if (newCardWeighted.length >= limit) {\n break;\n }\n }\n\n logger.info(\n `[TagFilteredContentSource] Found ${newCardWeighted.length} new cards matching filter`\n );\n\n // Get pending reviews: reviews for cards in the eligible set\n const allReviews = await this.user.getPendingReviews(this.courseId);\n const filteredReviews = allReviews.filter((review) => eligibleCardIds.has(review.cardId));\n\n logger.info(\n `[TagFilteredContentSource] Found ${filteredReviews.length} pending reviews matching filter ` +\n `(of ${allReviews.length} total)`\n );\n\n const reviewWeighted: WeightedCard[] = filteredReviews.map((r) => ({\n cardId: r.cardId,\n courseId: r.courseId,\n score: 1.0,\n reviewID: r._id,\n provenance: [\n {\n strategy: 'tagFilter',\n strategyName: 'Tag Filter',\n strategyId: 'TAG_FILTER',\n action: 'generated' as const,\n score: 1.0,\n reason: `Tag-filtered review (tags: ${this.filter.include.join(', ')})`,\n },\n ],\n }));\n\n // Reviews first, then new cards; respect limit\n return [...reviewWeighted, ...newCardWeighted].slice(0, limit);\n }\n\n /**\n * Clears the cached resolved card IDs.\n * Call this if the underlying tag data may have changed during a session.\n */\n public clearCache(): void {\n this.resolvedCardIds = null;\n }\n\n /**\n * Returns the course ID this source is filtering.\n */\n public getCourseId(): string {\n return this.courseId;\n }\n\n /**\n * Returns the active tag filter.\n */\n public getFilter(): TagFilter {\n return this.filter;\n }\n}\n","import { getDataLayer } from '@db/factory';\nimport { UserDBInterface } from '..';\nimport { StudentClassroomDB } from '../../impl/couch/classroomDB';\nimport { WeightedCard } from '../navigators';\nimport { TagFilter, hasActiveFilter } from '@vue-skuilder/common';\nimport { TagFilteredContentSource } from '../../study/TagFilteredContentSource';\n\nexport type StudySessionFailedItem = StudySessionFailedNewItem | StudySessionFailedReviewItem;\n\nexport interface StudySessionFailedNewItem extends StudySessionItem {\n status: 'failed-new';\n}\nexport interface StudySessionFailedReviewItem extends StudySessionReviewItem {\n status: 'failed-review';\n}\n\nexport interface StudySessionNewItem extends StudySessionItem {\n status: 'new';\n}\n\nexport interface StudySessionReviewItem extends StudySessionItem {\n reviewID: string;\n status: 'review' | 'failed-review';\n}\nexport function isReview(item: StudySessionItem): item is StudySessionReviewItem {\n const ret = item.status === 'review' || item.status === 'failed-review' || 'reviewID' in item;\n\n // console.log(`itemIsReview: ${ret}\n // \\t${JSON.stringify(item)}`);\n\n return ret;\n}\n\nexport interface StudySessionItem {\n status: 'new' | 'review' | 'failed-new' | 'failed-review';\n contentSourceType: 'course' | 'classroom';\n contentSourceID: string;\n // qualifiedID: `${string}-${string}` | `${string}-${string}-${number}`;\n cardID: string;\n courseID: string;\n elo?: number;\n // reviewID?: string;\n}\n\nexport interface ContentSourceID {\n type: 'course' | 'classroom';\n id: string;\n /**\n * Optional tag filter for scoped study sessions.\n * When present, creates a TagFilteredContentSource instead of a regular course source.\n */\n tagFilter?: TagFilter;\n}\n\n// #region docs_StudyContentSource\n/**\n * Interface for sources that provide study content to SessionController.\n *\n * Content sources return scored candidates via getWeightedCards(), which\n * SessionController uses to populate study queues.\n *\n * See: packages/db/docs/navigators-architecture.md\n */\nexport interface StudyContentSource {\n /**\n * Get cards with suitability scores for presentation.\n *\n * Returns unified scored candidates that can be sorted and selected by SessionController.\n * The card origin ('new' | 'review' | 'failed') is determined by provenance metadata.\n *\n * @param limit - Maximum number of cards to return\n * @returns Cards sorted by score descending\n */\n getWeightedCards(limit: number): Promise<WeightedCard[]>;\n}\n// #endregion docs_StudyContentSource\n\nexport async function getStudySource(\n source: ContentSourceID,\n user: UserDBInterface\n): Promise<StudyContentSource> {\n if (source.type === 'classroom') {\n return await StudentClassroomDB.factory(source.id, user);\n } else {\n // Check if this is a tag-filtered course source\n if (hasActiveFilter(source.tagFilter)) {\n return new TagFilteredContentSource(source.id, source.tagFilter!, user);\n }\n\n // Regular course source\n return getDataLayer().getCourseDB(source.id) as unknown as StudyContentSource;\n }\n}\n","import { CourseConfig, CourseElo, DataShape, SkuilderCourseData } from '@vue-skuilder/common';\nimport { StudyContentSource, StudySessionItem } from './contentSource';\nimport { TagStub, Tag, QualifiedCardID } from '../types/types-legacy';\nimport { DataLayerResult } from '../types/db';\nimport { NavigationStrategyManager } from './navigationStrategyManager';\n\n/**\n * Course content and management\n */\nexport interface CoursesDBInterface {\n /**\n * Get course config\n */\n getCourseConfig(courseId: string): Promise<CourseConfig>;\n\n /**\n * Get a list of all courses\n */\n getCourseList(): Promise<CourseConfig[]>;\n\n disambiguateCourse(courseId: string, disambiguator: string): Promise<void>;\n}\n\nexport interface CourseInfo {\n cardCount: number;\n registeredUsers: number;\n}\n\nexport interface CourseDBInterface extends NavigationStrategyManager, StudyContentSource {\n /**\n * Get course config\n */\n getCourseConfig(): Promise<CourseConfig>;\n\n getCourseID(): string;\n\n /**\n * Set course config\n */\n updateCourseConfig(cfg: CourseConfig): Promise<PouchDB.Core.Response>;\n\n getCourseInfo(): Promise<CourseInfo>;\n\n getCourseDoc<T extends SkuilderCourseData>(\n id: string,\n options?: PouchDB.Core.GetOptions\n ): Promise<T>;\n getCourseDocs<T extends SkuilderCourseData>(\n ids: string[],\n options?: PouchDB.Core.AllDocsOptions\n ): Promise<PouchDB.Core.AllDocsWithKeysResponse<{} & T>>;\n\n /**\n * Get cards sorted by ELO rating\n */\n getCardsByELO(\n elo: number,\n limit?: number\n ): Promise<\n {\n courseID: string;\n cardID: string;\n elo?: number;\n }[]\n >;\n\n /**\n * Get ELO data for specific cards\n */\n getCardEloData(cardIds: string[]): Promise<CourseElo[]>;\n\n /**\n * Update card ELO rating\n */\n updateCardElo(cardId: string, elo: CourseElo): Promise<PouchDB.Core.Response>;\n\n /**\n * Get cards centered at a particular ELO rating\n */\n getCardsCenteredAtELO(\n options: { limit: number; elo: 'user' | 'random' | number },\n filter?: (card: QualifiedCardID) => boolean\n ): Promise<StudySessionItem[]>;\n\n /**\n * Get tags for a card\n */\n getAppliedTags(cardId: string): Promise<PouchDB.Query.Response<TagStub>>;\n\n /**\n * Get tags for multiple cards in a single batch query.\n * More efficient than calling getAppliedTags() for each card.\n *\n * This method reduces redundant database operations when multiple filters\n * need tag data for the same cards. The Pipeline uses this to pre-hydrate\n * tags on WeightedCard objects before filters run.\n *\n * @param cardIds - Array of card IDs to fetch tags for\n * @returns Map from cardId to array of tag names\n */\n getAppliedTagsBatch(cardIds: string[]): Promise<Map<string, string[]>>;\n\n /**\n * Add a tag to a card\n */\n addTagToCard(cardId: string, tagId: string, updateELO?: boolean): Promise<PouchDB.Core.Response>;\n\n /**\n * Remove a tag from a card\n */\n removeTagFromCard(cardId: string, tagId: string): Promise<PouchDB.Core.Response>;\n\n /**\n * Create a new tag\n */\n createTag(tagName: string, author: string): Promise<PouchDB.Core.Response>;\n\n /**\n * Get a tag by name\n */\n getTag(tagName: string): Promise<Tag>;\n\n /**\n * Update a tag\n */\n updateTag(tag: Tag): Promise<PouchDB.Core.Response>;\n\n /**\n * Get all tag stubs for a course\n */\n getCourseTagStubs(): Promise<PouchDB.Core.AllDocsResponse<Tag>>;\n\n /**\n * Add a note to the course\n */\n addNote(\n codeCourse: string,\n shape: DataShape,\n data: unknown,\n author: string,\n tags: string[],\n uploads?: { [key: string]: PouchDB.Core.FullAttachment },\n elo?: CourseElo\n ): Promise<DataLayerResult>;\n\n removeCard(cardId: string): Promise<PouchDB.Core.Response>;\n\n getInexperiencedCards(): Promise<\n {\n courseId: string;\n cardId: string;\n count: number;\n elo: CourseElo;\n }[]\n >;\n\n /**\n * Search for cards by text content\n * @param query Text to search for\n * @returns Array of matching card data\n */\n searchCards(query: string): Promise<any[]>;\n\n /**\n * Find documents using PouchDB query syntax\n * @param request PouchDB find request\n * @returns Query response\n */\n find(request: PouchDB.Find.FindRequest<any>): Promise<PouchDB.Find.FindResponse<any>>;\n}\n","// db/src/core/interfaces.ts\n\nimport { UserDBInterface, UserDBReader } from './userDB';\nimport { CourseDBInterface, CoursesDBInterface } from './courseDB';\nimport { ClassroomDBInterface } from './classroomDB';\nimport { AdminDBInterface } from './adminDB';\n\n/**\n * Main factory interface for data access\n */\nexport interface DataLayerProvider {\n /**\n * Get the user database interface\n */\n getUserDB(): UserDBInterface;\n\n /**\n * Create a UserDBReader for a specific user (admin access required)\n * Uses session authentication to verify requesting user is admin\n * @param targetUsername - The username to create a reader for\n * @throws Error if requesting user is not 'admin'\n */\n createUserReaderForUser(targetUsername: string): Promise<UserDBReader>;\n\n /**\n * Get a course database interface\n */\n getCourseDB(courseId: string): CourseDBInterface;\n\n /**\n * Get the courses-lookup interface\n */\n getCoursesDB(): CoursesDBInterface;\n\n /**\n * Get a classroom database interface\n */\n getClassroomDB(classId: string, type: 'student' | 'teacher'): Promise<ClassroomDBInterface>;\n\n /**\n * Get the admin database interface\n */\n getAdminDB(): AdminDBInterface;\n\n /**\n * Initialize the data layer\n */\n initialize(): Promise<void>;\n\n /**\n * Teardown the data layer\n */\n teardown(): Promise<void>;\n\n /**\n * Check if this data layer is read-only\n */\n isReadOnly(): boolean;\n}\n","import {\n ActivityRecord,\n CourseRegistration,\n CourseRegistrationDoc,\n ScheduledCard,\n} from '@db/core/types/user';\nimport { CourseElo, Status } from '@vue-skuilder/common';\nimport { Moment } from 'moment';\nimport { CardHistory, CardRecord, QualifiedCardID } from '../types/types-legacy';\nimport { UserConfig } from '../types/user';\nimport { DocumentUpdater } from '@db/study';\n\n/**\n * Read-only user data operations\n */\nexport interface UserDBReader {\n get<T>(id: string): Promise<T & PouchDB.Core.RevisionIdMeta>;\n getUsername(): string;\n isLoggedIn(): boolean;\n\n /**\n * Get user configuration\n */\n getConfig(): Promise<UserConfig>;\n\n /**\n * Get cards that the user has seen\n */\n getSeenCards(courseId?: string): Promise<string[]>;\n\n /**\n * Get cards that are actively scheduled for review\n */\n getActiveCards(): Promise<QualifiedCardID[]>;\n\n /**\n * Get user's course registrations\n */\n getCourseRegistrationsDoc(): Promise<CourseRegistrationDoc>;\n\n /**\n * Get the registration doc for a specific course.\n * @param courseId\n */\n getCourseRegDoc(courseId: string): Promise<CourseRegistration>;\n\n /**\n * Get user's active courses\n */\n getActiveCourses(): Promise<CourseRegistration[]>;\n\n /**\n * Get user's pending reviews\n */\n getPendingReviews(courseId?: string): Promise<ScheduledCard[]>;\n\n getActivityRecords(): Promise<ActivityRecord[]>;\n\n /**\n * Get strategy-specific state for a course.\n *\n * Strategies use this to persist preferences, learned patterns, or temporal\n * tracking data across sessions. Each strategy owns its own namespace.\n *\n * @param courseId - The course this state applies to\n * @param strategyKey - Unique key identifying the strategy (typically class name)\n * @returns The strategy's data payload, or null if no state exists\n */\n getStrategyState<T>(courseId: string, strategyKey: string): Promise<T | null>;\n\n /**\n * Get user's classroom registrations\n */\n getUserClassrooms(): Promise<ClassroomRegistrationDoc>;\n\n /**\n * Get user's active classes\n */\n getActiveClasses(): Promise<string[]>;\n\n getCourseInterface(courseId: string): Promise<UsrCrsDataInterface>;\n}\n\n/**\n * User data mutation operations\n */\nexport interface UserDBWriter extends DocumentUpdater {\n /**\n * Update user configuration\n */\n setConfig(config: Partial<UserConfig>): Promise<void>;\n\n /**\n * Record a user's interaction with a card\n */\n putCardRecord<T extends CardRecord>(record: T): Promise<CardHistory<CardRecord>>;\n\n /**\n * Register user for a course\n */\n registerForCourse(courseId: string, previewMode?: boolean): Promise<PouchDB.Core.Response>;\n\n /**\n * Drop a course registration\n */\n dropCourse(courseId: string, dropStatus?: string): Promise<PouchDB.Core.Response>;\n\n /**\n * Schedule a card for review\n */\n scheduleCardReview(review: {\n user: string;\n course_id: string;\n card_id: string;\n time: Moment;\n scheduledFor: 'course' | 'classroom';\n schedulingAgentId: string;\n }): Promise<void>;\n\n /**\n * Remove a scheduled card review\n */\n removeScheduledCardReview(reviewId: string): Promise<void>;\n\n /**\n * Register user for a classroom\n */\n registerForClassroom(\n classId: string,\n registerAs: 'student' | 'teacher' | 'aide' | 'admin'\n ): Promise<PouchDB.Core.Response>;\n\n /**\n * Drop user from classroom\n */\n dropFromClassroom(classId: string): Promise<PouchDB.Core.Response>;\n\n /**\n * Update user's ELO rating for a course\n */\n updateUserElo(courseId: string, elo: CourseElo): Promise<PouchDB.Core.Response>;\n\n /**\n * Reset all user data (progress, registrations, etc.) while preserving authentication\n */\n resetUserData(): Promise<{ status: Status; error?: string }>;\n\n /**\n * Store strategy-specific state for a course.\n *\n * Strategies use this to persist preferences, learned patterns, or temporal\n * tracking data across sessions. Each strategy owns its own namespace.\n *\n * @param courseId - The course this state applies to\n * @param strategyKey - Unique key identifying the strategy (typically class name)\n * @param data - The strategy's data payload to store\n */\n putStrategyState<T>(courseId: string, strategyKey: string, data: T): Promise<void>;\n\n /**\n * Delete strategy-specific state for a course.\n *\n * @param courseId - The course this state applies to\n * @param strategyKey - Unique key identifying the strategy (typically class name)\n */\n deleteStrategyState(courseId: string, strategyKey: string): Promise<void>;\n}\n\n/**\n * Authentication and account management operations\n */\nexport interface UserDBAuthenticator {\n /**\n * Create a new user account\n */\n createAccount(\n username: string,\n password: string\n ): Promise<{\n status: Status;\n error: string;\n }>;\n\n /**\n * Log in as a user\n */\n login(\n username: string,\n password: string\n ): Promise<{\n ok: boolean;\n name?: string;\n roles?: string[];\n }>;\n\n /**\n * Log out the current user\n */\n logout(): Promise<{\n ok: boolean;\n }>;\n}\n\n/**\n * Complete user database interface - combines all user operations\n * This maintains backward compatibility with existing code\n */\nexport interface UserDBInterface extends UserDBReader, UserDBWriter, UserDBAuthenticator {}\n\nexport interface UserCourseSettings {\n [setting: string]: string | number | boolean;\n}\n\nexport interface UserCourseSetting {\n key: string;\n value: string | number | boolean;\n}\n\n// [ ] reconsider here. Should maybe be generic type based on <T extends StudyContentSource> ?\nexport interface UsrCrsDataInterface {\n getScheduledReviewCount(): Promise<number>;\n getCourseSettings(): Promise<UserCourseSettings>;\n updateCourseSettings(updates: UserCourseSetting[]): void; // [ ] return a result of some sort?\n // getRegistrationDoc(): Promise<CourseRegistration>;\n}\n\nexport type ClassroomRegistrationDesignation = 'student' | 'teacher' | 'aide' | 'admin';\n\nexport interface ClassroomRegistration {\n classID: string;\n registeredAs: ClassroomRegistrationDesignation;\n}\n\nexport interface ClassroomRegistrationDoc {\n registrations: ClassroomRegistration[];\n}\n","export * from './adminDB';\nexport * from './classroomDB';\nexport * from './contentSource';\nexport * from './courseDB';\nexport * from './dataLayerProvider';\n\nexport * from './userDB';\n","import { CourseElo } from '@vue-skuilder/common';\nimport { Moment } from 'moment';\n\nexport interface SessionTrackingData {\n peekSessionCount: number;\n studySessionCount: number;\n sessionCount: number; // total\n firstSessionDate: string;\n lastSessionDate: string;\n signupPrompted: boolean;\n promptDismissalCount: number;\n studyModeAcknowledged: boolean;\n}\n\nexport interface UserConfig {\n darkMode: boolean;\n likesConfetti: boolean;\n sessionTimeLimit: number; // Session time limit in minutes\n email?: string; // Optional email for verification flows (added for enhanced auth)\n\n // Session tracking for trial enforcement (per-course)\n // Key is courseId (e.g., 'letterspractice-basic')\n sessionTracking?: Record<string, SessionTrackingData>;\n}\n\nexport interface ActivityRecord {\n timeStamp: number | string;\n [key: string]: any;\n}\n\nexport interface CourseRegistration {\n status?: 'active' | 'dropped' | 'maintenance-mode' | 'preview';\n courseID: string;\n admin: boolean;\n moderator: boolean;\n user: boolean;\n settings?: {\n [setting: string]: string | number | boolean;\n };\n elo: number | CourseElo;\n}\n\ninterface StudyWeights {\n [courseID: string]: number;\n}\n\nexport interface CourseRegistrationDoc {\n courses: CourseRegistration[];\n studyWeight: StudyWeights;\n}\n\nexport interface ScheduledCard {\n _id: PouchDB.Core.DocumentId;\n\n /**\n * The docID of the card to be reviewed\n */\n cardId: PouchDB.Core.DocumentId;\n /**\n * The ID of the course\n */\n courseId: string;\n /**\n * The time at which the card becomes eligible for review.\n *\n * (Should probably be UTC adjusted so that performance is\n * not wonky across time zones)\n * \n * Note: Stored as ISO string for PouchDB serialization compatibility,\n * but can be consumed as Moment objects via moment.utc(reviewTime)\n */\n reviewTime: string | Moment;\n\n /**\n * The time at which this scheduled event was created.\n * \n * Note: Stored as ISO string for PouchDB serialization compatibility,\n * but can be consumed as Moment objects via moment.utc(scheduledAt)\n */\n scheduledAt: string | Moment;\n\n /**\n * Classifying whether this card is scheduled on behalf of a\n * user-registered course or by as assigned content from a\n * user-registered classroom\n */\n scheduledFor: 'course' | 'classroom';\n\n /**\n * The ID of the course or classroom that requested this card\n */\n schedulingAgentId: string;\n}\n","import { DocType, DocTypePrefixes } from './types-legacy';\n\n/**\n * Template literal type for strategy state document IDs.\n *\n * Format: `STRATEGY_STATE-{courseId}-{strategyKey}`\n */\nexport type StrategyStateId =\n `${(typeof DocTypePrefixes)[DocType.STRATEGY_STATE]}::${string}::${string}`;\n\n/**\n * Document storing strategy-specific state in the user database.\n *\n * Each strategy can persist its own state (user preferences, learned patterns,\n * temporal tracking, etc.) using this document type. The state is scoped to\n * a (user, course, strategy) tuple.\n *\n * ## Use Cases\n *\n * 1. **Explicit user preferences**: User configures tag filters, difficulty\n * preferences, or learning goals. UI writes to strategy state.\n *\n * 2. **Learned/temporal state**: Strategy tracks patterns over time, e.g.,\n * \"when did I last introduce confusable concepts together?\"\n *\n * 3. **Adaptive personalization**: Strategy infers user preferences from\n * behavior and stores them for future sessions.\n *\n * ## Storage Location\n *\n * These documents live in the **user database**, not the course database.\n * They sync with the user's data across devices.\n *\n * ## Document ID Format\n *\n * `STRATEGY_STATE::{courseId}::{strategyKey}`\n *\n * Example: `STRATEGY_STATE::piano-basics::UserTagPreferenceFilter`\n *\n * @template T - The shape of the strategy-specific data payload\n */\nexport interface StrategyStateDoc<T = unknown> {\n _id: StrategyStateId;\n _rev?: string;\n docType: DocType.STRATEGY_STATE;\n\n /**\n * The course this state applies to.\n */\n courseId: string;\n\n /**\n * Unique key identifying the strategy instance.\n * Typically the strategy class name (e.g., \"UserTagPreferenceFilter\",\n * \"InterferenceMitigatorNavigator\").\n *\n * If a course has multiple instances of the same strategy type with\n * different configurations, use a more specific key.\n */\n strategyKey: string;\n\n /**\n * Strategy-specific data payload.\n * Each strategy defines its own schema for this field.\n */\n data: T;\n\n /**\n * ISO timestamp of last update.\n * Use `moment.utc(updatedAt)` to parse into a Moment object.\n */\n updatedAt: string;\n}\n\n/**\n * Build the document ID for a strategy state document.\n *\n * @param courseId - The course ID\n * @param strategyKey - The strategy key (typically class name)\n * @returns The document ID in format `STRATEGY_STATE::{courseId}::{strategyKey}`\n */\nexport function buildStrategyStateId(courseId: string, strategyKey: string): StrategyStateId {\n return `STRATEGY_STATE::${courseId}::${strategyKey}`;\n}\n","import { DocType, DocTypePrefixes, CardHistory, CardRecord, QuestionRecord } from '../types/types-legacy';\n\nexport function areQuestionRecords(h: CardHistory<CardRecord>): h is CardHistory<QuestionRecord> {\n return isQuestionRecord(h.records[0]);\n}\n\nexport function isQuestionRecord(c: CardRecord): c is QuestionRecord {\n return (c as QuestionRecord).userAnswer !== undefined;\n}\n\nexport function getCardHistoryID(courseID: string, cardID: string): PouchDB.Core.DocumentId {\n return `${DocTypePrefixes[DocType.CARDRECORD]}-${courseID}-${cardID}`;\n}\n\nexport function parseCardHistoryID(id: string): {\n courseID: string;\n cardID: string;\n} {\n const split = id.split('-');\n let error: string = '';\n error += split.length === 3 ? '' : `\\n\\tgiven ID has incorrect number of '-' characters`;\n error +=\n split[0] === DocTypePrefixes[DocType.CARDRECORD] ? '' : `\n\tgiven ID does not start with ${DocTypePrefixes[DocType.CARDRECORD]}`;\n\n if (split.length === 3 && split[0] === DocTypePrefixes[DocType.CARDRECORD]) {\n return {\n courseID: split[1],\n cardID: split[2],\n };\n } else {\n throw new Error('parseCardHistory Error:' + error);\n }\n}\n\ninterface PouchDBError extends Error {\n error?: string;\n reason?: string;\n}\n\nexport function docIsDeleted(e: PouchDBError): boolean {\n return Boolean(e?.error === 'not_found' && e?.reason === 'deleted');\n}\n","import { CourseElo, Status, ParsedCard, BulkImportCardData } from '@vue-skuilder/common';\nimport { CourseDBInterface } from '../../core/interfaces/courseDB';\nimport { ImportResult, BulkCardProcessorConfig } from './types';\nimport { logger } from '../../util/logger';\n\n/**\n * Processes multiple cards from bulk text input\n *\n * @param parsedCards - Array of parsed cards to import\n * @param courseDB - Course database interface\n * @param config - Configuration for the card processor\n * @returns Array of import results\n */\nexport async function importParsedCards(\n parsedCards: ParsedCard[],\n courseDB: CourseDBInterface,\n config: BulkCardProcessorConfig\n): Promise<ImportResult[]> {\n const results: ImportResult[] = [];\n\n for (const parsedCard of parsedCards) {\n try {\n // processCard takes a ParsedCard and returns an ImportResult\n const result = await processCard(parsedCard, courseDB, config);\n results.push(result);\n } catch (error) {\n logger.error('Error processing card:', error);\n // Reconstruct originalText from parsedCard for this specific catch block\n // This is a fallback if processCard itself throws an unhandled error.\n // Normally, processCard should return an ImportResult with status 'error'.\n let errorOriginalText = parsedCard.markdown;\n if (parsedCard.tags && parsedCard.tags.length > 0) {\n errorOriginalText += `\\ntags: ${parsedCard.tags.join(', ')}`;\n }\n if (parsedCard.elo !== undefined) {\n errorOriginalText += `\\nelo: ${parsedCard.elo}`;\n }\n results.push({\n originalText: errorOriginalText,\n status: 'error',\n message: `Error processing card: ${\n error instanceof Error ? error.message : 'Unknown error'\n }`,\n });\n }\n }\n\n return results;\n}\n\n/**\n * Processes a single parsed card\n *\n * @param parsedCard - Parsed card data\n * @param courseDB - Course database interface\n * @param config - Configuration for the card processor\n * @returns Import result for the card\n */\nasync function processCard(\n parsedCard: ParsedCard,\n courseDB: CourseDBInterface,\n config: BulkCardProcessorConfig\n): Promise<ImportResult> {\n const { markdown, tags, elo } = parsedCard;\n\n // Build the original text representation including metadata\n let originalText = markdown;\n if (tags.length > 0) {\n originalText += `\\ntags: ${tags.join(', ')}`;\n }\n if (elo !== undefined) {\n originalText += `\\nelo: ${elo}`;\n }\n\n // Create card data object\n const cardData: BulkImportCardData = {\n Input: markdown,\n Uploads: [], // No uploads for bulk import\n };\n\n const tagsElo: CourseElo['tags'] = {};\n for (const tag of tags) {\n tagsElo[tag] = {\n score: elo || 0,\n count: 1,\n };\n }\n\n try {\n const result = await courseDB.addNote(\n config.courseCode,\n config.dataShape,\n cardData,\n config.userName,\n tags,\n undefined, // attachments\n elo\n ? {\n global: {\n score: elo,\n count: 1,\n },\n tags: tagsElo,\n misc: {},\n }\n : undefined\n );\n\n if (result.status === Status.ok) {\n return {\n originalText,\n status: 'success',\n message: 'Card added successfully.',\n cardId: result.id ? result.id : '(unknown)',\n };\n } else {\n return {\n originalText,\n status: 'error',\n message: result.message || 'Failed to add card to database. Unknown error.',\n };\n }\n } catch (error) {\n logger.error('Error adding note:', error);\n return {\n originalText,\n status: 'error',\n message: `Error adding card: ${error instanceof Error ? error.message : 'Unknown error'}`,\n };\n }\n}\n\n/**\n * Validates the configuration for bulk card processing\n *\n * @param config - Configuration to validate\n * @returns Object with validation result and error message if any\n */\nexport function validateProcessorConfig(config: Partial<BulkCardProcessorConfig>): {\n isValid: boolean;\n errorMessage?: string;\n} {\n if (!config.dataShape) {\n return {\n isValid: false,\n errorMessage: 'No data shape provided for card processing',\n };\n }\n\n if (!config.courseCode) {\n return {\n isValid: false,\n errorMessage: 'No course code provided for card processing',\n };\n }\n\n if (!config.userName) {\n return {\n isValid: false,\n errorMessage: 'No user name provided for card processing',\n };\n }\n\n return { isValid: true };\n}\n","import { DataShape } from '@vue-skuilder/common';\n\n/**\n * Interface representing the result of a bulk import operation for a single card\n */\nexport interface ImportResult {\n /** The original text input for the card */\n originalText: string;\n /** Status of the import operation */\n status: 'success' | 'error';\n /** Message describing the result or error */\n message: string;\n /** ID of the newly created card (only for success) */\n cardId?: string;\n}\n\n/**\n * Configuration for the bulk card processor\n */\nexport interface BulkCardProcessorConfig {\n /** The data shape to use for the cards */\n dataShape: DataShape;\n /** The course code used for adding notes */\n courseCode: string;\n /** The username of the current user */\n userName: string;\n}\n","export * from './cardProcessor.js';\nexport * from './types.js';","// Export all core interfaces and types\n\nexport * from './interfaces';\nexport * from './types/types-legacy';\nexport * from './types/user';\nexport * from './types/strategyState';\nexport * from '../util/Loggable';\nexport * from './util';\nexport * from './navigators';\nexport * from './bulkImport';\n","// Cross-platform data directory utilities for PouchDB\n// Provides OS-appropriate application data directories\n\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport * as os from 'os';\nimport { logger } from './logger';\nimport { ENV } from '@db/factory';\n\n/**\n * Get the application data directory for the current platform\n * Uses ~/.tuilder as requested by user for simplicity\n */\nexport function getAppDataDirectory(): string {\n if (ENV.LOCAL_STORAGE_PREFIX) {\n return path.join(os.homedir(), `.tuilder`, ENV.LOCAL_STORAGE_PREFIX);\n } else {\n return path.join(os.homedir(), '.tuilder');\n }\n}\n\n/**\n * Ensure the application data directory exists\n * Creates directory recursively if it doesn't exist\n */\nexport async function ensureAppDataDirectory(): Promise<string> {\n const appDataDir = getAppDataDirectory();\n \n try {\n await fs.promises.mkdir(appDataDir, { recursive: true });\n } catch (err: any) {\n if (err.code !== 'EEXIST') {\n throw new Error(`Failed to create app data directory ${appDataDir}: ${err.message}`);\n }\n }\n \n return appDataDir;\n}\n\n/**\n * Get the full path for a PouchDB database file\n * @param dbName - The database name (e.g., 'userdb-Colin')\n */\nexport function getDbPath(dbName: string): string {\n return path.join(getAppDataDirectory(), dbName);\n}\n\n/**\n * Initialize data directory for PouchDB usage\n * Should be called once at application startup\n */\nexport async function initializeDataDirectory(): Promise<void> {\n await ensureAppDataDirectory();\n \n // Log initialization\n logger.info(`PouchDB data directory initialized: ${getAppDataDirectory()}`);\n}","// packages/db/src/impl/common/userDBHelpers.ts\n\nimport moment from 'moment';\nimport { DocType, DocTypePrefixes } from '@db/core';\nimport { logger } from '../../util/logger';\nimport { ScheduledCard } from '@db/core/types/user';\n\nexport const REVIEW_TIME_FORMAT: string = 'YYYY-MM-DD--kk:mm:ss-SSS';\n\nimport pouch from '../couch/pouchdb-setup';\nimport { getDbPath } from '../../util/dataDirectory';\n\nconst log = (s: any) => {\n logger.info(s);\n};\n\nexport function hexEncode(str: string): string {\n let hex: string;\n let returnStr: string = '';\n\n for (let i = 0; i < str.length; i++) {\n hex = str.charCodeAt(i).toString(16);\n returnStr += ('000' + hex).slice(3);\n }\n\n return returnStr;\n}\n\nexport function filterAllDocsByPrefix<T>(\n db: PouchDB.Database,\n prefix: string,\n opts?: PouchDB.Core.AllDocsOptions\n) {\n // see couchdb docs 6.2.2:\n // Guide to Views -> Views Collation -> String Ranges\n const options: PouchDB.Core.AllDocsWithinRangeOptions = {\n startkey: prefix,\n endkey: prefix + '\\ufff0',\n include_docs: true,\n };\n\n if (opts) {\n Object.assign(options, opts);\n }\n return db.allDocs<T>(options);\n}\n\nexport function getStartAndEndKeys(key: string): {\n startkey: string;\n endkey: string;\n} {\n return {\n startkey: key,\n endkey: key + '\\ufff0',\n };\n}\n\nexport function updateGuestAccountExpirationDate(guestDB: PouchDB.Database<object>) {\n const currentTime = moment.utc();\n const expirationDate: string = currentTime.add(2, 'months').toISOString();\n const expiryDocID: string = 'GuestAccountExpirationDate';\n\n void guestDB\n .get(expiryDocID)\n .then((doc) => {\n return guestDB.put({\n _id: expiryDocID,\n _rev: doc._rev,\n date: expirationDate,\n });\n })\n .catch(() => {\n return guestDB.put({\n _id: expiryDocID,\n date: expirationDate,\n });\n });\n}\n\n/**\n * Get local user database with appropriate adapter for environment\n */\nexport function getLocalUserDB(username: string): PouchDB.Database {\n // // Choose adapter based on environment\n //\n // Not certain of this is required. Let's let pouch's auto detection\n // handle it until we specifically know we need to intervene.\n //\n // let adapter: string;\n // if (typeof window !== 'undefined') {\n // // Browser environment - use IndexedDB\n // adapter = 'idb';\n // } else {\n // // Node.js environment (tests) - use memory adapter\n // adapter = 'memory';\n // }\n\n const dbName = `userdb-${username}`;\n \n // Use proper data directory in Node.js, browser will use IndexedDB\n if (typeof window === 'undefined') {\n // Node.js environment - use filesystem with proper app data directory\n return new pouch(getDbPath(dbName), {});\n } else {\n // Browser environment - use default (IndexedDB)\n return new pouch(dbName, {});\n }\n}\n\n/**\n * Schedule a card review (strategy-agnostic version)\n */\nexport function scheduleCardReviewLocal(\n userDB: PouchDB.Database,\n review: {\n card_id: PouchDB.Core.DocumentId;\n time: moment.Moment;\n course_id: string;\n scheduledFor: ScheduledCard['scheduledFor'];\n schedulingAgentId: ScheduledCard['schedulingAgentId'];\n }\n) {\n const now = moment.utc();\n logger.info(`Scheduling for review in: ${review.time.diff(now, 'h') / 24} days`);\n void userDB.put<ScheduledCard>({\n _id: DocTypePrefixes[DocType.SCHEDULED_CARD] + review.time.format(REVIEW_TIME_FORMAT),\n cardId: review.card_id,\n reviewTime: review.time.toISOString(),\n courseId: review.course_id,\n scheduledAt: now.toISOString(),\n scheduledFor: review.scheduledFor,\n schedulingAgentId: review.schedulingAgentId,\n });\n}\n\n/**\n * Remove a scheduled card review (strategy-agnostic version)\n */\nexport async function removeScheduledCardReviewLocal(\n userDB: PouchDB.Database,\n reviewDocID: string\n) {\n const reviewDoc = await userDB.get(reviewDocID);\n userDB\n .remove(reviewDoc)\n .then((res) => {\n if (res.ok) {\n log(`Removed Review Doc: ${reviewDocID}`);\n }\n })\n .catch((err) => {\n log(`Failed to remove Review Doc: ${reviewDocID},\\n${JSON.stringify(err)}`);\n });\n}\n","import {\n ScheduledCard,\n UserCourseSetting,\n UserCourseSettings,\n UsrCrsDataInterface,\n} from '@db/core';\n\nimport moment, { Moment } from 'moment';\n\nimport { UserDBInterface } from '@db/core';\nimport { logger } from '../../util/logger';\n\nexport class UsrCrsData implements UsrCrsDataInterface {\n private user: UserDBInterface;\n private _courseId: string;\n\n constructor(user: UserDBInterface, courseId: string) {\n this.user = user;\n this._courseId = courseId;\n }\n\n public async getReviewsForcast(daysCount: number) {\n const time = moment.utc().add(daysCount, 'days');\n return this.getReviewstoDate(time);\n }\n\n public async getPendingReviews() {\n const now = moment.utc();\n return this.getReviewstoDate(now);\n }\n\n public async getScheduledReviewCount(): Promise<number> {\n return (await this.getPendingReviews()).length;\n }\n\n public async getCourseSettings(): Promise<UserCourseSettings> {\n const regDoc = await this.user.getCourseRegistrationsDoc();\n const crsDoc = regDoc.courses.find((c) => c.courseID === this._courseId);\n\n if (crsDoc && crsDoc.settings) {\n return crsDoc.settings;\n } else {\n logger.warn(`no settings found during lookup on course ${this._courseId}`);\n return {};\n }\n }\n public updateCourseSettings(updates: UserCourseSetting[]): void {\n // TODO: Add updateCourseSettings method to UserDBInterface\n // For now, we'll need to cast to access the concrete implementation\n if ('updateCourseSettings' in this.user) {\n void (this.user as any).updateCourseSettings(this._courseId, updates);\n }\n }\n\n private async getReviewstoDate(targetDate: Moment) {\n // Use the interface method instead of direct database access\n const allReviews = await this.user.getPendingReviews(this._courseId);\n\n logger.debug(\n `Fetching ${this.user.getUsername()}'s scheduled reviews for course ${this._courseId}.`\n );\n\n return allReviews.filter((review: ScheduledCard) => {\n const reviewTime = moment.utc(review.reviewTime);\n return targetDate.isAfter(reviewTime);\n });\n }\n}\n","import { DocType, DocTypePrefixes, StrategyStateDoc, buildStrategyStateId } from '@db/core';\nimport { getCardHistoryID } from '@db/core/util';\nimport { CourseElo, Status } from '@vue-skuilder/common';\nimport moment, { Moment } from 'moment';\nimport { GuestUsername } from '../../core/types/types-legacy';\nimport { logger } from '../../util/logger';\n\nimport {\n ClassroomRegistrationDoc,\n UserCourseSetting,\n UserDBInterface,\n UsrCrsDataInterface,\n} from '@db/core';\nimport {\n ActivityRecord,\n CourseRegistration,\n CourseRegistrationDoc,\n ScheduledCard,\n UserConfig,\n} from '@db/core/types/user';\nimport { DocumentUpdater } from '@db/study';\nimport { CardHistory, CardRecord } from '../../core/types/types-legacy';\nimport type { SyncStrategy } from './SyncStrategy';\nimport {\n filterAllDocsByPrefix,\n getStartAndEndKeys,\n REVIEW_TIME_FORMAT,\n getLocalUserDB,\n scheduleCardReviewLocal,\n removeScheduledCardReviewLocal,\n} from './userDBHelpers';\nimport { PouchError } from '../couch/types';\nimport UpdateQueue, { Update } from '../couch/updateQueue';\nimport { UsrCrsData } from '../couch/user-course-relDB';\nimport { getCredentialledCourseConfig } from '../couch/index';\n\nconst log = (s: any) => {\n logger.info(s);\n};\n\n// logger.log(`Connecting to remote: ${remoteStr}`);\n\ninterface DesignDoc {\n _id: string;\n views: {\n [viewName: string]: {\n map: string; // String representation of the map function\n };\n };\n}\n\n/**\n * Base user database implementation that uses a pluggable sync strategy.\n * Handles local storage operations and delegates sync/remote operations to the strategy.\n */\nexport class BaseUser implements UserDBInterface, DocumentUpdater {\n private static _instance: BaseUser;\n private static _initialized: boolean = false;\n\n public static Dummy(syncStrategy: SyncStrategy): BaseUser {\n return new BaseUser('Me', syncStrategy);\n }\n\n static readonly DOC_IDS = {\n CONFIG: 'CONFIG',\n COURSE_REGISTRATIONS: 'CourseRegistrations',\n CLASSROOM_REGISTRATIONS: 'ClassroomRegistrations',\n };\n\n // private email: string;\n private _username: string;\n private syncStrategy: SyncStrategy;\n\n public getUsername(): string {\n return this._username;\n }\n\n public isLoggedIn(): boolean {\n return !this._username.startsWith(GuestUsername);\n }\n\n public remote(): PouchDB.Database {\n return this.remoteDB;\n }\n\n private localDB!: PouchDB.Database;\n private remoteDB!: PouchDB.Database;\n private writeDB!: PouchDB.Database; // Database to use for write operations (local-first approach)\n\n private updateQueue!: UpdateQueue;\n\n public async createAccount(\n username: string,\n password: string\n ): Promise<{\n status: Status;\n error: string;\n }> {\n if (!this.syncStrategy.canCreateAccount()) {\n throw new Error('Account creation not supported by current sync strategy');\n }\n\n if (!this._username.startsWith(GuestUsername)) {\n throw new Error(\n `Cannot create a new account while logged in:\nCurrently logged-in as ${this._username}.`\n );\n }\n\n const result = await this.syncStrategy.createAccount!(username, password);\n\n // If account creation was successful, update the username and reinitialize\n if (result.status === Status.ok) {\n log(`Account created successfully, updating username to ${username}`);\n this._username = username;\n try {\n localStorage.removeItem('sk-guest-uuid');\n } catch (e) {\n logger.warn('localStorage not available (Node.js environment):', e);\n }\n await this.init();\n }\n\n return {\n status: result.status,\n error: result.error || '',\n };\n }\n public async login(username: string, password: string) {\n if (!this.syncStrategy.canAuthenticate()) {\n throw new Error('Authentication not supported by current sync strategy');\n }\n\n if (!this._username.startsWith(GuestUsername) && this._username != username) {\n if (this._username != username) {\n throw new Error(`Cannot change accounts while logged in.\n Log out of account ${this.getUsername()} before logging in as ${username}.`);\n }\n logger.warn(`User ${this._username} is already logged in, but executing login again.`);\n }\n\n const loginResult = await this.syncStrategy.authenticate!(username, password);\n if (loginResult.ok) {\n log(`Logged in as ${username}`);\n this._username = username;\n try {\n localStorage.removeItem('sk-guest-uuid');\n } catch (e) {\n logger.warn('localStorage not available (Node.js environment):', e);\n }\n await this.init();\n }\n return loginResult;\n }\n\n public async resetUserData(): Promise<{ status: Status; error?: string }> {\n // Only allow reset for local-only sync strategies\n if (this.syncStrategy.canAuthenticate()) {\n return {\n status: Status.error,\n error:\n 'Reset user data is only available for local-only mode. Use logout instead for remote sync.',\n };\n }\n\n try {\n const localDB = getLocalUserDB(this._username);\n\n // Get all documents to identify user data to clear\n const allDocs = await localDB.allDocs({ include_docs: false });\n\n // Identify documents to delete (preserve authentication and user identity)\n const docsToDelete = allDocs.rows\n .filter((row) => {\n const id = row.id;\n // Delete user progress data but preserve core user documents\n return (\n id.startsWith(DocTypePrefixes[DocType.CARDRECORD]) || // Card interaction history\n id.startsWith(DocTypePrefixes[DocType.SCHEDULED_CARD]) || // Scheduled reviews\n id === BaseUser.DOC_IDS.COURSE_REGISTRATIONS || // Course registrations\n id === BaseUser.DOC_IDS.CLASSROOM_REGISTRATIONS || // Classroom registrations\n id === BaseUser.DOC_IDS.CONFIG // User config\n );\n })\n .map((row) => ({ _id: row.id, _rev: row.value.rev, _deleted: true }));\n\n if (docsToDelete.length > 0) {\n await localDB.bulkDocs(docsToDelete);\n }\n\n // Reinitialize to create fresh default documents\n await this.init();\n\n return { status: Status.ok };\n } catch (error) {\n logger.error('Failed to reset user data:', error);\n return {\n status: Status.error,\n error: error instanceof Error ? error.message : 'Unknown error during reset',\n };\n }\n }\n\n public async logout() {\n if (!this.syncStrategy.canAuthenticate()) {\n // For strategies that don't support authentication, just switch to guest\n this._username = await this.syncStrategy.getCurrentUsername();\n await this.init();\n return { ok: true };\n }\n\n const ret = await this.syncStrategy.logout!();\n // return to 'guest' mode\n this._username = await this.syncStrategy.getCurrentUsername();\n await this.init();\n\n return ret;\n }\n\n public async get<T>(id: string): Promise<T & PouchDB.Core.RevisionIdMeta> {\n return this.localDB.get<T>(id);\n }\n\n public update<T extends PouchDB.Core.Document<object>>(id: string, update: Update<T>) {\n return this.updateQueue.update(id, update);\n }\n\n public async getCourseRegistrationsDoc(): Promise<\n CourseRegistrationDoc & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta\n > {\n logger.debug(`Fetching courseRegistrations for ${this.getUsername()}`);\n\n let ret;\n\n try {\n const regDoc = await this.localDB.get<CourseRegistrationDoc>(\n BaseUser.DOC_IDS.COURSE_REGISTRATIONS\n );\n return regDoc;\n } catch (e) {\n const err = e as PouchError;\n if (err.status === 404) {\n await this.localDB.put<CourseRegistrationDoc>({\n _id: BaseUser.DOC_IDS.COURSE_REGISTRATIONS,\n courses: [],\n studyWeight: {},\n });\n ret = await this.getCourseRegistrationsDoc();\n } else {\n throw new Error(\n `Unexpected error ${JSON.stringify(e)} in getOrCreateCourseRegistrationDoc...`\n );\n }\n }\n\n return ret;\n }\n\n public async getActiveCourses() {\n const reg = await this.getCourseRegistrationsDoc();\n return reg.courses.filter((c) => {\n return c.status === undefined || c.status === 'active';\n });\n }\n\n /**\n * Returns a promise of the card IDs that the user has\n * a scheduled review for.\n *\n */\n public async getActiveCards() {\n const keys = getStartAndEndKeys(DocTypePrefixes[DocType.SCHEDULED_CARD]);\n\n const reviews = await this.remoteDB.allDocs<ScheduledCard>({\n startkey: keys.startkey,\n endkey: keys.endkey,\n include_docs: true,\n });\n\n return reviews.rows.map((r) => {\n return {\n courseID: r.doc!.courseId,\n cardID: r.doc!.cardId,\n };\n });\n }\n\n public async getActivityRecords(): Promise<ActivityRecord[]> {\n try {\n const hist = await this.getHistory();\n\n const allRecords: ActivityRecord[] = [];\n if (!Array.isArray(hist)) {\n logger.error('getHistory did not return an array:', hist);\n return allRecords;\n }\n\n // Sample the first few records to understand structure\n let sampleCount = 0;\n\n for (let i = 0; i < hist.length; i++) {\n try {\n if (hist[i] && Array.isArray(hist[i]!.records)) {\n hist[i]!.records.forEach((record: CardRecord) => {\n try {\n // Skip this record if timeStamp is missing\n if (!record.timeStamp) {\n return;\n }\n\n let timeStamp;\n\n // Handle different timestamp formats\n if (typeof record.timeStamp === 'object') {\n // It's likely a Moment object\n if (typeof record.timeStamp.toDate === 'function') {\n // It's definitely a Moment object\n timeStamp = record.timeStamp.toISOString();\n } else if (record.timeStamp instanceof Date) {\n // It's a Date object\n timeStamp = record.timeStamp.toISOString();\n } else {\n // Log a sample of unknown object types, but don't flood logger\n if (sampleCount < 3) {\n logger.warn('Unknown timestamp object type:', record.timeStamp);\n sampleCount++;\n }\n return;\n }\n } else if (typeof record.timeStamp === 'string') {\n // It's already a string, but make sure it's a valid date\n const date = new Date(record.timeStamp);\n if (isNaN(date.getTime())) {\n return; // Invalid date string\n }\n timeStamp = record.timeStamp;\n } else if (typeof record.timeStamp === 'number') {\n // Assume it's a Unix timestamp (milliseconds since epoch)\n timeStamp = new Date(record.timeStamp).toISOString();\n } else {\n // Unknown type, skip\n return;\n }\n\n allRecords.push({\n timeStamp,\n courseID: record.courseID || 'unknown',\n cardID: record.cardID || 'unknown',\n timeSpent: record.timeSpent || 0,\n type: 'card_view',\n });\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (err) {\n // Silently skip problematic records to avoid flooding logs\n }\n });\n }\n } catch (err) {\n logger.error('Error processing history item:', err);\n }\n }\n\n logger.debug(`Found ${allRecords.length} activity records`);\n return allRecords;\n } catch (err) {\n logger.error('Error in getActivityRecords:', err);\n return [];\n }\n }\n\n private async getReviewstoDate(targetDate: Moment, course_id?: string) {\n const keys = getStartAndEndKeys(DocTypePrefixes[DocType.SCHEDULED_CARD]);\n\n const reviews = await this.remoteDB.allDocs<ScheduledCard>({\n startkey: keys.startkey,\n endkey: keys.endkey,\n include_docs: true,\n });\n\n log(\n `Fetching ${this._username}'s scheduled reviews${\n course_id ? ` for course ${course_id}` : ''\n }.`\n );\n return reviews.rows\n .filter((r) => {\n if (r.id.startsWith(DocTypePrefixes[DocType.SCHEDULED_CARD])) {\n const date = moment.utc(\n r.id.substr(DocTypePrefixes[DocType.SCHEDULED_CARD].length),\n REVIEW_TIME_FORMAT\n );\n if (targetDate.isAfter(date)) {\n if (course_id === undefined || r.doc!.courseId === course_id) {\n return true;\n }\n }\n }\n })\n .map((r) => r.doc!);\n }\n\n public async getReviewsForcast(daysCount: number) {\n const time = moment.utc().add(daysCount, 'days');\n return this.getReviewstoDate(time);\n }\n\n public async getPendingReviews(course_id?: string) {\n const now = moment.utc();\n return this.getReviewstoDate(now, course_id);\n }\n\n public async getScheduledReviewCount(course_id: string): Promise<number> {\n return (await this.getPendingReviews(course_id)).length;\n }\n\n public async getRegisteredCourses() {\n const regDoc = await this.getCourseRegistrationsDoc();\n return regDoc.courses.filter((c) => {\n return !c.status || c.status === 'active' || c.status === 'maintenance-mode';\n });\n }\n\n public async getCourseRegDoc(courseID: string) {\n const regDocs = await this.getCourseRegistrationsDoc();\n const ret = regDocs.courses.find((c) => c.courseID === courseID);\n if (ret) {\n return ret;\n } else {\n throw new Error(`Course registration not found for course ID: ${courseID}`);\n }\n }\n\n public async registerForCourse(course_id: string, previewMode: boolean = false) {\n return this.getCourseRegistrationsDoc()\n .then((doc: CourseRegistrationDoc) => {\n const status = previewMode ? 'preview' : 'active';\n logger.debug(`Registering for ${course_id} with status: ${status}`);\n\n const regItem: CourseRegistration = {\n status: status,\n courseID: course_id,\n user: true,\n admin: false,\n moderator: false,\n elo: {\n global: {\n score: 1000,\n count: 0,\n },\n tags: {},\n misc: {},\n },\n };\n\n if (\n doc.courses.filter((course) => {\n return course.courseID === regItem.courseID;\n }).length === 0\n ) {\n log(`It's a new course registration!`);\n doc.courses.push(regItem);\n doc.studyWeight[course_id] = 1;\n } else {\n doc.courses.forEach((c) => {\n log(`Found the previously registered course!`);\n if (c.courseID === course_id) {\n c.status = status;\n }\n });\n }\n\n return this.localDB.put<CourseRegistrationDoc>(doc);\n })\n .catch((e) => {\n log(`Registration failed because of: ${JSON.stringify(e)}`);\n throw e;\n });\n }\n public async dropCourse(course_id: string, dropStatus: CourseRegistration['status'] = 'dropped') {\n return this.getCourseRegistrationsDoc().then((doc) => {\n let index: number = -1;\n for (let i = 0; i < doc.courses.length; i++) {\n if (doc.courses[i].courseID === course_id) {\n index = i;\n }\n }\n\n if (index !== -1) {\n // remove from the relative-weighting of course study\n delete doc.studyWeight[course_id];\n // set drop status\n doc.courses[index].status = dropStatus;\n } else {\n throw new Error(\n `User ${this.getUsername()} is not currently registered for course ${course_id}`\n );\n }\n\n return this.localDB.put<CourseRegistrationDoc>(doc);\n });\n }\n\n public async getCourseInterface(courseId: string): Promise<UsrCrsDataInterface> {\n return new UsrCrsData(this, courseId);\n }\n\n public async getUserEditableCourses() {\n let courseIDs: string[] = [];\n\n const registeredCourses = await this.getCourseRegistrationsDoc();\n\n courseIDs = courseIDs.concat(\n registeredCourses.courses.map((course) => {\n return course.courseID;\n })\n );\n\n const cfgs = await Promise.all(\n courseIDs.map(async (id) => {\n return await getCredentialledCourseConfig(id);\n })\n );\n return cfgs;\n }\n\n public async getConfig(): Promise<UserConfig & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta> {\n const defaultConfig: PouchDB.Core.Document<UserConfig> = {\n _id: BaseUser.DOC_IDS.CONFIG,\n darkMode: false,\n likesConfetti: false,\n sessionTimeLimit: 5,\n };\n\n try {\n const cfg = await this.localDB.get<UserConfig>(BaseUser.DOC_IDS.CONFIG);\n logger.debug('Raw config from DB:', cfg);\n\n return cfg;\n } catch (e) {\n const err = e as PouchError;\n if (err.name && err.name === 'not_found') {\n await this.localDB.put<UserConfig>(defaultConfig);\n return this.getConfig();\n } else {\n logger.error(`Error setting user default config:`, e);\n throw new Error(`Error returning the user's configuration: ${JSON.stringify(e)}`);\n }\n }\n }\n\n public async setConfig(items: Partial<UserConfig>) {\n logger.debug(`Setting Config items ${JSON.stringify(items)}`);\n\n const c = await this.getConfig();\n const put = await this.localDB.put<UserConfig>({\n ...c,\n ...items,\n });\n\n if (put.ok) {\n logger.debug(`Config items set: ${JSON.stringify(items)}`);\n } else {\n logger.error(`Error setting config items: ${JSON.stringify(put)}`);\n }\n }\n\n /**\n *\n * This function should be called *only* by the pouchdb datalayer provider\n * auth store.\n *\n *\n * Anyone else seeking the current user should use the auth store's\n * exported `getCurrentUser` method.\n *\n */\n public static async instance(syncStrategy: SyncStrategy, username?: string): Promise<BaseUser> {\n if (username) {\n BaseUser._instance = new BaseUser(username, syncStrategy);\n await BaseUser._instance.init();\n return BaseUser._instance;\n } else if (BaseUser._instance && BaseUser._initialized) {\n // log(`USER.instance() returning user ${BaseUser._instance._username}`);\n return BaseUser._instance;\n } else if (BaseUser._instance) {\n return new Promise((resolve) => {\n (function waitForUser() {\n if (BaseUser._initialized) {\n return resolve(BaseUser._instance);\n } else {\n setTimeout(waitForUser, 50);\n }\n })();\n });\n } else {\n const guestUsername = await syncStrategy.getCurrentUsername();\n BaseUser._instance = new BaseUser(guestUsername, syncStrategy);\n await BaseUser._instance.init();\n return BaseUser._instance;\n }\n }\n\n private constructor(username: string, syncStrategy: SyncStrategy) {\n BaseUser._initialized = false;\n this._username = username;\n this.syncStrategy = syncStrategy;\n this.setDBandQ();\n }\n\n private setDBandQ() {\n this.localDB = getLocalUserDB(this._username);\n this.remoteDB = this.syncStrategy.setupRemoteDB(this._username);\n // writeDB follows local-first pattern: static mode writes to local, CouchDB writes to remote/local as appropriate\n this.writeDB = this.syncStrategy.getWriteDB\n ? this.syncStrategy.getWriteDB(this._username)\n : this.localDB;\n this.updateQueue = new UpdateQueue(this.localDB, this.writeDB);\n }\n\n private async init() {\n BaseUser._initialized = false;\n\n // Skip admin user\n if (this._username === 'admin') {\n BaseUser._initialized = true;\n return;\n }\n\n this.setDBandQ();\n\n this.syncStrategy.startSync(this.localDB, this.remoteDB);\n this.applyDesignDocs().catch((error) => {\n log(`Error in applyDesignDocs background task: ${error}`);\n if (error && typeof error === 'object') {\n log(`Full error details in applyDesignDocs: ${JSON.stringify(error)}`);\n }\n });\n this.deduplicateReviews().catch((error) => {\n log(`Error in deduplicateReviews background task: ${error}`);\n if (error && typeof error === 'object') {\n log(`Full error details in background task: ${JSON.stringify(error)}`);\n }\n });\n BaseUser._initialized = true;\n }\n\n private static designDocs: DesignDoc[] = [\n {\n _id: '_design/reviewCards',\n views: {\n reviewCards: {\n map: `function (doc) {\n if (doc._id && doc._id.indexOf('card_review') === 0 && doc.courseId && doc.cardId) {\n emit(doc._id, doc.courseId + '-' + doc.cardId);\n }\n }`,\n },\n },\n },\n ];\n\n private async applyDesignDocs() {\n log(`Starting applyDesignDocs for user: ${this._username}`);\n log(`Remote DB name: ${this.remoteDB.name || 'unknown'}`);\n\n if (this._username === 'admin') {\n // Skip admin user\n log('Skipping design docs for admin user');\n return;\n }\n\n log(`Applying ${BaseUser.designDocs.length} design docs`);\n for (const doc of BaseUser.designDocs) {\n log(`Applying design doc: ${doc._id}`);\n try {\n // Try to get existing doc\n try {\n const existingDoc = await this.remoteDB.get(doc._id);\n // Update existing doc\n await this.remoteDB.put({\n ...doc,\n _rev: existingDoc._rev,\n });\n } catch (e: unknown) {\n if (e instanceof Error && e.name === 'not_found') {\n // Create new doc\n await this.remoteDB.put(doc);\n } else {\n throw e; // Re-throw unexpected errors\n }\n }\n } catch (error: unknown) {\n if ((error as any).name && (error as any).name === 'conflict') {\n logger.warn(`Design doc ${doc._id} update conflict - will retry`);\n // Wait a bit and try again\n await new Promise((resolve) => setTimeout(resolve, 1000));\n await this.applyDesignDoc(doc); // Recursive retry\n } else {\n logger.error(`Failed to apply design doc ${doc._id}:`, error);\n throw error;\n }\n }\n }\n }\n\n // Helper method for single doc update with retry\n private async applyDesignDoc(doc: DesignDoc, retries = 3): Promise<void> {\n try {\n const existingDoc = await this.remoteDB.get(doc._id);\n await this.remoteDB.put({\n ...doc,\n _rev: existingDoc._rev,\n });\n } catch (e: unknown) {\n if (e instanceof Error && e.name === 'conflict' && retries > 0) {\n await new Promise((resolve) => setTimeout(resolve, 1000));\n return this.applyDesignDoc(doc, retries - 1);\n }\n throw e;\n }\n }\n\n /**\n * Logs a record of the user's interaction with the card and returns the card's\n * up-to-date history.\n *\n * **Automatic Initialization:**\n * If this is the user's first interaction with the card (CardHistory doesn't exist),\n * this method automatically creates the CardHistory document with initial values\n * (lapses: 0, streak: 0, bestInterval: 0).\n *\n * **Error Handling:**\n * - Handles 404 errors by creating initial CardHistory document\n * - Re-throws all other errors from UpdateQueue\n *\n * // [ ] #db-refactor extract to a smaller scope - eg, UserStudySession\n *\n * @param record - The recent recorded interaction between user and card\n * @returns The updated state of the card's CardHistory data\n * @throws Error if document creation fails or non-404 database error occurs\n */\n\n public async putCardRecord<T extends CardRecord>(\n record: T\n ): Promise<CardHistory<CardRecord> & PouchDB.Core.RevisionIdMeta> {\n const cardHistoryID = getCardHistoryID(record.courseID, record.cardID);\n // stringify the current record to make it writable to couchdb\n record.timeStamp = moment.utc(record.timeStamp).toString() as unknown as Moment;\n\n try {\n const cardHistory = await this.update<CardHistory<T>>(\n cardHistoryID,\n function (h: CardHistory<T>) {\n h.records.push(record);\n h.bestInterval = h.bestInterval || 0;\n h.lapses = h.lapses || 0;\n h.streak = h.streak || 0;\n return h;\n }\n );\n\n // Convert timestamps to moment objects\n cardHistory.records = cardHistory.records.map<T>((record) => {\n const ret: T = {\n ...(record as object),\n } as T;\n ret.timeStamp = moment.utc(record.timeStamp);\n return ret;\n });\n return cardHistory;\n } catch (e) {\n const reason = e as PouchError;\n if (reason.status === 404) {\n try {\n const initCardHistory: CardHistory<T> = {\n _id: cardHistoryID,\n cardID: record.cardID,\n courseID: record.courseID,\n records: [record],\n lapses: 0,\n streak: 0,\n bestInterval: 0,\n };\n const putResult = await this.writeDB.put<CardHistory<T>>(initCardHistory);\n return { ...initCardHistory, _rev: putResult.rev };\n } catch (creationError) {\n throw new Error(\n `Failed to create CardHistory for ${cardHistoryID}. Reason: ${creationError}`\n );\n }\n } else {\n throw new Error(`putCardRecord failed because of:\n name:${reason.name}\n error: ${reason.error}\n message: ${reason.message}`);\n }\n }\n }\n\n private async deduplicateReviews() {\n try {\n log('Starting deduplication of scheduled reviews...');\n log(`Remote DB name: ${this.remoteDB.name || 'unknown'}`);\n log(`Write DB name: ${this.writeDB.name || 'unknown'}`);\n /**\n * Maps the qualified-id of a scheduled review card to\n * the docId of the same scheduled review.\n *\n * EG: {\n * courseId-cardId: 'card_review_2021-06--17:12:165'\n * }\n */\n const reviewsMap: { [index: string]: string } = {};\n const duplicateDocIds: string[] = [];\n\n log(\n `Attempting to query remoteDB for reviewCards/reviewCards. Database: ${this.remoteDB.name || 'unknown'}`\n );\n const scheduledReviews = await this.remoteDB.query<{\n id: string;\n value: string;\n }>('reviewCards/reviewCards');\n\n log(`Found ${scheduledReviews.rows.length} scheduled reviews to process`);\n\n // First pass: identify duplicates\n scheduledReviews.rows.forEach((r) => {\n const qualifiedCardId = r.value; // courseId-cardId\n const docId = r.key; // card_review_2021-06--17:12:165\n\n if (reviewsMap[qualifiedCardId]) {\n // this card is scheduled more than once! mark the earlier one for deletion\n log(`Found duplicate scheduled review for card: ${qualifiedCardId}`);\n log(\n `Marking earlier review ${reviewsMap[qualifiedCardId]} for deletion, keeping ${docId}`\n );\n duplicateDocIds.push(reviewsMap[qualifiedCardId]);\n // replace with the later-dated scheduled review\n reviewsMap[qualifiedCardId] = docId;\n } else {\n // note that this card is scheduled for review\n reviewsMap[qualifiedCardId] = docId;\n }\n });\n\n // Second pass: remove duplicates\n if (duplicateDocIds.length > 0) {\n log(`Removing ${duplicateDocIds.length} duplicate reviews...`);\n const deletePromises = duplicateDocIds.map(async (docId) => {\n try {\n const doc = await this.remoteDB.get(docId);\n await this.writeDB.remove(doc);\n log(`Successfully removed duplicate review: ${docId}`);\n } catch (error) {\n log(`Failed to remove duplicate review ${docId}: ${error}`);\n }\n });\n\n await Promise.all(deletePromises);\n log(`Deduplication complete. Processed ${duplicateDocIds.length} duplicates`);\n } else {\n log('No duplicate reviews found');\n }\n } catch (error) {\n log(`Error during review deduplication: ${error}`);\n if (error && typeof error === 'object' && 'status' in error && error.status === 404) {\n log(\n `Database not found (404) during review deduplication. Database: ${this.remoteDB.name || 'unknown'}`\n );\n log(\n `This might indicate the user database doesn't exist or the reviewCards view isn't available`\n );\n }\n // Log full error details for debugging\n if (error && typeof error === 'object') {\n log(`Full error details: ${JSON.stringify(error)}`);\n }\n }\n }\n\n /**\n * Returns a promise of the card IDs that the user has\n * encountered in the past.\n *\n * @param course_id optional specification of individual course\n */\n async getSeenCards(course_id?: string) {\n let prefix = DocTypePrefixes[DocType.CARDRECORD];\n if (course_id) {\n prefix += course_id;\n }\n const docs = await filterAllDocsByPrefix(this.localDB, prefix, {\n include_docs: false,\n });\n // const docs = await this.localDB.allDocs({});\n const ret: PouchDB.Core.DocumentId[] = [];\n docs.rows.forEach((row) => {\n if (row.id.startsWith(DocTypePrefixes[DocType.CARDRECORD])) {\n ret.push(row.id.substr(DocTypePrefixes[DocType.CARDRECORD].length));\n }\n });\n return ret;\n }\n\n /**\n *\n * @returns A promise of the cards that the user has seen in the past.\n */\n async getHistory() {\n const cards = await filterAllDocsByPrefix<CardHistory<CardRecord>>(\n this.remoteDB,\n DocTypePrefixes[DocType.CARDRECORD],\n {\n include_docs: true,\n attachments: false,\n }\n );\n return cards.rows.map((r) => r.doc);\n }\n\n async updateCourseSettings(course_id: string, settings: UserCourseSetting[]) {\n void this.getCourseRegistrationsDoc().then((doc) => {\n const crs = doc.courses.find((c) => c.courseID === course_id);\n if (crs) {\n if (crs.settings === null || crs.settings === undefined) {\n crs.settings = {};\n }\n settings.forEach((setting) => {\n crs!.settings![setting.key] = setting.value;\n });\n }\n\n return this.localDB.put(doc);\n });\n }\n async getCourseSettings(course_id: string) {\n const regDoc = await this.getCourseRegistrationsDoc();\n const crsDoc = regDoc.courses.find((c) => c.courseID === course_id);\n\n if (crsDoc) {\n return crsDoc.settings;\n } else {\n throw new Error(`getCourseSettings Failed:\n User is not registered for course ${course_id}`);\n }\n }\n\n private async getOrCreateClassroomRegistrationsDoc(): Promise<\n ClassroomRegistrationDoc & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta\n > {\n let ret;\n\n try {\n ret = await this.remoteDB.get<ClassroomRegistrationDoc>(\n BaseUser.DOC_IDS.CLASSROOM_REGISTRATIONS\n );\n } catch (e) {\n const err = e as PouchError;\n\n if (err.status === 404) {\n // doc does not exist. Create it and then run this fcn again.\n await this.writeDB.put<ClassroomRegistrationDoc>({\n _id: BaseUser.DOC_IDS.CLASSROOM_REGISTRATIONS,\n registrations: [],\n });\n ret = await this.getOrCreateClassroomRegistrationsDoc();\n } else {\n // Properly serialize error information\n const errorDetails = {\n name: err.name,\n status: err.status,\n message: err.message,\n reason: err.reason,\n error: err.error,\n };\n\n logger.error(\n 'Database error in getOrCreateClassroomRegistrationsDoc (private method):',\n errorDetails\n );\n\n throw new Error(\n `Database error accessing classroom registrations: ${err.message || err.name || 'Unknown error'} (status: ${err.status})`\n );\n }\n }\n\n logger.debug(`Returning classroom registrations doc: ${JSON.stringify(ret)}`);\n return ret;\n }\n\n /**\n * Retrieves the list of active classroom IDs where the user is registered as a student.\n *\n * @returns Promise<string[]> - Array of classroom IDs, or empty array if classroom\n * registration document is unavailable due to database errors\n *\n * @description This method gracefully handles database connectivity issues by returning\n * an empty array when the classroom registrations document cannot be accessed.\n * This ensures that users can still access other application features even\n * when classroom functionality is temporarily unavailable.\n */\n public async getActiveClasses(): Promise<string[]> {\n try {\n return (await this.getOrCreateClassroomRegistrationsDoc()).registrations\n .filter((c) => c.registeredAs === 'student')\n .map((c) => c.classID);\n } catch (error) {\n logger.warn(\n 'Failed to load classroom registrations, continuing without classroom data:',\n error\n );\n // Return empty array so user can still access other features\n return [];\n }\n }\n\n public async scheduleCardReview(review: {\n user: string;\n course_id: string;\n card_id: PouchDB.Core.DocumentId;\n time: Moment;\n scheduledFor: ScheduledCard['scheduledFor'];\n schedulingAgentId: ScheduledCard['schedulingAgentId'];\n }) {\n return scheduleCardReviewLocal(this.writeDB, review);\n }\n public async removeScheduledCardReview(reviewId: string): Promise<void> {\n return removeScheduledCardReviewLocal(this.writeDB, reviewId);\n }\n\n public async registerForClassroom(\n _classId: string,\n _registerAs: 'student' | 'teacher' | 'aide' | 'admin'\n ): Promise<PouchDB.Core.Response> {\n return registerUserForClassroom(this._username, _classId, _registerAs);\n }\n\n public async dropFromClassroom(classId: string): Promise<PouchDB.Core.Response> {\n return dropUserFromClassroom(this._username, classId);\n }\n public async getUserClassrooms(): Promise<ClassroomRegistrationDoc> {\n return getUserClassrooms(this._username);\n }\n\n public async updateUserElo(courseId: string, elo: CourseElo): Promise<PouchDB.Core.Response> {\n return updateUserElo(this._username, courseId, elo);\n }\n\n public async getStrategyState<T>(courseId: string, strategyKey: string): Promise<T | null> {\n const docId = buildStrategyStateId(courseId, strategyKey);\n try {\n const doc = await this.localDB.get<StrategyStateDoc<T>>(docId);\n return doc.data;\n } catch (e) {\n const err = e as PouchError;\n if (err.status === 404) {\n return null;\n }\n throw e;\n }\n }\n\n public async putStrategyState<T>(courseId: string, strategyKey: string, data: T): Promise<void> {\n const docId = buildStrategyStateId(courseId, strategyKey);\n let existingRev: string | undefined;\n\n try {\n const existing = await this.localDB.get<StrategyStateDoc<T>>(docId);\n existingRev = existing._rev;\n } catch (e) {\n const err = e as PouchError;\n if (err.status !== 404) {\n throw e;\n }\n }\n\n const doc: StrategyStateDoc<T> = {\n _id: docId,\n _rev: existingRev,\n docType: DocType.STRATEGY_STATE,\n courseId,\n strategyKey,\n data,\n updatedAt: new Date().toISOString(),\n };\n\n await this.localDB.put(doc);\n }\n\n public async deleteStrategyState(courseId: string, strategyKey: string): Promise<void> {\n const docId = buildStrategyStateId(courseId, strategyKey);\n try {\n const doc = await this.localDB.get(docId);\n await this.localDB.remove(doc);\n } catch (e) {\n const err = e as PouchError;\n if (err.status === 404) {\n return;\n }\n throw e;\n }\n }\n}\n\nexport function accomodateGuest(): {\n username: string;\n firstVisit: boolean;\n} {\n logger.log('[funnel] accomodateGuest() called');\n\n // Check if localStorage is available (browser environment)\n if (typeof localStorage === 'undefined') {\n logger.log(\n '[funnel] localStorage not available (Node.js environment), returning default guest'\n );\n return {\n username: GuestUsername + 'nodejs-test',\n firstVisit: true,\n };\n }\n\n const dbUUID = 'sk-guest-uuid';\n let firstVisit: boolean;\n\n const existingUUID = localStorage.getItem(dbUUID);\n logger.log('[funnel] Checking localStorage for key:', dbUUID);\n logger.log('[funnel] Existing UUID value:', existingUUID);\n logger.log('[funnel] existingUUID !== null:', existingUUID !== null);\n\n if (existingUUID !== null) {\n firstVisit = false;\n logger.log(`[funnel] Returning guest ${existingUUID} \"logging in\".`);\n } else {\n firstVisit = true;\n logger.log('[funnel] No existing UUID, generating new one...');\n const uuid = generateUUID();\n logger.log('[funnel] Generated UUID:', uuid);\n logger.log('[funnel] UUID length:', uuid.length);\n\n try {\n localStorage.setItem(dbUUID, uuid);\n logger.log('[funnel] Successfully stored UUID in localStorage');\n const verification = localStorage.getItem(dbUUID);\n logger.log('[funnel] Verification read from localStorage:', verification);\n } catch (e) {\n logger.error('[funnel] ERROR storing UUID:', e);\n }\n\n logger.log(`[funnel] Accommodating a new guest with account: ${uuid}`);\n }\n\n const finalUUID = localStorage.getItem(dbUUID);\n const finalUsername = GuestUsername + finalUUID;\n logger.log('[funnel] Final UUID from localStorage:', finalUUID);\n logger.log('[funnel] GuestUsername constant:', GuestUsername);\n logger.log('[funnel] Final username to return:', finalUsername);\n\n return {\n username: finalUsername,\n firstVisit: firstVisit,\n };\n\n // Use cryptographically secure UUID generation\n function generateUUID() {\n logger.log('[funnel] Inside generateUUID()');\n\n // Use crypto.randomUUID() if available (Node 14.17+ / modern browsers)\n if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {\n const uuid = crypto.randomUUID();\n logger.log('[funnel] Generated UUID using crypto.randomUUID():', uuid);\n return uuid;\n }\n\n // Fallback for older environments: use crypto.getRandomValues()\n if (typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function') {\n const bytes = new Uint8Array(16);\n crypto.getRandomValues(bytes);\n\n // Set version (4) and variant bits according to RFC 4122\n bytes[6] = (bytes[6] & 0x0f) | 0x40; // Version 4\n bytes[8] = (bytes[8] & 0x3f) | 0x80; // Variant 10\n\n const uuid = [\n Array.from(bytes.slice(0, 4))\n .map((b) => b.toString(16).padStart(2, '0'))\n .join(''),\n Array.from(bytes.slice(4, 6))\n .map((b) => b.toString(16).padStart(2, '0'))\n .join(''),\n Array.from(bytes.slice(6, 8))\n .map((b) => b.toString(16).padStart(2, '0'))\n .join(''),\n Array.from(bytes.slice(8, 10))\n .map((b) => b.toString(16).padStart(2, '0'))\n .join(''),\n Array.from(bytes.slice(10, 16))\n .map((b) => b.toString(16).padStart(2, '0'))\n .join(''),\n ].join('-');\n\n logger.log('[funnel] Generated UUID using crypto.getRandomValues():', uuid);\n return uuid;\n }\n\n // Last resort fallback (should never happen in modern environments)\n logger.warn('[funnel] crypto API not available, using timestamp-based UUID (NOT SECURE)');\n let d = new Date().getTime();\n if (typeof performance !== 'undefined' && typeof performance.now === 'function') {\n d += performance.now();\n }\n const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {\n const r = (d + Math.random() * 16) % 16 | 0;\n d = Math.floor(d / 16);\n return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);\n });\n logger.log('[funnel] Generated UUID (fallback):', uuid);\n return uuid;\n }\n}\n\nconst userCoursesDoc = 'CourseRegistrations';\nconst userClassroomsDoc = 'ClassroomRegistrations';\n\nasync function getOrCreateClassroomRegistrationsDoc(\n user: string\n): Promise<ClassroomRegistrationDoc & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta> {\n let ret;\n\n try {\n ret = await getLocalUserDB(user).get<ClassroomRegistrationDoc>(userClassroomsDoc);\n } catch (e) {\n const err = e as PouchError;\n\n if (err.status === 404) {\n // doc does not exist. Create it and then run this fcn again.\n await getLocalUserDB(user).put<ClassroomRegistrationDoc>({\n _id: userClassroomsDoc,\n registrations: [],\n });\n ret = await getOrCreateClassroomRegistrationsDoc(user);\n } else {\n // Properly serialize error information\n const errorDetails = {\n name: err.name,\n status: err.status,\n message: err.message,\n reason: err.reason,\n error: err.error,\n };\n\n logger.error(\n 'Database error in getOrCreateClassroomRegistrationsDoc (standalone function):',\n errorDetails\n );\n\n throw new Error(\n `Database error accessing classroom registrations: ${err.message || err.name || 'Unknown error'} (status: ${err.status})`\n );\n }\n }\n\n return ret;\n}\n\nasync function getOrCreateCourseRegistrationsDoc(\n user: string\n): Promise<CourseRegistrationDoc & PouchDB.Core.IdMeta & PouchDB.Core.GetMeta> {\n let ret;\n\n try {\n ret = await getLocalUserDB(user).get<CourseRegistrationDoc>(userCoursesDoc);\n } catch (e) {\n const err = e as PouchError;\n if (err.status === 404) {\n // doc does not exist. Create it and then run this fcn again.\n await getLocalUserDB(user).put<CourseRegistrationDoc>({\n _id: userCoursesDoc,\n courses: [],\n studyWeight: {},\n });\n ret = await getOrCreateCourseRegistrationsDoc(user);\n } else {\n throw new Error(\n `Unexpected error ${JSON.stringify(e)} in getOrCreateCourseRegistrationDoc...`\n );\n }\n }\n\n return ret;\n}\n\nexport async function updateUserElo(user: string, course_id: string, elo: CourseElo) {\n const regDoc = await getOrCreateCourseRegistrationsDoc(user);\n const course = regDoc.courses.find((c) => c.courseID === course_id)!;\n course.elo = elo;\n return getLocalUserDB(user).put(regDoc);\n}\n\nexport async function registerUserForClassroom(\n user: string,\n classID: string,\n registerAs: 'student' | 'teacher' | 'aide' | 'admin'\n) {\n log(`Registering user: ${user} in course: ${classID}`);\n return getOrCreateClassroomRegistrationsDoc(user).then((doc) => {\n const regItem = {\n classID: classID,\n registeredAs: registerAs,\n };\n\n if (\n doc.registrations.filter((reg) => {\n return reg.classID === regItem.classID && reg.registeredAs === regItem.registeredAs;\n }).length === 0\n ) {\n doc.registrations.push(regItem);\n } else {\n log(`User ${user} is already registered for class ${classID}`);\n }\n\n return getLocalUserDB(user).put(doc);\n });\n}\n\nexport async function dropUserFromClassroom(user: string, classID: string) {\n return getOrCreateClassroomRegistrationsDoc(user).then((doc) => {\n let index: number = -1;\n\n for (let i = 0; i < doc.registrations.length; i++) {\n if (doc.registrations[i].classID === classID) {\n index = i;\n }\n }\n\n if (index !== -1) {\n doc.registrations.splice(index, 1);\n }\n return getLocalUserDB(user).put(doc);\n });\n}\n\nexport async function getUserClassrooms(user: string) {\n return getOrCreateClassroomRegistrationsDoc(user);\n}\n","// packages/db/src/impl/common/index.ts\n\nexport type { SyncStrategy } from './SyncStrategy';\nexport { BaseSyncStrategy } from './SyncStrategy';\nexport type {\n AccountCreationResult,\n AuthenticationResult,\n UserSession,\n SyncConfig,\n SyncStatus,\n} from './types';\nexport { BaseUser, accomodateGuest } from './BaseUserDB';\nexport {\n REVIEW_TIME_FORMAT,\n hexEncode,\n filterAllDocsByPrefix,\n getStartAndEndKeys,\n updateGuestAccountExpirationDate,\n getLocalUserDB,\n scheduleCardReviewLocal,\n removeScheduledCardReviewLocal,\n} from './userDBHelpers';\n","// db/src/factory.ts\n\nimport { DataLayerProvider } from './core/interfaces';\nimport { BaseUser } from './impl/common';\nimport { logger } from './util/logger';\nimport { StaticCourseManifest } from './util/packer/types';\n\nconst NOT_SET = 'NOT_SET' as const;\n\ninterface DBEnv {\n COUCHDB_SERVER_URL: string; // URL of CouchDB server\n COUCHDB_SERVER_PROTOCOL: string; // Protocol of CouchDB server (http or https)\n COUCHDB_USERNAME?: string;\n COUCHDB_PASSWORD?: string;\n LOCAL_STORAGE_PREFIX: string; // Prefix for IndexedDB storage names\n}\n\nexport const ENV: DBEnv = {\n COUCHDB_SERVER_PROTOCOL: NOT_SET,\n COUCHDB_SERVER_URL: NOT_SET,\n LOCAL_STORAGE_PREFIX: '',\n};\n\nexport { NOT_SET };\n\n// Configuration type for data layer initialization\nexport interface DataLayerConfig {\n type: 'couch' | 'static';\n options: {\n staticContentPath?: string; // Path to static content JSON files\n localStoragePrefix?: string; // Prefix for IndexedDB storage names\n manifests?: Record<string, StaticCourseManifest>; // Course manifests for static mode\n COUCHDB_SERVER_URL?: string;\n COUCHDB_SERVER_PROTOCOL?: string;\n COUCHDB_USERNAME?: string;\n COUCHDB_PASSWORD?: string;\n\n COURSE_IDS?: string[];\n };\n}\n\n// Singleton instance\nlet dataLayerInstance: DataLayerProvider | null = null;\n\n/**\n * Initialize the data layer with the specified configuration\n */\nexport async function initializeDataLayer(config: DataLayerConfig): Promise<DataLayerProvider> {\n if (dataLayerInstance) {\n logger.warn('Data layer already initialized. Returning existing instance.');\n return dataLayerInstance;\n }\n if (config.options.localStoragePrefix) {\n ENV.LOCAL_STORAGE_PREFIX = config.options.localStoragePrefix;\n }\n\n if (config.type === 'couch') {\n if (!config.options.COUCHDB_SERVER_URL || !config.options.COUCHDB_SERVER_PROTOCOL) {\n throw new Error('Missing CouchDB server URL or protocol');\n }\n ENV.COUCHDB_SERVER_PROTOCOL = config.options.COUCHDB_SERVER_PROTOCOL;\n ENV.COUCHDB_SERVER_URL = config.options.COUCHDB_SERVER_URL;\n ENV.COUCHDB_USERNAME = config.options.COUCHDB_USERNAME;\n ENV.COUCHDB_PASSWORD = config.options.COUCHDB_PASSWORD;\n\n if (\n config.options.COUCHDB_PASSWORD &&\n config.options.COUCHDB_USERNAME &&\n typeof window !== 'undefined'\n ) {\n // Dynamic import to avoid loading both implementations when only one is needed\n const { CouchDBSyncStrategy } = await import('./impl/couch/CouchDBSyncStrategy');\n\n // Create a sync strategy instance and authenticate\n const syncStrategy = new CouchDBSyncStrategy();\n\n const user = await BaseUser.instance(syncStrategy, config.options.COUCHDB_USERNAME);\n const authResult = await user.login(\n config.options.COUCHDB_USERNAME,\n config.options.COUCHDB_PASSWORD\n );\n\n if (authResult.ok) {\n logger.info(`Successfully authenticated as ${config.options.COUCHDB_USERNAME}`);\n } else {\n logger.warn(`Authentication failed: ${authResult.error}`);\n }\n }\n\n // Dynamic import to avoid loading both implementations when only one is needed\n const { CouchDataLayerProvider } = await import('./impl/couch/PouchDataLayerProvider');\n dataLayerInstance = new CouchDataLayerProvider(config.options.COURSE_IDS);\n } else if (config.type === 'static') {\n const { StaticDataLayerProvider } = await import('./impl/static/StaticDataLayerProvider');\n dataLayerInstance = new StaticDataLayerProvider(config.options);\n } else {\n throw new Error(`Unknown data layer type: ${config.type}`);\n }\n\n await dataLayerInstance.initialize();\n return dataLayerInstance;\n}\n\n/**\n * Get the initialized data layer instance\n * @throws Error if not initialized\n */\nexport function getDataLayer(): DataLayerProvider {\n if (!dataLayerInstance) {\n throw new Error('Data layer not initialized. Call initializeDataLayer first.');\n }\n return dataLayerInstance;\n}\n\n/**\n * Reset the data layer (primarily for testing)\n */\nexport async function _resetDataLayer(): Promise<void> {\n if (dataLayerInstance) {\n await dataLayerInstance.teardown();\n }\n dataLayerInstance = null;\n}\n","import pouch from './pouchdb-setup';\nimport { ENV } from '@db/factory';\nimport {\n createPouchDBConfig,\n getStartAndEndKeys,\n getCredentialledCourseConfig,\n updateCredentialledCourseConfig,\n} from '.';\nimport { TeacherClassroomDB, ClassroomLookupDB } from './classroomDB';\nimport { PouchError } from './types';\n\nimport { AdminDBInterface } from '@db/core';\nimport CourseLookup from './courseLookupDB';\nimport { logger } from '@db/util/logger';\n\nexport class AdminDB implements AdminDBInterface {\n private usersDB!: PouchDB.Database;\n\n constructor() {\n // [ ] execute a check here against credentials, and throw an error\n // if the user is not an admin\n this.usersDB = new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + '_users',\n createPouchDBConfig()\n );\n }\n\n public async getUsers() {\n return (\n await this.usersDB.allDocs({\n include_docs: true,\n ...getStartAndEndKeys('org.couchdb.user:'),\n })\n ).rows.map((r) => r.doc!);\n }\n\n public async getCourses() {\n const list = await CourseLookup.allCourseWare();\n return await Promise.all(\n list.map((c) => {\n return getCredentialledCourseConfig(c._id);\n })\n );\n }\n public async removeCourse(id: string) {\n // remove the indexer\n const delResp = await CourseLookup.delete(id);\n\n // set the 'CourseConfig' to 'deleted'\n const cfg = await getCredentialledCourseConfig(id);\n cfg.deleted = true;\n const isDeletedResp = await updateCredentialledCourseConfig(id, cfg);\n\n return {\n ok: delResp.ok && isDeletedResp.ok,\n id: delResp.id,\n rev: delResp.rev,\n };\n }\n\n public async getClassrooms() {\n // const joincodes =\n const uuids = (\n await ClassroomLookupDB().allDocs<{ uuid: string }>({\n include_docs: true,\n })\n ).rows.map((r) => r.doc!.uuid);\n logger.debug(uuids.join(', '));\n\n const promisedCRDbs: TeacherClassroomDB[] = [];\n for (let i = 0; i < uuids.length; i++) {\n try {\n const db = await TeacherClassroomDB.factory(uuids[i]);\n promisedCRDbs.push(db);\n } catch (e) {\n const err = e as PouchError;\n if (err.error && err.error === 'not_found') {\n logger.warn(`db ${uuids[i]} not found`);\n }\n }\n }\n\n return promisedCRDbs.map((db) => {\n return {\n ...db.getConfig(),\n _id: db._id,\n };\n });\n }\n}\n","import { ENV, NOT_SET } from '@db/factory';\nimport { logger } from '@db/util/logger';\nimport fetch from 'cross-fetch';\n\ninterface SessionResponse {\n info: unknown;\n ok: boolean;\n userCtx: {\n name: string;\n roles: string[];\n };\n}\n\nexport async function getCurrentSession(): Promise<SessionResponse> {\n // Legacy XMLHttpRequest implementation\n // return new Promise((resolve, reject) => {\n // const authXML = new XMLHttpRequest();\n // authXML.withCredentials = true;\n //\n // authXML.onerror = (e): void => {\n // reject(new Error('Session check failed:', e));\n // };\n //\n // authXML.addEventListener('load', () => {\n // try {\n // const resp: SessionResponse = JSON.parse(authXML.responseText);\n // resolve(resp);\n // } catch (e) {\n // reject(e);\n // }\n // });\n //\n // const url = `${ENV.COUCHDB_SERVER_PROTOCOL}://${ENV.COUCHDB_SERVER_URL}_session`;\n // authXML.open('GET', url);\n // authXML.send();\n // });\n \n try {\n // Handle case where ENV variables might not be properly set\n if (ENV.COUCHDB_SERVER_URL === NOT_SET || ENV.COUCHDB_SERVER_PROTOCOL === NOT_SET) {\n throw new Error(`CouchDB server configuration not properly initialized. Protocol: \"${ENV.COUCHDB_SERVER_PROTOCOL}\", URL: \"${ENV.COUCHDB_SERVER_URL}\"`);\n }\n \n // Ensure URL has proper slash before _session endpoint\n const baseUrl = ENV.COUCHDB_SERVER_URL.endsWith('/') \n ? ENV.COUCHDB_SERVER_URL.slice(0, -1) \n : ENV.COUCHDB_SERVER_URL;\n const url = `${ENV.COUCHDB_SERVER_PROTOCOL}://${baseUrl}/_session`;\n logger.debug(`Attempting session check at: ${url}`);\n \n const response = await fetch(url, {\n method: 'GET',\n credentials: 'include',\n });\n \n if (!response.ok) {\n throw new Error(`Session check failed: ${response.status}`);\n }\n \n const resp: SessionResponse = await response.json();\n return resp;\n } catch (error) {\n // Use same URL construction logic for error reporting\n const baseUrl = ENV.COUCHDB_SERVER_URL.endsWith('/') \n ? ENV.COUCHDB_SERVER_URL.slice(0, -1) \n : ENV.COUCHDB_SERVER_URL;\n const url = `${ENV.COUCHDB_SERVER_PROTOCOL}://${baseUrl}/_session`;\n logger.error(`Session check error attempting to connect to: ${url} - ${error}`);\n throw new Error(`Session check failed connecting to ${url}: ${error}`);\n }\n}\n\nexport async function getLoggedInUsername(): Promise<string> {\n const session = await getCurrentSession();\n if (session.userCtx.name && session.userCtx.name !== '') {\n return session.userCtx.name;\n }\n // Not logged in - throw so caller can handle guest account\n throw new Error('No logged in user');\n}\n","// packages/db/src/impl/couch/CouchDBSyncStrategy.ts\n\nimport { ENV } from '@db/factory';\nimport { GuestUsername } from '../../core/types/types-legacy';\nimport { logger } from '../../util/logger';\nimport { Status } from '@vue-skuilder/common';\nimport type { SyncStrategy } from '../common/SyncStrategy';\nimport type { AccountCreationResult, AuthenticationResult } from '../common/types';\nimport { getLocalUserDB, hexEncode, updateGuestAccountExpirationDate, accomodateGuest } from '../common';\nimport pouch from './pouchdb-setup';\nimport { createPouchDBConfig } from './index';\nimport { getLoggedInUsername } from './auth';\n\nconst log = (s: any) => {\n logger.info(s);\n};\n\n/**\n * Sync strategy that implements full CouchDB remote synchronization\n * Handles account creation, authentication, and live sync with remote CouchDB server\n */\nexport class CouchDBSyncStrategy implements SyncStrategy {\n private syncHandle?: any; // Handle to cancel sync if needed\n\n setupRemoteDB(username: string): PouchDB.Database {\n if (username === GuestUsername || username.startsWith(GuestUsername)) {\n // For guest users, remote is same as local (no remote sync)\n return getLocalUserDB(username);\n } else {\n // For real users, connect to remote CouchDB\n return this.getUserDB(username);\n }\n }\n\n getWriteDB(username: string): PouchDB.Database {\n if (username === GuestUsername || username.startsWith(GuestUsername)) {\n // Guest users write to local database\n return getLocalUserDB(username);\n } else {\n // Authenticated users write to remote (which will sync to local)\n return this.getUserDB(username);\n }\n }\n\n startSync(localDB: PouchDB.Database, remoteDB: PouchDB.Database): void {\n // Only sync if local and remote are different instances\n if (localDB !== remoteDB) {\n this.syncHandle = pouch.sync(localDB, remoteDB, {\n live: true,\n retry: true,\n });\n }\n // If they're the same (guest mode), no sync needed\n }\n\n stopSync?(): void {\n if (this.syncHandle) {\n this.syncHandle.cancel();\n this.syncHandle = undefined;\n }\n }\n\n canCreateAccount(): boolean {\n return true;\n }\n\n canAuthenticate(): boolean {\n return true;\n }\n\n async createAccount(username: string, password: string): Promise<AccountCreationResult> {\n // IMPORTANT: Capture funnel username BEFORE any operations that might change session state\n const funnelUsername = await this.getCurrentUsername();\n const isFunnelAccount = funnelUsername.startsWith(GuestUsername);\n\n if (isFunnelAccount) {\n logger.info(`Creating account for funnel user ${funnelUsername} -> ${username}`);\n }\n\n try {\n const signupRequest = await this.getRemoteCouchRootDB().signUp(username, password);\n\n if (signupRequest.ok) {\n log(`CREATEACCOUNT: Successfully created account for ${username}`);\n\n // Log out any existing session\n try {\n const logoutResult = await this.getRemoteCouchRootDB().logOut();\n log(`CREATEACCOUNT: logged out: ${logoutResult.ok}`);\n } catch {\n // Ignore logout errors - might not be logged in\n }\n\n // Log in as the new user\n const loginResult = await this.getRemoteCouchRootDB().logIn(username, password);\n log(`CREATEACCOUNT: logged in as new user: ${loginResult.ok}`);\n\n if (loginResult.ok) {\n // Migrate funnel account data if applicable\n if (isFunnelAccount) {\n logger.info(`Migrating data from funnel account ${funnelUsername} to ${username}`);\n const migrationResult = await this.migrateFunnelData(funnelUsername, username);\n if (!migrationResult.success) {\n logger.warn(`Migration failed: ${migrationResult.error}`);\n // Continue anyway - don't block account creation\n }\n }\n\n return {\n status: Status.ok,\n error: undefined,\n };\n } else {\n return {\n status: Status.error,\n error: 'Failed to log in after account creation',\n };\n }\n } else {\n logger.warn(`Signup not OK: ${JSON.stringify(signupRequest)}`);\n return {\n status: Status.error,\n error: 'Account creation failed',\n };\n }\n } catch (e: any) {\n if (e.reason === 'Document update conflict.') {\n return {\n status: Status.error,\n error: 'This username is taken!',\n };\n }\n logger.error(`Error on signup: ${JSON.stringify(e)}`);\n return {\n status: Status.error,\n error: e.message || 'Unknown error during account creation',\n };\n }\n }\n\n async authenticate(username: string, password: string): Promise<AuthenticationResult> {\n try {\n const loginResult = await this.getRemoteCouchRootDB().logIn(username, password);\n\n if (loginResult.ok) {\n log(`Successfully logged in as ${username}`);\n return {\n ok: true,\n };\n } else {\n log(`Login failed for ${username}`);\n return {\n ok: false,\n error: 'Invalid username or password',\n };\n }\n } catch (error: any) {\n logger.error(`Authentication error for ${username}:`, error);\n return {\n ok: false,\n error: error.message || 'Authentication failed',\n };\n }\n }\n\n async logout(): Promise<AuthenticationResult> {\n try {\n const result = await this.getRemoteCouchRootDB().logOut();\n return {\n ok: result.ok,\n error: result.ok ? undefined : 'Logout failed',\n };\n } catch (error: any) {\n logger.error('Logout error:', error);\n return {\n ok: false,\n error: error.message || 'Logout failed',\n };\n }\n }\n\n async getCurrentUsername(): Promise<string> {\n logger.log('[funnel] CouchDBSyncStrategy.getCurrentUsername() called');\n try {\n const loggedInUsername = await getLoggedInUsername();\n logger.log('[funnel] getLoggedInUsername() returned:', loggedInUsername);\n return loggedInUsername;\n } catch (e) {\n // Not logged in - return unique guest account\n logger.log('[funnel] getLoggedInUsername() failed, calling accomodateGuest()');\n logger.log('[funnel] Error was:', e);\n const guestInfo = accomodateGuest();\n logger.log('[funnel] accomodateGuest() returned:', guestInfo);\n return guestInfo.username;\n }\n }\n\n /**\n * Migrate data from funnel account to real account\n */\n private async migrateFunnelData(\n oldUsername: string,\n newUsername: string\n ): Promise<{ success: boolean; error?: string }> {\n try {\n logger.info(`Starting data migration from ${oldUsername} to ${newUsername}`);\n\n const oldLocalDB = getLocalUserDB(oldUsername);\n const newLocalDB = getLocalUserDB(newUsername);\n\n // Get all docs from funnel account\n const allDocs = await oldLocalDB.allDocs({ include_docs: true });\n\n logger.info(`Found ${allDocs.rows.length} documents in funnel account`);\n\n // Filter out design docs and prepare for migration\n const docsToMigrate = allDocs.rows\n .filter(row => !row.id.startsWith('_design/'))\n .map(row => ({\n ...row.doc,\n _rev: undefined, // Remove rev to insert as new\n }));\n\n if (docsToMigrate.length > 0) {\n await newLocalDB.bulkDocs(docsToMigrate);\n logger.info(`Successfully migrated ${docsToMigrate.length} documents from ${oldUsername} to ${newUsername}`);\n } else {\n logger.info('No documents to migrate from funnel account');\n }\n\n return { success: true };\n } catch (error) {\n logger.error('Migration failed:', error);\n return {\n success: false,\n error: error instanceof Error ? error.message : 'Unknown error'\n };\n }\n }\n\n /**\n * Get remote CouchDB root database for authentication operations\n */\n private getRemoteCouchRootDB(): PouchDB.Database {\n const remoteStr: string =\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + 'skuilder';\n\n try {\n return new pouch(remoteStr, {\n skip_setup: true,\n });\n } catch (error) {\n logger.error('Failed to initialize remote CouchDB connection:', error);\n throw new Error(`Failed to initialize CouchDB: ${JSON.stringify(error)}`);\n }\n }\n\n /**\n * Get remote user database for a specific user\n */\n private getUserDB(username: string): PouchDB.Database {\n const guestAccount: boolean = false;\n\n const hexName = hexEncode(username);\n const dbName = `userdb-${hexName}`;\n log(`Fetching user database: ${dbName} (${username})`);\n\n // Odd construction here the result of a bug in the\n // interaction between pouch, pouch-auth.\n // see: https://github.com/pouchdb-community/pouchdb-authentication/issues/239\n const ret = new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,\n createPouchDBConfig()\n );\n\n if (guestAccount) {\n updateGuestAccountExpirationDate(ret);\n }\n\n return ret;\n }\n}\n","import { ENV } from '@db/factory';\nimport {\n DocType,\n DocTypePrefixes,\n GuestUsername,\n log,\n SkuilderCourseData,\n} from '../../core/types/types-legacy';\nimport fetch from 'cross-fetch';\n// import { getCurrentUser } from '../../stores/useAuthStore';\nimport moment, { Moment } from 'moment';\nimport { logger } from '@db/util/logger';\n\nimport pouch from './pouchdb-setup';\n\nimport { ScheduledCard } from '@db/core/types/user';\nimport process from 'process';\n\nconst isBrowser = typeof window !== 'undefined';\n\nif (isBrowser) {\n (window as any).process = process; // required as a fix for pouchdb - see #18\n}\n\nconst expiryDocID: string = 'GuestAccountExpirationDate';\n\nconst GUEST_LOCAL_DB = `userdb-${GuestUsername}`;\nexport const localUserDB: PouchDB.Database = new pouch(GUEST_LOCAL_DB);\n\nexport function hexEncode(str: string): string {\n let hex: string;\n let returnStr: string = '';\n\n for (let i = 0; i < str.length; i++) {\n hex = str.charCodeAt(i).toString(16);\n returnStr += ('000' + hex).slice(3);\n }\n\n return returnStr;\n}\nconst pouchDBincludeCredentialsConfig: PouchDB.Configuration.RemoteDatabaseConfiguration = {\n fetch(url: string | Request, opts: RequestInit): Promise<Response> {\n opts.credentials = 'include';\n\n return (pouch as any).fetch(url, opts);\n },\n} as PouchDB.Configuration.RemoteDatabaseConfiguration;\n\n/**\n * Creates PouchDB configuration with appropriate authentication method\n * - Uses HTTP Basic Auth when credentials are available (Node.js/MCP)\n * - Falls back to cookie auth for browser environments\n */\nexport function createPouchDBConfig(): PouchDB.Configuration.RemoteDatabaseConfiguration {\n // Check if running in Node.js with explicit credentials\n const hasExplicitCredentials = ENV.COUCHDB_USERNAME && ENV.COUCHDB_PASSWORD;\n const isNodeEnvironment = typeof window === 'undefined';\n \n if (hasExplicitCredentials && isNodeEnvironment) {\n // Use HTTP Basic Auth for Node.js environments (MCP server)\n return {\n fetch(url: string | Request, opts: RequestInit = {}): Promise<Response> {\n const basicAuth = btoa(`${ENV.COUCHDB_USERNAME}:${ENV.COUCHDB_PASSWORD}`);\n const headers = new Headers(opts.headers || {});\n headers.set('Authorization', `Basic ${basicAuth}`);\n \n const newOpts = {\n ...opts,\n headers: headers\n };\n \n return (pouch as any).fetch(url, newOpts);\n }\n } as PouchDB.Configuration.RemoteDatabaseConfiguration;\n }\n \n // Use cookie-based auth for browser environments or when no explicit credentials\n return pouchDBincludeCredentialsConfig;\n}\n\nfunction getCouchDB(dbName: string): PouchDB.Database {\n return new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,\n createPouchDBConfig()\n );\n}\n\nexport function getCourseDB(courseID: string): PouchDB.Database {\n // todo: keep a cache of opened courseDBs? need to benchmark this somehow\n return new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + 'coursedb-' + courseID,\n createPouchDBConfig()\n );\n}\n\nexport async function getLatestVersion() {\n try {\n const docs = await getCouchDB('version').allDocs({\n descending: true,\n limit: 1,\n });\n if (docs && docs.rows && docs.rows[0]) {\n return docs.rows[0].id;\n } else {\n return '0.0.0';\n }\n } catch {\n return '-1';\n }\n}\n\n/**\n * Checks the remote couchdb to see if a given username is available\n * @param username The username to be checked\n */\nexport async function usernameIsAvailable(username: string): Promise<boolean> {\n log(`Checking availability of ${username}`);\n \n // Legacy XMLHttpRequest implementation (browser sync)\n // const req = new XMLHttpRequest();\n // const url = ENV.COUCHDB_SERVER_URL + 'userdb-' + hexEncode(username);\n // req.open('HEAD', url, false);\n // req.send();\n // return req.status === 404;\n \n try {\n const url = ENV.COUCHDB_SERVER_URL + 'userdb-' + hexEncode(username);\n const response = await fetch(url, { method: 'HEAD' });\n return response.status === 404;\n } catch (error) {\n log(`Error checking username availability: ${error}`);\n return false;\n }\n}\n\nexport function updateGuestAccountExpirationDate(guestDB: PouchDB.Database<object>) {\n const currentTime = moment.utc();\n const expirationDate: string = currentTime.add(2, 'months').toISOString();\n\n void guestDB\n .get(expiryDocID)\n .then((doc) => {\n return guestDB.put({\n _id: expiryDocID,\n _rev: doc._rev,\n date: expirationDate,\n });\n })\n .catch(() => {\n return guestDB.put({\n _id: expiryDocID,\n date: expirationDate,\n });\n });\n}\n\nexport function getCourseDocs<T extends SkuilderCourseData>(\n courseID: string,\n docIDs: string[],\n options: PouchDB.Core.AllDocsOptions = {}\n) {\n return getCourseDB(courseID).allDocs<T>({\n ...options,\n keys: docIDs,\n });\n}\n\nexport function getCourseDoc<T extends SkuilderCourseData>(\n courseID: string,\n docID: PouchDB.Core.DocumentId,\n options: PouchDB.Core.GetOptions = {}\n): Promise<T> {\n return getCourseDB(courseID).get<T>(docID, options);\n}\n\n/**\n * Returns *all* cards from the parameter courses, in\n * 'qualified' card format (\"courseid-cardid\")\n *\n * @param courseIDs A list of all course_ids to get cards from\n */\nexport async function getRandomCards(courseIDs: string[]) {\n if (courseIDs.length === 0) {\n throw new Error(`getRandomCards:\\n\\tAttempted to get all cards from no courses!`);\n } else {\n const courseResults = await Promise.all(\n courseIDs.map((course) => {\n return getCourseDB(course).find({\n selector: {\n docType: DocType.CARD,\n },\n limit: 1000,\n });\n })\n );\n\n const ret: string[] = [];\n courseResults.forEach((courseCards, index) => {\n courseCards.docs.forEach((doc) => {\n ret.push(`${courseIDs[index]}-${doc._id}`);\n });\n });\n\n return ret;\n }\n}\n\nexport const REVIEW_TIME_FORMAT: string = 'YYYY-MM-DD--kk:mm:ss-SSS';\n\nexport function getCouchUserDB(username: string): PouchDB.Database {\n const guestAccount: boolean = false;\n // console.log(`Getting user db: ${username}`);\n\n const hexName = hexEncode(username);\n const dbName = `userdb-${hexName}`;\n log(`Fetching user database: ${dbName} (${username})`);\n\n // odd construction here the result of a bug in the\n // interaction between pouch, pouch-auth.\n // see: https://github.com/pouchdb-community/pouchdb-authentication/issues/239\n const ret = new pouch(\n ENV.COUCHDB_SERVER_PROTOCOL + '://' + ENV.COUCHDB_SERVER_URL + dbName,\n createPouchDBConfig()\n );\n if (guestAccount) {\n updateGuestAccountExpirationDate(ret);\n }\n\n return ret;\n}\n\nexport function scheduleCardReview(review: {\n user: string;\n course_id: string;\n card_id: PouchDB.Core.DocumentId;\n time: Moment;\n scheduledFor: ScheduledCard['scheduledFor'];\n schedulingAgentId: ScheduledCard['schedulingAgentId'];\n}) {\n const now = moment.utc();\n logger.info(`Scheduling for review in: ${review.time.diff(now, 'h') / 24} days`);\n void getCouchUserDB(review.user).put<ScheduledCard>({\n _id: DocTypePrefixes[DocType.SCHEDULED_CARD] + review.time.format(REVIEW_TIME_FORMAT),\n cardId: review.card_id,\n reviewTime: review.time.toISOString(),\n courseId: review.course_id,\n scheduledAt: now.toISOString(),\n scheduledFor: review.scheduledFor,\n schedulingAgentId: review.schedulingAgentId,\n });\n}\n\nexport function filterAllDocsByPrefix<T>(\n db: PouchDB.Database,\n prefix: string,\n opts?: PouchDB.Core.AllDocsOptions\n) {\n // see couchdb docs 6.2.2:\n // Guide to Views -> Views Collation -> String Ranges\n const options: PouchDB.Core.AllDocsWithinRangeOptions = {\n startkey: prefix,\n endkey: prefix + '\\ufff0',\n include_docs: true,\n };\n\n if (opts) {\n Object.assign(options, opts);\n }\n return db.allDocs<T>(options);\n}\n\nexport function getStartAndEndKeys(key: string): {\n startkey: string;\n endkey: string;\n} {\n return {\n startkey: key,\n endkey: key + '\\ufff0',\n };\n}\n\n//////////////////////\n// Package exports\n//////////////////////\n\nexport * from '../../core/interfaces/contentSource';\nexport * from './adminDB';\nexport * from './classroomDB';\nexport * from './courseAPI';\nexport * from './courseDB';\nexport * from './CouchDBSyncStrategy';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAOM,eAEO;AATb;AAAA;AAAA;AAOA,IAAM,gBAAgB,OAAO,YAAY,eAAe,QAAQ,IAAI,aAAa;AAE1E,IAAM,SAAS;AAAA;AAAA;AAAA;AAAA,MAIpB,OAAO,CAAC,YAAoB,SAAsB;AAChD,YAAI,eAAe;AAEjB,kBAAQ,MAAM,cAAc,OAAO,IAAI,GAAG,IAAI;AAAA,QAChD;AAAA,MACF;AAAA;AAAA;AAAA;AAAA,MAKA,MAAM,CAAC,YAAoB,SAAsB;AAE/C,gBAAQ,KAAK,aAAa,OAAO,IAAI,GAAG,IAAI;AAAA,MAC9C;AAAA;AAAA;AAAA;AAAA,MAKA,MAAM,CAAC,YAAoB,SAAsB;AAE/C,gBAAQ,KAAK,aAAa,OAAO,IAAI,GAAG,IAAI;AAAA,MAC9C;AAAA;AAAA;AAAA;AAAA,MAKA,OAAO,CAAC,YAAoB,SAAsB;AAEhD,gBAAQ,MAAM,cAAc,OAAO,IAAI,GAAG,IAAI;AAAA,MAChD;AAAA;AAAA;AAAA;AAAA,MAKA,KAAK,CAAC,YAAoB,SAAsB;AAC9C,YAAI,eAAe;AAEjB,kBAAQ,IAAI,YAAY,OAAO,IAAI,GAAG,IAAI;AAAA,QAC5C;AAAA,MACF;AAAA,IACF;AAAA;AAAA;;;ACrDA,oBACA,qBACA,+BAoBO;AAtBP;AAAA;AAAA;AAAA,qBAAoB;AACpB,0BAAwB;AACxB,oCAAwB;AAG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v4","import_common","module","import_common","import_common","moment","getCourseDB","updateCardElo","import_common","import_moment","init_classroomDB","moment","import_common","import_common","init_classroomDB","init_courseDB","init_courseDB","import_common","filterAllDocsByPrefix","getStartAndEndKeys","moment","expiryDocID","REVIEW_TIME_FORMAT","log","import_moment","import_moment","moment","uuid","log","import_common","import_moment","getStartAndEndKeys","moment","REVIEW_TIME_FORMAT","record","filterAllDocsByPrefix","init_adminDB","init_classroomDB","fetch","import_common","log","getCourseDB","hexEncode","updateCardElo","updateGuestAccountExpirationDate","fetch","moment","import_cross_fetch","import_moment","init_adminDB","init_classroomDB","process"]}
|