|
| 1 | +# Reflection Widget - Developer Guide |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `ReflectionMatrix` class provides a chat-based interface for users to interact with AI mentors for reflecting on their music blocks project. It manages chat history, mentor selection, message exchange with a backend server, and report generation. |
| 6 | + |
| 7 | +Backend Code for the widget : [musicblocks_reflection_fastapi](https://github.com/Commanderk3/musicblocks_reflection_fastapi) |
| 8 | + |
| 9 | +## Key Features |
| 10 | + |
| 11 | +- **Multi-mentor Chat:** Switch between AI mentors (Meta/Rohan, Music/Beethoven, Code/Steve). |
| 12 | +- **Chat History:** Stores and renders conversation history. |
| 13 | +- **Typing Indicator:** Shows animated "Thinking..." while awaiting responses. |
| 14 | +- **Markdown Rendering:** Converts Markdown responses to HTML for display. |
| 15 | +- **Analysis & Summary:** Fetches project analysis after sufficient chat history. |
| 16 | +- **Local Storage:** Saves and retrieves analysis reports. |
| 17 | +- **Export:** Download chat transcript as a text file. |
| 18 | + |
| 19 | +## Components |
| 20 | + |
| 21 | +- **Widget Interface**: `js/widgets/reflection.js` |
| 22 | +- **Block Definition**: `js/blocks/WidgetBlocks.js` |
| 23 | +- **Registration**: `js/activity.js` (line 176) and `js/logo.js` (line 113) |
| 24 | +- **CSS styling**: `css/activities.css` (line 2023 - 2072) |
| 25 | + |
| 26 | +## Methods |
| 27 | + |
| 28 | +- **Widget Window Initialisation** |
| 29 | + |
| 30 | + `init(activity)` : Triggers when the reflection block is clicked. It renders the widget and initializes the required data structures. |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +- **Mentor Switching** |
| 35 | + |
| 36 | + `changeMentor("code")` : Changes the active mentor and updates button highlights. |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +- Sending Messages |
| 41 | + |
| 42 | + `sendMessage()` : Pushes user message to history, displays it, and requests bot reply. |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +- Bot Reply Handling |
| 47 | + |
| 48 | + `botReplyDiv(message, user_query, md)` : |
| 49 | + |
| 50 | + - If `user_query` is true, sends message to backend and displays reply. |
| 51 | + - If `md` is true, renders reply as Markdown. |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +- Analysis & Summary |
| 56 | + |
| 57 | + `getAnalysis()` : |
| 58 | + |
| 59 | + - Requests project analysis from backend if chat history is sufficient. |
| 60 | + - Saves analysis to localStorage. |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +- Local Storage |
| 65 | + |
| 66 | + - **Save:** `saveReport(data)` stores analysis. |
| 67 | + - **Read:** `readReport()` retrieves analysis. |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +- Export Conversation |
| 72 | + |
| 73 | + `downloadAsTxt(conversationData)` : Downloads chat history as a `.txt` file. |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +- Markdown Rendering |
| 78 | + |
| 79 | + `mdToHTML(md)` : Converts Markdown to HTML for display in chat. |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## API Endpoints |
| 84 | + |
| 85 | +### 1. `/projectcode` |
| 86 | + |
| 87 | +- **Method:** `POST` |
| 88 | +- **Purpose:** Sends the exported project code to the backend to receive an algorithmic summary. |
| 89 | +- **Payload:** |
| 90 | + ```json |
| 91 | + { |
| 92 | + "code": "<project code string>" |
| 93 | + } |
| 94 | + ``` |
| 95 | +- **Response:** |
| 96 | + ```json |
| 97 | + { |
| 98 | + "algorithm": "<algorithm string>", |
| 99 | + "message": "<first message>" |
| 100 | + } |
| 101 | + ``` |
| 102 | + or |
| 103 | + ```json |
| 104 | + { |
| 105 | + "error": "<error message>" |
| 106 | + } |
| 107 | + ``` |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +### 2. `/chat` |
| 112 | + |
| 113 | +- **Method:** `POST` |
| 114 | +- **Purpose:** Sends a user query, chat history, mentor selection, and project algorithm to the backend to get an AI mentor's reply. |
| 115 | +- **Payload:** |
| 116 | + ```json |
| 117 | + { |
| 118 | + "query": "<user message>", |
| 119 | + "messages": [ { "role": "...", "content": "..." }, ... ], |
| 120 | + "mentor": "<mentor key>", |
| 121 | + "algorithm": "<algorithm string>" |
| 122 | + } |
| 123 | + ``` |
| 124 | +- **Response:** |
| 125 | + `{ response: "<bot reply>" }` |
| 126 | + or |
| 127 | + `{ error: "<error message>" }` |
| 128 | + |
| 129 | +- **Example `messages` structure** |
| 130 | + ```json |
| 131 | + [ |
| 132 | + { |
| 133 | + "role": "meta", |
| 134 | + "content": "hello" |
| 135 | + }, |
| 136 | + { |
| 137 | + "role": "user", |
| 138 | + "content": "hi" |
| 139 | + }, |
| 140 | + { |
| 141 | + "role": "meta", |
| 142 | + "content": "Hey there! I'm Rohan, the meta-mentor here. Tell me, what did you create in your project?" |
| 143 | + } |
| 144 | + ] |
| 145 | + ``` |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +### 3. `/analysis` |
| 150 | + |
| 151 | +- **Method:** `POST` |
| 152 | +- **Purpose:** Sends the chat history and summary to the backend to receive a project analysis report. |
| 153 | +- **Payload:** |
| 154 | + ```json |
| 155 | + { |
| 156 | + "messages": [ { "role": "...", "content": "..." }, ... ], |
| 157 | + "summary": "<summary string>" |
| 158 | + } |
| 159 | + ``` |
| 160 | +- **Response:** |
| 161 | + `{ response: "<analysis report>" }` |
| 162 | + or |
| 163 | + `{ error: "<error message>" }` |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +**All endpoints expect and return JSON.** |
| 168 | +**The backend is expected to run locally while development.** |
| 169 | + |
| 170 | +--- |
| 171 | + |
| 172 | +### API testing |
| 173 | + |
| 174 | +```bash |
| 175 | +curl -X POST http://localhost:PORT/projectcode \ |
| 176 | + -H "Content-Type: application/json" \ |
| 177 | + -d '{ |
| 178 | + "code": "MUSICBLOCKS_PROJECT_CODE_HERE" |
| 179 | + }' |
| 180 | +``` |
| 181 | + |
| 182 | +**Note** : Save your Music Blocks project as an HTML file. Open the file to locate the embedded JSON code, and make sure to stringify the JSON before using it for testing. |
| 183 | + |
| 184 | +Written by : [Diwangshu Kakoty](https://github.com/Commanderk3) |
0 commit comments