# ๐Ÿ› ๏ธ Technical Architecture

LearnAI TV is built on a **serverless, API-first architecture** designed for scalability and rapid development.

๐Ÿ—๏ธ System Overview

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Browser   โ”‚  (HTML/CSS/JS - GitHub Pages)
โ”‚  (Student)  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚ HTTPS POST
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   n8n Flow  โ”‚  (Easypanel hosted)
โ”‚  Webhook     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚
       โ”œโ”€โ”€โ–บ OpenAI GPT-4.1-mini (LLM)
       โ”œโ”€โ”€โ–บ Gemini-2.5-flash (Fallback Model)
       โ”œโ”€โ”€โ–บ Wolfram|Alpha (Math)
       โ”œโ”€โ”€โ–บ 6x MCP Servers (Subject DBs)
       โ””โ”€โ”€โ–บ Memory Buffer (Chat history)
       โ”‚
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Response  โ”‚
โ”‚   (JSON)    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“ฆ Tech Stack

Frontend

  • HTML5 - Semantic structure

  • CSS3 - Gradients, animations, flexbox/grid

  • Vanilla JavaScript - No frameworks (lightweight!)

  • KaTeX 0.16.9 - Math formula rendering

Why Vanilla JS?

  • โšก Faster load times (no React bundle)

  • ๐ŸŽฏ Educational transparency (readable code)

  • ๐Ÿ“ฆ Single-file deploy (GitHub Pages friendly)

Backend / AI Orchestration

  • n8n (self-hosted on Easypanel) - Visual workflow automation

  • OpenAI GPT-4.1-mini - Cost-effective LLM ($0.10/1M tokens)

  • Gemini Flash 2.5 API - For free

  • Wolfram|Alpha API - Mathematical computation engine

Knowledge Layer (MCP Servers)

  • 6 MCP Servers connected to Google Docs:

    1. matematica_mcp โ†’ Math curriculum

    2. portugues_mcp โ†’ Portuguese lessons

    3. ciencias_mcp โ†’ Science topics

    4. historia_mcp โ†’ History content

    5. geografia_mcp โ†’ Geography data

    6. idiomas_mcp โ†’ Language lessons

  • Wolfram MCP โ†’ Advanced math via SSE transport

Data Source: Google Docs (easily editable by teachers/parents)

Deployment

  • Frontend: GitHub Pages (free, CDN, HTTPS)

  • Backend: Easypanel (Docker containers)

  • Domain: Custom subdomain (iavendas-n8n.tkxtrv.easypanel.host)


๐Ÿ”„ n8n Workflow Design

Main Flow (Learn AI agent):

MCP Server Pattern:

Key Design Decisions:

  1. Stateless API - No database needed (MCP servers handle persistence)

  2. Session memory via origin header - Tracks conversations per user

  3. Fallback logic - JavaScript handles AI failures gracefully

  4. Timeout protection - 10-15s max per request


๐Ÿ”ฎ Wolfram|Alpha Integration

Implementation

n8n Node: Wolfram Alpha Tool Credentials: WolframAlphaApi account (app ID) Use Cases:

  • Solve equations: solve 2x + 5 = 15

  • Graphing: plot x^2 from -5 to 5

  • Unit conversion: convert 50 miles to km

  • Step-by-step solutions

Code Example (JavaScript parser)

Student Experience

When student asks: "What's the integral of xยฒ?"

  1. AI detects math complexity

  2. Calls Wolfram via MCP

  3. Returns: โˆซxยฒ dx = (xยณ/3) + C with step-by-step link

  4. Student clicks link โ†’ Full Wolfram explanation opens

Impact: Students learn how to solve, not just what the answer is.


๐Ÿค– MCP (Model Context Protocol) Servers

What are MCP Servers?

MCP is an open protocol by Anthropic that lets AI models access external data sources (like databases, APIs, or documents) in a standardized way.

Our Implementation:

Each subject has a dedicated MCP server:

Data Flow:

Why MCP?

  • โœ… Curriculum control - Teachers update Google Docs, AI uses latest info

  • โœ… Subject isolation - Math queries don't search History docs

  • โœ… Audit trail - All tool calls logged in n8n

  • โœ… Scalability - Add new subjects without changing code


๐Ÿงฎ Quiz Generation Pipeline

Challenge: Generate consistent, parseable quizzes from LLM responses.

Solution: Multi-layer parsing with fallbacks.

Prompt Engineering

Parsing Logic

Fallback System

If AI generation fails:

  1. Use generateFallbackQuiz() with pre-written questions per subject

  2. Display fallback gracefully (student never knows it failed)

  3. Log error for debugging

Result: 95%+ quiz success rate, even with varying AI outputs.


๐ŸŽจ Frontend Architecture

Design Philosophy: Progressive enhancement + accessibility first.

File Structure

State Management

Why Globals?

  • Single-page app (no routing)

  • All state resets on page reload

  • Simpler debugging (inspect in console)

API Communication

Error Handling:

  • Network failures โ†’ Show fallback content

  • Timeouts โ†’ Use cached/default data

  • Malformed responses โ†’ Parse defensively


๐Ÿ” Security & Privacy

  • โœ… No user accounts - Zero PII collected

  • โœ… Session-only memory - Chat history cleared on reload

  • โœ… Public API - Webhook is rate-limited, not secret

  • โœ… HTTPS only - GitHub Pages enforces SSL

  • โœ… CORS enabled - Safe cross-origin requests

Data Flow:

Compliance:

  • COPPA-friendly (no data collection)

  • GDPR-compliant (no personal data)

  • Safe for schools (no ads, no trackers)


๐Ÿ“Š Performance Metrics

Metric
Target
Actual

First Contentful Paint

<1.5s

0.8s

Time to Interactive

<3s

2.1s

API Response Time

<8s

3-6s

Quiz Parse Success

>90%

~95%

Mobile Usability

100/100

โœ…

Optimizations:

  • Lazy-load KaTeX (only when chat opens)

  • Debounce API calls (prevent spam)

  • Cache subject lists (reduce re-renders)

  • Minify CSS/JS (GitHub Pages auto-minifies)


๐Ÿš€ Deployment Pipeline

Local Development:

Production Deploy:

n8n Workflow Updates:

  1. Edit workflow in n8n UI

  2. Save (auto-activates)

  3. No restart needed (hot reload)


๐Ÿ”ฎ Future Architecture Plans

Phase 2 (Post-Hackathon):

Phase 3 (Scale):

Last updated