hot-potato-server

Public
OVERVIEW.md

The Hot Potato server turns a saved lesson into trusted context for a conversation.

The Hot Potato interface lets a teacher publish a lesson and a student ask about it. This repository handles what has to remain true behind those screens. Early AI routes answered one request at a time; a continuing tutor needed the stored lesson, its questions, teacher guidance, and the student's recent turns to meet in one place. That moved the core of the conversation to the API.

Drawing the boundaries on the server

A tutor request identifies a lesson and the current turn. The server loads the authoritative lesson document, serializes its content and questions, applies the teacher's response settings, and asks Gemini for a reply. One tutor flow handles free chat, question feedback, writing evaluation, and follow-ups, with streaming or a plain response. Signed-in students can keep answer and chat history; anonymous students can still read public lessons and use the tutor, carrying their recent thread in the request.

The same service stores accounts, lessons, access levels, collaborators, answers, and image metadata. It checks roles and lesson ownership at the API boundary. Stale edits return a conflict rather than silently overwriting another version. Teacher AI can propose outlines, sections, questions, and reviews, but generated question data is validated and the client requires a preview and acceptance before changing a lesson. Operational checks and offline tests cover failure paths as well as the happy path.

The result is the backend for the deployed learning product, not a standalone chatbot. Its important design choice is that the server owns both the lesson context and the access rules while the teacher keeps control of instructional content. Automatic student-memory updates exist in code but are disabled by default, and this repository does not establish measured educational impact.

README.md

Hot Potato — Server

The API behind teacher-led lessons and an AI study companion that helps students think through their own questions.

Node.js Express TypeScript MongoDB Gemini

Showcase · Client Repository · Workspace Repository


Overview

Many students have material to read but no one available to discuss a question or test an idea with. Hot Potato supports critical thinking through lessons and question guidance set by teachers, paired with an AI study companion that students can ask open-ended questions. The student interface is Thai-first and phone-first for people with little tech experience; public lessons and AI chat work without an account.

Teachers own the instructional content and the tutor's direction. The API stores their lesson document, questions, guide answers, and per-lesson agent_settings: response persona, whether to answer directly, lesson-only versus related general discussion, and custom guidelines. The AI can help a teacher structure pasted worksheet, Word, or note text into a draft, but the teacher previews and accepts it before publication. There is no automatic paper scanning in this flow.

This repository contains the REST API: an Express 5 + TypeScript service backed by MongoDB, providing JWT authentication (email/password and Google Sign-In), lesson CRUD with access control and optimistic concurrency, per-student answer tracking, a Cloudinary-backed image library, and the entire AI layer — a unified streaming tutor endpoint and an eleven-action teacher copilot.

Implementing a feature? Keep the root workspace repository alongside this repo. Its developer guidebook covers the client/server contract, and its graphify-out/ knowledge graph links routes, controllers, models, and client consumers. Consult the graph and both current codebases when changing an API response, lesson schema, access rule, or tutor behavior.

The project-wide overview and illustrated student and teacher use cases are in the workspace README and client README. This README is the API reference and setup guide.

Two product principles shape the API design:

  1. AI access is never rationed for real students — no per-student quotas. Rate limiting exists only to stop bots, with generous, environment-tunable thresholds.
  2. Anonymous users keep full access — the AI tutor and public lesson reads use optional authentication, never a login wall. Logging in only adds persistence (chat history, tutor memory, answer tracking).

Features

  • Unified AI study companion — a single POST /api/chat/tutor serves four conversation modes (free chat, question feedback, writing evaluation, follow-up). Students can ask beyond a right/wrong answer; the teacher's lesson and per-lesson settings guide the reply. Supports Server-Sent Events streaming with a plain-JSON fallback, follow-up suggestion chips, and six student-selectable tutor personalities.
  • Teacher AI copilot — POST /api/creator/assist exposes eleven authoring actions, including import_structure for pasted existing material, lesson outlining, drafting, question generation, and review. This lowers the work of moving teaching material online while keeping the teacher's preview-and-accept step. Generated JSON is validated server-side before it reaches the editor.
  • Teacher-guided tutor style — Content.agent_settings stores the persona note, answer directness, knowledge scope, and custom guidelines for each lesson; the server includes these in the tutor instruction along with the teacher-authored material.
  • Server-owned lesson context — the server loads and serializes the lesson document for every AI call (cached by updatedAt), so clients never ship lesson text over the wire.
  • Student memory — for logged-in students, a background job periodically distills conversations into a small personalization sketch (interests, strengths, growth areas) that is injected into the tutor's system instruction. Fully viewable and erasable by the student. (Shelved by default since 2026-07-12 — enable with AI_MEMORY_ENABLED=true.)
  • Authentication — JWT sessions with bcrypt-hashed passwords, Google Sign-In via verified Google Identity Services ID tokens (find-or-create with safe auto-linking by verified email), role-based guards (admin / creator / learner), and a structured 401 contract the client uses for automatic logout.
  • Lesson storage — TipTap documents stored per lesson with public / link-only / private access levels, collaborator support, denormalized author snapshots that propagate on rename, and optimistic-concurrency updates (409 on conflict).
  • Resilience — transient AI network errors are retried; model routing sends lightweight tasks to a fast model and coaching tasks to a stronger one; rate limiters are per-surface so teacher AI usage never consumes the student budget.
  • Observability — an in-memory error ring buffer and AI health snapshot surfaced through sanitized public status endpoints, with internals restricted to admins.
  • Offline test suite — Vitest + Supertest + in-memory MongoDB with a mocked Gemini client: no network, no real database, no API key required.

