|
1 | | -# maia platform |
| 1 | +<div align="center" style="margin-bottom: 3rem;"> |
| 2 | + <img src="public/maia-no-bg.png" width="64" alt="Maia Chess Logo"> |
| 3 | + <h1><a href="https://www.maiachess.com">Maia Chess</a></h1> |
| 4 | + <i>A human-like chess engine</i> |
| 5 | +</div> |
2 | 6 |
|
3 | | -This is the code that powers the[ Maia Chess platform](https://www.maiachess.com). |
4 | | - |
5 | | -It is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with the additions of [`typescript`](https://www.typescriptlang.org/), [`eslint`](https://eslint.org/), [`storybook`](https://storybook.js.org/), [`prettier`](https://prettier.io/), [`sass`](https://sass-lang.com/), [`testing-library`](https://testing-library.com/) ,and [`jest`](https://jestjs.io/). |
| 7 | +This repository contains the source code for the [Maia Chess Platform](https://www.maiachess.com), a modern web application designed for chess training and analysis. It leverages the Maia chess engine, developed by the University of Toronto's Computational Social Science Lab, to provide human-like move predictions and insights. The platform is built with Next.js, TypeScript, and Tailwind CSS. |
6 | 8 |
|
7 | | -Initialize a new `next-app` with this project as a template by running |
| 9 | +Join our Discord server for discussions, questions, and to connect with the community: |
| 10 | +[Maia Chess Community on Discord](https://discord.gg/hHb6gqFpxZ) |
8 | 11 |
|
9 | | -```bash |
10 | | -npx create next-app --example https://github.com/datadeque/next-app |
11 | | -# or |
12 | | -yarn create next-app --example https://github.com/datadeque/next-app |
13 | | -``` |
14 | | - |
15 | | -## Development Guide |
16 | | - |
17 | | -This project uses [`yarn`](https://yarnpkg.com/) and is developed & maintained for [`node`](https://nodejs.org/en/) version 17+ (however CI will try builds using versions 12, 14, and 16 as well). |
18 | | - |
19 | | -The recommended code editor is [`vscode`](https://code.visualstudio.com/) along with the following extensions (see `.vscode/extensions.json`): |
20 | | - |
21 | | -- eslint (Linter) |
22 | | -- prettier (Formatter) |
23 | | -- mdx (Syntax Highlighting for Storybook) |
| 12 | +## Getting Started |
24 | 13 |
|
25 | | -These extensions are highly recommended along with vscode because the project is pre-configured to format and fix ALL fixable issues on save. Furthermore, please open this project by running `code .` in the root directory from your terminal since there are known issues with environment variables when opening the project from gui. |
| 14 | +Follow these instructions to set up the development environment on your local machine. |
26 | 15 |
|
27 | | -It also helps to know the following tools: |
| 16 | +### Prerequisites |
28 | 17 |
|
29 | | -- [`sass`](https://sass-lang.com/) |
30 | | -- [`react contexts`](https://reactjs.org/docs/context.html) |
31 | | -- [`react custom hooks`](https://reactjs.org/docs/hooks-custom.html) |
| 18 | +- Node.js (v17+ recommended) |
| 19 | +- npm (comes bundled with Node.js) |
32 | 20 |
|
33 | | -### Design |
| 21 | +### Installation |
34 | 22 |
|
35 | | -Each page of the platform can be found in `src/pages`. Inside, a wrapper component fetches the data from the api and renders the page itself when the data is received. An example of this is in the `train` page where `TrainPage` fetches the game, and renders `Train`. |
| 23 | +1. Clone the repository to your local machine: |
36 | 24 |
|
37 | | -We'll refer the component rendered by the wrapper as the main component. Inside the main component a custom hook is used to isolate all the logic, and two variables `mobileLayout` and `desktopLayout` are initialized to represent the corresponding layouts. |
| 25 | + ```bash |
| 26 | + git clone https://github.com/maia-chess/maia-platform-frontend.git |
| 27 | + cd maia-platform-frontend |
| 28 | + ``` |
38 | 29 |
|
39 | | -The `GameControllerContext` is used to simply pass data down, as any child component of the main component can easily consume the controller without passing it down multiple layers using props. |
| 30 | +2. Install the project dependencies using npm: |
| 31 | + ```bash |
| 32 | + npm install |
| 33 | + ``` |
40 | 34 |
|
41 | | -Each main component typically contains a `GameBoard` component which renders the lichess chessboard UI, and a family of surrounding components that are either interactive or show information such as analysis. |
| 35 | +### Running the Development Server |
42 | 36 |
|
43 | | -### Getting Started |
44 | | - |
45 | | -First, install the dependencies by running |
| 37 | +To start the local development server, run the following command. This will launch the application on `http://localhost:3000` with hot-reloading enabled. |
46 | 38 |
|
47 | 39 | ```bash |
48 | | -yarn |
49 | | -# or |
50 | | -yarn install |
| 40 | +npm run dev |
51 | 41 | ``` |
52 | 42 |
|
53 | | -Then start the development server by running |
| 43 | +### Building for Production |
| 44 | + |
| 45 | +To create a production-ready build of the application, use the following command. This will compile and optimize the code, outputting the final assets to the `.next` directory. |
54 | 46 |
|
55 | 47 | ```bash |
56 | | -yarn dev |
| 48 | +npm run build |
57 | 49 | ``` |
58 | 50 |
|
59 | | -Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. |
| 51 | +You can then start the production server with `npm run start`. |
60 | 52 |
|
61 | | -You can start editing the page by modifying `pages/index.tsx`. The page auto-updates as you edit the file. |
| 53 | +## Development Guide |
62 | 54 |
|
63 | | -### Storybook |
| 55 | +This section provides guidelines for contributing to the platform's development. |
64 | 56 |
|
65 | | -To start storybook development server by running |
| 57 | +### Branching Strategy |
66 | 58 |
|
67 | | -```bash |
68 | | -yarn storybook |
69 | | -``` |
| 59 | +The repository follows a simple branching model: |
70 | 60 |
|
71 | | -Open http://localhost:6006/ with your browser and see the result. |
| 61 | +- `main`: This branch is synced with the live deployment on Vercel. All code on this branch is considered production-ready. |
| 62 | +- **Feature Branches**: All development work, including new features and bug fixes, should be done on separate feature branches. These branches are then merged into `main` via pull requests. |
72 | 63 |
|
73 | | -### Linting & Testing |
| 64 | +### Conventional Commits |
74 | 65 |
|
75 | | -You can lint the entire project using the built-in `eslint` config by running |
| 66 | +We use the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) specification for our commit messages. This standard creates a more readable and structured commit history. Each commit message should follow the format: |
76 | 67 |
|
77 | | -```bash |
78 | | -yarn lint |
79 | | -``` |
| 68 | +`{type}: {description}` |
80 | 69 |
|
81 | | -This is the same command that runs during the lint step of the initial CI. |
| 70 | +Common types include: |
82 | 71 |
|
83 | | -You can run your test files by running |
| 72 | +- `feat`: A new feature |
| 73 | +- `fix`: A bug fix |
| 74 | +- `chore`: Changes to the build process or auxiliary tools |
| 75 | +- `style`: Code style changes (formatting, etc.) |
| 76 | +- `refactor`: A code change that neither fixes a bug nor adds a feature |
| 77 | +- `docs`: Documentation only changes |
84 | 78 |
|
85 | | -```bash |
86 | | -yarn test |
87 | | -``` |
| 79 | +### Architectural Overview |
88 | 80 |
|
89 | | -This is the same command that runs during the test step of the initial CI. |
| 81 | +The platform is architected around a modular and scalable structure, leveraging modern React patterns. |
90 | 82 |
|
91 | | -## Development Practices |
| 83 | +#### File Structure |
92 | 84 |
|
93 | | -### Committing |
| 85 | +The `src/` directory contains all the core application code, organized as follows: |
| 86 | +
|
| 87 | +``` |
| 88 | +src/ |
| 89 | +├── api/ # Backend API client functions, organized by feature |
| 90 | +├── components/ # Reusable React components, structured by feature or domain |
| 91 | +├── contexts/ # React Context providers for global state management |
| 92 | +├── hooks/ # Custom React Hooks containing business logic and state |
| 93 | +├── pages/ # Next.js pages, defining the application's routes |
| 94 | +├── providers/ # Wrappers for context providers |
| 95 | +├── styles/ # Global styles and Tailwind CSS configuration |
| 96 | +├── types/ # TypeScript type definitions, organized by feature |
| 97 | +└── utils/ # Utility functions and helpers |
| 98 | +``` |
94 | 99 |
|
95 | | -Conventional commits is a pretty simple convention, you can learm more about it [here](https://www.conventionalcommits.org/en/v1.0.0/). |
| 100 | +#### Core Concepts and Interactions |
96 | 101 |
|
97 | | -Basically commit messages follow the following format: `{action}: {description}`, where action is one of `feat`, `chore`, `fix`, `style`... and description is description of the change in present tense. |
| 102 | +The application's logic is primarily driven by a combination of custom hooks, React contexts, and components, creating a clear separation of concerns. |
98 | 103 |
|
99 | | -### Testing |
| 104 | +- **Components (`src/components/`)**: These are the building blocks of the UI. They are designed to be "dumb" or presentational, receiving data and callbacks via props. Major features like `Analysis`, `Play`, `Openings`, and `Training` have their own dedicated component directories. |
100 | 105 |
|
101 | | -Every react component should atleast have one test, to see if it renders. Test files are to be placed in `__test__` and follow the same file structure as the project root. |
| 106 | +- **Hooks (`src/hooks/`)**: This is where the majority of the application's business logic resides. Each major feature has a corresponding "controller" hook (e.g., `usePlayController`, `useAnalysisController`). These hooks encapsulate state management, interactions with the chess engines, and API calls. They effectively act as state machines for their respective features. |
102 | 107 |
|
103 | | -Suppose you built a new component under `src/components/button/button.tsx`, a test should be placed in `__tests__/components/button/button.test.tsx` with the following: |
| 108 | +- **Contexts (`src/contexts/`)**: To avoid prop drilling, we use React Context to provide the state and methods from our controller hooks to the component tree. For example, `PlayControllerContext` will expose the state and functions from the `usePlayController` hook to any child component that needs it, such as the `GameBoard` or `PlayControls`. |
104 | 109 |
|
105 | | -```tsx |
106 | | -import { render } from '@testing-library/react' |
107 | | -import Button from 'components/button' |
| 110 | +This architecture allows for a decoupled system where the UI (components) is a function of the state managed by the hooks, and the state is shared efficiently through contexts. For example, a page component under `src/pages` will initialize a controller hook. That hook's state is then provided to the component tree via a Context Provider. Child components can then consume that context to access state and dispatch actions without passing props down multiple levels. |
108 | 111 |
|
109 | | -describe('Button', () => { |
110 | | - it('renders', () => { |
111 | | - render(<Button />) |
112 | | - }) |
113 | | -}) |
114 | | -``` |
| 112 | +#### Learning Resources |
115 | 113 |
|
116 | | -This will ensure that future modifications by others won't severely break your component. Should you have a complex component that interacts with its props, you should add some more complex tests. |
| 114 | +To better understand the patterns used in this codebase, we recommend reviewing the official documentation for these core React and Next.js features: |
117 | 115 |
|
118 | | -### File Structure |
| 116 | +- [React Hooks](https://react.dev/reference/react/hooks) |
| 117 | +- [React Context](https://react.dev/reference/react/createContext) |
| 118 | +- [Next.js App Router](https://nextjs.org/docs) |
119 | 119 |
|
120 | | -Below is the an example of a recommended file structure: |
| 120 | +### Client-Side Chess Engines |
121 | 121 |
|
122 | | -``` |
123 | | -src/ |
124 | | - pages/ |
125 | | - components/ |
126 | | - ComponentName/ |
127 | | - ComponentName.tsx |
128 | | - index.tsx |
129 | | - styles.scss # Modular Styles if needed |
130 | | - index.tsx |
131 | | - styles/ |
132 | | - contexts/ |
133 | | - hooks/ |
134 | | - utils/ |
135 | | -``` |
| 122 | +A key feature of the platform is its ability to run both Stockfish and Maia directly in the user's browser. This is accomplished using WebAssembly and ONNX Runtime Web. |
136 | 123 |
|
137 | | -## Github Actions |
138 | | - |
139 | | -This project is has the following pre-configuration, which tests, lints and builds all branches and PRs. |
140 | | - |
141 | | -```yaml |
142 | | -name: CI |
143 | | - |
144 | | -on: |
145 | | - push: |
146 | | - pull_request: |
147 | | - branches: ['**'] |
148 | | - |
149 | | -jobs: |
150 | | - test: |
151 | | - runs-on: ubuntu-latest |
152 | | - steps: |
153 | | - - uses: actions/checkout@v2 |
154 | | - - uses: actions/setup-node@v2 |
155 | | - - run: yarn |
156 | | - - run: yarn test |
157 | | - |
158 | | - lint: |
159 | | - runs-on: ubuntu-latest |
160 | | - steps: |
161 | | - - uses: actions/checkout@v2 |
162 | | - - uses: actions/setup-node@v2 |
163 | | - - run: yarn |
164 | | - - run: yarn lint |
165 | | - |
166 | | - build: |
167 | | - runs-on: ubuntu-latest |
168 | | - strategy: |
169 | | - matrix: |
170 | | - node: ['12', '14', '16', '17'] |
171 | | - name: Node ${{ matrix.node }} Build |
172 | | - steps: |
173 | | - - uses: actions/checkout@v2 |
174 | | - - uses: actions/setup-node@v2 |
175 | | - with: |
176 | | - node-version: ${{ matrix.node }} |
177 | | - - run: yarn |
178 | | - - run: yarn build |
179 | | -``` |
| 124 | +- **Stockfish (`src/hooks/useStockfishEngine/`)**: We use a WebAssembly (WASM) version of Stockfish for standard chess analysis. The `useStockfishEngine` hook provides a simple interface to interact with the engine, allowing for move evaluation streams. This provides the "objective" best moves in any given position. |
180 | 125 |
|
181 | | -## Learn More |
| 126 | +- **Maia (`src/hooks/useMaiaEngine/`)**: The Maia engine is a neural network provided as an ONNX (Open Neural Network Exchange) model. We use the `onnxruntime-web` library to load and run Maia models on the client-side. The `useMaiaEngine` hook manages the download, initialization, and execution of the various Maia models (e.g., `maia_kdd_1100` to `maia_kdd_1900`). This engine provides the "human-like" move predictions that are central to the platform's mission. |
182 | 127 |
|
183 | | -To learn more about Next.js, take a look at the following resources: |
| 128 | +These hooks are consumed by higher-level controller hooks (like `useAnalysisController`) to provide the dual-engine analysis that powers many of the platform's features. |
184 | 129 |
|
185 | | -- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. |
186 | | -- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. |
| 130 | +## Deployment |
187 | 131 |
|
188 | | -You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! |
| 132 | +The Maia Chess Platform is deployed on [Vercel](https://vercel.com/). The `main` branch is automatically built and deployed to the production URL. Pull requests also generate unique preview deployments, allowing for easy testing and review before merging. |
0 commit comments