K12Beast is a personalized tutoring app for K12 students, designed to help them master concepts through tailored lessons, examples, and quizzes based on their test results. Built with Super Grok 3 and Next.js, it leverages Supabase (or any PostgreSQL database) for data storage and the Grok/X xAI API for generating educational content.
This guide will walk you through setting up a local instance of the app from scratch.
The student experience follows this flow:
- Student Registration/Login: Students register or log in to access the app.
- New Chat Session: Students start a new chat session to begin their learning journey.
- Problem Submission with Optional Student Solution: Students input a problem and optionally a proposed solution via text or images.
- AI Problem Evaluation and Solution Comments: The AI evaluates the problem and the student’s proposed solution (if provided), offering comments on correctness and areas for improvement.
- Personalized Tutoring Lesson: The AI delivers a tailored lesson based on the problem and the student’s performance.
- More Similar Problem Examples: Upon request, the AI provides additional problem examples with solutions for self-study.
- Quizzes: Upon request, the AI offers quizzes for self-testing to reinforce learning.
- End Session: Students end the session when they feel ready.
- Share Session with Parents: Students can share the session with their parents or teachers for review.
Parents can engage with their child’s learning progress in a fun and interactive way:
- Register/Login 🔐: Parents create an account or log in to access the app.
- Start a Problem Prompt ✍️: Parents submit a problem (via text or image) to initiate a learning session for their child.
- Share with Kid 📩: Parents share the session link with their child for completion.
- Kid Clones and Completes 🚀: The child clones the session into their own workspace, works through lessons, examples, and quizzes until achieving >90% readiness.
- Kid Shares Proof of Homework 📄: The child shares the completed session with the parent, showcasing their progress and quiz results.
- Parent Happy, Kid Rewarded 🎉: Parents review the work, celebrate success, and kids earn extra credit to ask for something special (like ice cream or game time)!
Before setting up K12Beast, ensure you have the following tools and accounts:
- Node.js and npm: Version 18.x or later. Download from nodejs.org.
- Git: To clone the repository. Install from git-scm.com.
- Supabase Account: For database and storage (optional if using another PostgreSQL instance). Sign up at supabase.com.
- PostgreSQL: Version 14.x or later, if not using Supabase. Install from postgresql.org.
- xAI API Key: For generating tutoring content. Request access at x.ai.
- Text Editor: Such as VS Code, for editing code.
- Browser: A modern browser like Chrome or Firefox to test the app.
To get started, clone the K12Beast repository to your local machine:
- Open a terminal and run the following command to clone the repo:
git clone https://github.com/ivelin/k12beast.git - Navigate to the project directory:
cd k12beast
K12Beast requires environment variables to connect to Supabase (or PostgreSQL), the xAI API, and for testing. Follow these steps:
- Create a
.env.localfile in the root of the project:touch .env.local - Open
.env.localin a text editor and add the following variables:NEXT_PUBLIC_SUPABASE_URL=your-supabase-url NEXT_PUBLIC_SUPABASE_ANON_KEY=your-supabase-anon-key SUPABASE_SERVICE_ROLE_KEY=your-supabase-service-role-key XAI_API_KEY=your-xai-api-key XAI_MODEL_NAME=<your-model-name> TEST_USER_EMAIL=testuser@example.com TEST_USER_PASSWORD=password123- Replace
your-supabase-urlandyour-supabase-anon-keywith values from your Supabase project’s API settings. - Replace
your-supabase-service-role-keywith the service role key (keep this secret, do not expose in client-side code). - Replace
your-xai-api-keywith your xAI API key. - Replace
<your-model-name>with the latest xAI model name (check x.ai for details). - Set
TEST_USER_EMAILandTEST_USER_PASSWORDfor local E2E testing. These must match a test user created in Supabase (see Testing section).
- Replace
- Save the file. These variables will be loaded automatically by the app.
- For CI (GitHub Actions), ensure
NEXT_PUBLIC_SUPABASE_URL,NEXT_PUBLIC_SUPABASE_ANON_KEY,SUPABASE_SERVICE_ROLE_KEY,TEST_USER_EMAIL, andTEST_USER_PASSWORDare set in your repository’s secrets (see.github/workflows/e2e-tests.yaml).
K12Beast uses Node.js dependencies managed by npm. Install them with the following steps:
- Ensure you are in the project directory (
k12beast). - Run the following command to install all dependencies:
npm install - After installation, you’ll see a message reminding you to set up your environment variables if you haven’t already. Ensure
.env.localis configured as described in the previous section. - The
node_modulesdirectory andpackage-lock.jsonwill be created, containing all required packages.
K12Beast uses a PostgreSQL database, which can be hosted via Supabase or any PostgreSQL instance. Follow these steps:
-
Using Supabase (Cloud):
- Log in to your Supabase account and create a new project.
- Note your project’s URL and anon key from the API settings.
- In the Supabase dashboard, go to "Project Settings" > "API" to find your service role key.
- Deploy the
execute-sqlEdge Function before running migrations:- Copy the code from
src/supabase/functions/execute-sql/index.ts. - In the Supabase dashboard, go to "Edge Functions," create a new function named
execute-sql, and paste the code. - Deploy the function.
- Copy the code from
- Apply the local SQL functions:
- Copy the code from
src/supabase/functions/local-sql.sql. - In the Supabase dashboard, go to the "SQL Editor," paste the code, and run it. This sets up necessary functions and permissions, such as
validate_user_id, required for migrations.
- Copy the code from
-
Using a Local Supabase Instance:
- Ensure you have the Supabase CLI installed. Install it via npm if needed:
npm install -g supabase - Start your local Supabase instance:
supabase start - Deploy the
execute-sqlEdge Function locally before running migrations:- The
execute-sqlfunction is located atsrc/supabase/functions/execute-sql/index.ts. - Use the Supabase CLI to deploy it:
supabase functions deploy execute-sql --project-ref local - Note:
project-ref localassumes your local Supabase instance is running. If you encounter issues, ensure your Supabase CLI is logged in and linked to your project (usesupabase loginandsupabase linkif needed).
- The
- Apply the local SQL functions:
- Run the following command to apply
local-sql.sqlto your local database:psql -h localhost -p 54322 -U postgres -d k12beast -f src/supabase/functions/local-sql.sql - Adjust the host, port, and user as needed based on your
src/supabase/config.tomlsettings (default port for local Supabase is 54322).
- Run the following command to apply
- Ensure you have the Supabase CLI installed. Install it via npm if needed:
-
Using a Local PostgreSQL Instance (Without Supabase):
- Install PostgreSQL if not already installed.
- Create a new database:
createdb k12beast - Apply the local SQL functions:
- Run the following command to apply
local-sql.sqlto your database:psql -h localhost -p 5432 -U postgres -d k12beast -f src/supabase/functions/local-sql.sql - Adjust the host, port, and user as needed based on your PostgreSQL setup.
- Run the following command to apply
- Update
.env.localwith your PostgreSQL connection details instead of Supabase keys (you may need to adjust the app’s Supabase client setup insrc/supabase/serverClient.tsandsrc/supabase/browserClient.tsto use a direct PostgreSQL connection). - Note: If using a local PostgreSQL instance without Supabase, the
execute-sqlEdge Function is not applicable, and you’ll need to modify the migration scripts (scripts/migrationUtils.js) to execute SQL directly against your PostgreSQL database.
-
Ensure your database is running and accessible with the credentials provided in
.env.local.
K12Beast automatically runs database migrations on startup to set up the required schema. Here’s what happens:
- When you start the app (via
npm run devornpm start), thescripts/migrate.jsscript is executed automatically. - The migration script will:
- Connect to your Supabase or PostgreSQL database using the credentials in
.env.local. - Create necessary tables like
sessions,migrations, anddb_app_version_compatibility. - Apply any pending migrations (e.g., adding columns like
completed,notes).
- Connect to your Supabase or PostgreSQL database using the credentials in
- If migrations fail (e.g., due to network issues or missing environment variables), the app will not start, and you’ll see an error message. Check the troubleshooting section for solutions.
- You don’t need to run migrations manually unless debugging; they are handled on startup.
Once your environment is set up, you can start the K12Beast development server:
- Ensure your Supabase or PostgreSQL database is running and accessible.
- Start the development server with the following command:
npm run dev - The app will:
- Run database migrations automatically (as described in the previous section).
- Start a Next.js development server on
http://localhost:3000.
- Open your browser and navigate to
http://localhost:3000to access the app. - You should see the login page. Sign up or log in to start using K12Beast.
- Chat Page (
/chat): The main page where you can submit a problem (text or images) to start a tutoring session. After submitting a problem, you can request more examples, take a quiz, or end the session. - Session Detail Page (
/session/[sessionId]): Displays the details of a specific session, including the original problem, lesson, examples, quizzes, and notes. - Session History Page (
/history): Lists all your past sessions, allowing you to view their details by clicking on a session.
Here are solutions to common issues when setting up K12Beast:
- Error: "Missing Supabase environment variables"
- Ensure
NEXT_PUBLIC_SUPABASE_URL,NEXT_PUBLIC_SUPABASE_ANON_KEY, andSUPABASE_SERVICE_ROLE_KEYare set in.env.local. Double-check your Supabase project settings for the correct values.
- Ensure
- Error: "Failed to run database migrations on startup"
- Check your internet connection if using Supabase.
- Verify that the
execute-sqlEdge Function is deployed in Supabase (if applicable). - Ensure your database credentials in
.env.localare correct. - Check Supabase’s
authschema permissions to ensurevalidate_user_id(fromlocal-sql.sql) is accessible.
- Error: "Unexpected error in xAI request"
- Confirm that
XAI_API_KEYis set in.env.localand is valid. - Check your network connection, as the xAI API requires internet access.
- Confirm that
- App doesn’t load at
http://localhost:3000- Ensure the development server is running (
npm run dev). - Check for port conflicts; if port 3000 is in use, Next.js will prompt you to use a different port.
- Ensure the development server is running (
For more information on the tools and technologies used in K12Beast, check out these resources:
- K12Beast Medium Article: Learn about the project’s background and motivation at medium.com/@ivelin.atanasoff.ivanov/k12beast-kids-grok-it-da024ab7a864.
- Next.js Documentation: Learn about Next.js features and APIs at nextjs.org/docs.
- Supabase Documentation: Explore Supabase features, including database and Edge Functions, at supabase.com/docs.
- PostgreSQL Documentation: For using a local PostgreSQL instance, refer to postgresql.org/docs.
- xAI API: Details on the xAI API for generating educational content are available at x.ai.
- React Documentation: Understand React concepts at react.dev.
- Install Dependencies:
npm install - E2E Tests (Playwright):
- Start Supabase:
supabase start - Create a test user in Supabase Auth (e.g.,
testuser@example.com,password123) via the Supabase dashboard or CLI. - Ensure
TEST_USER_EMAILandTEST_USER_PASSWORDin.env.localmatch the test user. - Run:
npm run test:e2e - Debug UI:
npm run test:e2e:ui
- Start Supabase:
- Server-Side Tests (Jest):
- Run:
npm run test:server
- Run:
- E2E and server tests run automatically on push/PR to
mainvia.github/workflows/e2e-tests.yaml. - Ensure
NEXT_PUBLIC_SUPABASE_URL,NEXT_PUBLIC_SUPABASE_ANON_KEY,SUPABASE_SERVICE_ROLE_KEY,TEST_USER_EMAIL, andTEST_USER_PASSWORDare set in GitHub Secrets.
- Ensure
.env.localhas Supabase keys (NEXT_PUBLIC_SUPABASE_URL,NEXT_PUBLIC_SUPABASE_ANON_KEY,SUPABASE_SERVICE_ROLE_KEY) and test user credentials (TEST_USER_EMAIL,TEST_USER_PASSWORD) for local E2E tests. - Create a test user in Supabase Auth (e.g.,
testuser@example.com,password123) via the Supabase dashboard or CLI. - No xAI keys are needed; tests mock AI calls using
tests/server/mocks/xaiClient.ts.
K12Beast welcomes contributions from the community! Follow these steps to contribute:
- Fork the Repository: Fork the repo on GitHub and clone your fork locally.
- Create a Branch: Create a new branch for your changes.
git checkout -b feature/your-feature-name - Make Changes: Implement your feature or bug fix, following the project’s coding style (e.g., use TypeScript, adhere to ESLint rules).
- Test Your Changes: Ensure your changes work by running the app (
npm run dev) and testing locally. - Commit and Push: Commit your changes with a clear message and push to your fork.
git commit -m "Add your descriptive commit message" git push origin feature/your-feature-name - Submit a Pull Request: Open a pull request (PR) on the main repository. Describe your changes, reference any related issues, and ensure your PR passes any automated checks.
- Sign the Contributor License Agreement (CLA): By contributing, you agree that your contributions are licensed under the Apache 2.0 License. No formal CLA is required, but ensure your contributions comply with the license terms.
- Code of Conduct: We follow a standard open-source Code of Conduct. Be respectful, inclusive, and collaborative in all interactions.
For questions, reach out by opening an issue on GitHub.