API use cases

The web client is the usual caller. These are the main journeys through the API; the route reference below lists individual endpoints and their guards.

JourneyRequest flowAuthentication
Find and read a public lessonGET /api/content/search?explore=true → GET /api/content/load?id=<contentId>Optional; access checks still apply to private lessons
Ask the study companion anything about an ideaPOST /api/chat/tutor with contentId, mode: "free_chat", and message; use stream: true for SSE. The teacher's settings determine whether related general topics are in scope.Optional; anonymous callers send recent turns in clientThread
Answer a questionThe client evaluates a choice/blank or collects writing, then calls POST /api/chat/tutor in question_feedback or write_evaluation mode with questionContextOptional; signed-in users can also save answers through /api/content-answer/:contentId
Create and publish a lessonPOST /api/content/create → PUT /api/content/:id with the document and access settingsJWT; owner or collaborator for updates
Bring existing material onlinePOST /api/creator/assist with action: "import_structure" and pasted payload.rawText; preview the proposed structure and questions in the clientJWT and lesson ownership/collaboration
Guide the tutor's styleSave agent_settings with the lesson: persona note, direct-answer preference, scope, and custom guidelinesJWT and lesson ownership/collaboration
Check service healthGET /api/status/all for a sanitized public view; GET /api/status/ for admin diagnosticsPublic / admin respectively

For a minimal anonymous tutor request after opening an accessible lesson:

curl -X POST http://localhost:5000/api/chat/tutor \
  -H 'Content-Type: application/json' \
  -d '{"contentId":"<lesson-object-id>","mode":"free_chat","message":"ช่วยอธิบายหัวข้อนี้หน่อย"}'

Use a real lesson ID from the local database. The server loads the lesson context itself and returns JSON with reply, suggestions, and sessionId; add "stream":true to receive typed SSE events instead. Teacher and saved-progress routes require Authorization: Bearer <token>.

Tech stack

AreaTechnology
RuntimeNode.js, TypeScript (CommonJS)
FrameworkExpress 5
DatabaseMongoDB with Mongoose 9
AIGoogle Gemini via @google/genai
Authjsonwebtoken, bcryptjs, google-auth-library
Rate limitingexpress-rate-limit
TestingVitest, Supertest, mongodb-memory-server
Dev toolingnodemon, ts-node
DeploymentRender

Architecture

The service follows an MVC layout. A request flows through route → auth middleware → controller → model (Mongoose) → MongoDB, with cross-cutting logic in a services/ layer and a central error handler mounted last.

src/
├── app.ts                  # builds + exports the Express app (no listen, no DB connect)
├── index.ts                # entry point: dotenv → connect DB → listen
├── config/db.ts            # Mongoose connection
├── routes/                 # URL → controller wiring, applies auth guards
├── controllers/            # request validation + business logic
├── models/                 # Mongoose schemas — the data contract
├── middlewares/
│   ├── auth.middleware.ts  # protect / optionalAuth / restrictTo
│   └── error.middleware.ts # central error handler
├── services/
│   ├── lessonContext.service.ts        # lesson serialization for AI calls (cached)
│   ├── observability.service.ts        # error ring buffer + AI health
│   ├── contentAuthorSnapshot.service.ts # denormalized author names
│   ├── tutor/                          # persona, parsing, streaming, retry,
│   │                                   #   personalities, memory, model routing
│   └── creator/                        # copilot prompts, JSON parsing, validation
└── types/                  # shared TypeScript types (AuthRequest)

The app/entry split (app.ts exports the app; index.ts boots it) keeps the full HTTP surface importable by the test suite without opening ports or touching a real database.

API reference

All endpoints are under /api. Authentication is a JWT Bearer token from /api/auth/login or /api/auth/register.

Guard levels: public (no auth) · optional (works anonymously, richer when logged in) · auth (valid JWT required) · admin (auth + admin role).

Auth — /api/auth

MethodPathGuardDescription
POST/registerpublic, rate-limitedCreate an account → { user, token }
POST/loginpublic, rate-limitedLog in → { user, token }
POST/googlepublic, rate-limitedGoogle Sign-In: verifies a GIS ID token, then find-or-create / auto-link by verified email
GET/recheckauthRe-validate the token, return a fresh user
PUT/change-passwordauthChange password

Content — /api/content

MethodPathGuardDescription
GET/load?id=optionalLoad one lesson, enforcing its access level
POST/createauthCreate a blank lesson → { content_id }
PUT/:idauthUpdate (owner/collaborator); optimistic concurrency → 409 on conflict
DELETE/:idauthDelete (owner only)
GET/search?q=&mine=&explore=optionalList lessons (own / public explore / by title)

Student answers — /api/content-answer

MethodPathGuardDescription
GET/:contentIdauthGet this user's answer map for a lesson
PUT/:contentIdauthSave one answer { blockId, answer }
PUT/:contentId/bulkauthReplace the whole answer map

AI chat — /api/chat

MethodPathGuardDescription
POST/tutoroptional, rate-limitedThe unified tutor agent — four modes (free_chat, question_feedback, write_evaluation, followup); set stream: true for SSE (token / suggestions / done / error events) or omit for JSON { reply, suggestions, sessionId }
GET/memoryauthRead the student's tutor-memory sketch
DELETE/memoryauthErase the student's tutor memory

Logged-in users get server-side per-block chat sessions (capped at 50 messages); anonymous users pass their recent thread with each request and lose nothing but persistence.

Teacher copilot — /api/creator

MethodPathGuardDescription
POST/assistauth + owner/collaborator, rate-limitedOne endpoint, eleven actions via { contentId, action, payload }: outline, draft_section, import_structure, proofread, generate_questions, guide_answer, distractors, formula_latex, critic, lesson_meta, agent_settings_suggest

Users, history, media, status

GroupBase pathDescription
Users/api/usersGET/PUT /me/profile (auth); GET / lists all users (admin)
History/api/historyRecently visited lessons (auth)
Categories/api/categoriesImage-library folders — full CRUD (auth)
Images/api/imagesCloudinary image-library metadata (auth)
Status/api/statusGET /all aggregated public health check (AI health + sanitized recent errors); GET / internals (admin)

Data model

ModelPurpose
UserAccount: name, unique email, bcrypt-hashed password (optional for Google-only accounts), googleId, role (admin / creator / learner), status
UserDataExtended profile: avatar, bio, flexible metadata
ContentA lesson: owner, collaborators, title, stringified TipTap document, access level, topics, description, per-lesson AI tutor settings, denormalized author names
UserContentPer (user, lesson) answer map and progress tracker
LearningHistoryPer (user, lesson) recency record for "recently opened"
ChatSessionPer (user, lesson, block) tutor conversation, capped at 50 messages
StudentMemoryOne personalization sketch per student: interests, strengths, growth areas, preferences, recent topics, selected tutor personality
Category / ImageThe user's Cloudinary image library and its folders

AI integration

The tutor lives in src/services/tutor/ and src/controllers/tutor.controller.ts:

  • Persona — a system instruction built per request from the tutor character (warm, coaching, never shaming), teach rules, the lesson content and its questions, the teacher's per-lesson settings, the student's chosen personality preset, and the student's memory digest.
  • Modes — feedback modes wrap the student's turn with an evaluation task (the client sends deterministic per-choice/per-blank diagnostics; open-ended answers are judged by the model); free_chat and followup pass messages through untouched so casual conversation stays casual.
  • Streaming — SSE with typed events; the non-streaming JSON path shares the same parsing (reply + up to three suggestion chips).
  • Model routing — quick-check feedback uses a fast model (gemini-2.5-flash-lite by default); free chat, reflection, and writing evaluation use the tutor model (gemini-2.5-flash by default — switched from gemini-3.5-flash on 2026-07-12 for output cost; set AI_TUTOR_MODEL=gemini-3.5-flash to flip back). Both are environment-configurable.
  • Retry — transient network failures are retried with backoff before surfacing an error.

The teacher copilot (src/services/creator/) uses Gemini's JSON mode with a tolerant parser and a single retry; generated question blocks are schema-validated server-side and malformed items are dropped, so the editor never receives raw untrusted JSON.

Security

  • Passwords hashed with bcrypt (cost 12) in a model pre-save hook; never returned by any endpoint.
  • Google Sign-In verifies ID-token signatures and audience server-side via google-auth-library.
  • Structured 401 responses (TOKEN_EXPIRED, TOKEN_INVALID, …) let the client distinguish "log in again" from other failures.
  • Role guard (restrictTo) keeps diagnostics and user listings admin-only.
  • Rate limits on auth and AI routes are keyed by IP (or user ID when available) behind Render's proxy (trust proxy).
  • CORS allow-list is environment-driven; logs contain no student messages or tokens by default.

Getting started

Prerequisites

  • Node.js 20+ and npm
  • A MongoDB database (e.g. a free MongoDB Atlas cluster)
  • A Google Gemini API key (Google AI Studio)
  • A Cloudinary account (cloud name + unsigned upload preset)
  • Optional: a Google OAuth Web client ID for Google Sign-In

Installation

git clone https://github.com/Jakkarin-Promsee/hot-potato-server.git
cd hot-potato-server
npm install

Environment variables

Create a .env file in the project root (git-ignored):

PORT=5000
MONGO_URI=mongodb+srv://<user>:<pass>@<cluster>/<db>
JWT_SECRET=replace-with-a-long-random-string
JWT_EXPIRES_IN=7d
GEMINI_API_KEY=your-gemini-api-key
CLOUDINARY_CLOUD_NAME=your-cloud-name
CLOUDINARY_UPLOAD_PRESET=your-unsigned-preset

# Optional
GOOGLE_CLIENT_ID=your-google-oauth-client-id
CORS_ORIGINS=http://localhost:5173
AI_OUTPUT_LANGUAGE=thai
VariableRequiredDescription
PORTnoHTTP port (default 5000)
MONGO_URIyesMongoDB connection string; the process exits on boot if it cannot connect
JWT_SECRETyesSecret used to sign JWTs
JWT_EXPIRES_INyesToken lifetime, e.g. 7d
GEMINI_API_KEYyesGoogle Gemini API key
CLOUDINARY_CLOUD_NAMEyesCloudinary account name
CLOUDINARY_UPLOAD_PRESETyesCloudinary unsigned upload preset
GOOGLE_CLIENT_IDnoOAuth Web client ID; required only for POST /auth/google
CORS_ORIGINSnoComma-separated allowed browser origins; unset = open (dev only, boot warning)
AI_OUTPUT_LANGUAGEnothai (default) or english
AI_FAST_MODELnoModel for quick feedback + memory updates (default gemini-2.5-flash-lite)
AI_TUTOR_MODELnoModel for free chat / reflection / writing evaluation (default gemini-2.5-flash; set gemini-3.5-flash to switch back to the premium model)
AI_MEMORY_ENABLEDnoStudent-memory master switch — default off / shelved; set true to resume the sketch updates + digest injection
AI_MEMORY_EVERY_N_TURNSnoStudent-memory update cadence when enabled (default 3)
AI_RATELIMIT_PER_10MIN / AI_RATELIMIT_PER_DAYnoBot-only AI rate limits (defaults 60 / 1000)
AUTH_RATELIMIT_PER_10MINnoBot guard on login/register per IP (default 100)

Run

npm run dev      # development: nodemon + ts-node with auto-restart
npm run build    # compile TypeScript → dist/
npm start        # production: node dist/index.js (run build first)

The API listens on http://localhost:5000. GET / returns a friendly health string; GET /api/status/all returns the aggregated health check.

Available scripts

ScriptDescription
npm run devDevelopment server with auto-restart
npm run buildCompile TypeScript to dist/
npm startRun the compiled production build
npm testFull offline test suite (in-memory MongoDB, mocked Gemini)
npm run test:watchTest suite in watch mode
npm run test:aiLive Gemini smoke tests (requires a real API key, costs tokens)
npm run promote -- <email>Promote a user to admin
npm run check-modelsProbe which Gemini models the API key can access

Testing

npm test

The main suite runs fully offline: mongodb-memory-server provides a throwaway database and the Gemini client is mocked, so no network, real database, or API key is needed.

test/
├── setup.ts       # in-memory Mongo + test environment
├── fixtures/      # committed lesson fixture
├── unit/          # prompt builders, retry logic, lesson serialization
├── api/           # endpoint integration tests via Supertest
└── ai/            # live Gemini smoke tests (excluded from npm test)

Deployment

The API is configured for Render as a Node web service:

  • Build command: npm install && npm run build
  • Start command: npm start
  • Environment: set every required variable from the table above in the Render dashboard, including CORS_ORIGINS with the client's production origin.

Cold starts: on Render's free tier the service sleeps when idle, so the first request after a pause takes several seconds on top of normal AI latency. The web client accounts for this with loading states.

RepositoryDescription
hot-potato-clientReact 19 + TypeScript web client: the lesson editor, viewer, and AI chat surfaces

Author

Jakkarin Promsee — Computer Engineering, KMUTT

License

Original code and documentation authored by Jakkarin Promsee are licensed under the MIT License. Third-party code, assets, datasets, and other materials are not covered by this license; their original terms apply.