File size: 18,145 Bytes
cc633c7
 
 
 
 
 
 
 
 
 
b563b2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
765f0e5
 
 
b563b2f
 
 
765f0e5
b563b2f
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
---
title: PersonaFlow
emoji: πŸ€–
colorFrom: indigo
colorTo: purple
sdk: docker
app_port: 7860
pinned: false
---

# PersonaFlow β€” Dynamic AI Conversation Agent Platform

> **AI Quick-Reference README** β€” This file exists so an AI assistant can rapidly understand the full project without scanning every file.

---

## πŸ—‚οΈ Project Overview

**PersonaFlow** is a full-stack web application that lets users:
- Create custom AI conversation agents with unique names, roles, personalities, and rules.
- Chat (text) or Video Call (3D avatar with lip-sync) with their agents.
- Share agents publicly via tokenized share links.
- Generate performance reports with scoring, summaries, feedback, and transcripts.
- Chat with an AI assistant about each agent's report data (Report Chat).
- Practice public speaking via an "Anchoring" agent that reads teleprompter-style scripts.

---

## πŸ› οΈ Tech Stack

| Layer      | Technology                                              |
|------------|---------------------------------------------------------|
| Backend    | Python Β· Flask 3.0 Β· Flask-SQLAlchemy Β· Flask-CORS     |
| Database   | SQLite (`backend/instance/personaflow.db`)             |
| AI Engine  | Google Gemini API (default: `gemini-2.5-flash`) |
| TTS        | Kokoro TTS (local neural TTS, no API key required)     |
| STT        | OpenAI Whisper (speech recognition via Google SR API)  |
| Frontend   | Vanilla HTML Β· CSS Β· JavaScript (SPA via hash router)  |
| 3D Avatar  | Three.js (GLTF/FBX models + custom lip-sync engine)    |
| Fonts/Icons| Google Fonts (Inter) Β· Font Awesome 6.4                |

---

## πŸ“ Project Structure

```
personaflow/
β”œβ”€β”€ requirements.txt          # Python deps
β”œβ”€β”€ README.md                 # This file
β”‚
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ app.py                # Main Flask app β€” all API routes
β”‚   β”œβ”€β”€ models.py             # SQLAlchemy DB models
β”‚   β”œβ”€β”€ ai_service.py         # AI API calls (Gemini format) β€” delegates prompt building to agent_behavior.py
β”‚   β”œβ”€β”€ agent_behavior.py     # Agent Behavior Engine β€” role detection, scope enforcement, prompt builder, response-length rules
β”‚   β”œβ”€β”€ scoring_service.py    # Conversation scoring/evaluation logic
β”‚   β”œβ”€β”€ config.py             # App config (secret key, DB URI, API keys)
β”‚   └── migrate_anchoring.py  # Migration script for anchoring columns
β”‚
└── frontend/
    β”œβ”€β”€ index.html            # SPA shell β€” sidebar, modal, toast containers
    β”œβ”€β”€ css/
    β”‚   └── styles.css        # Single large stylesheet (~90KB), dark mode via [data-theme=dark]
    β”œβ”€β”€ image/
    β”‚   └── favicon.png       # App logo / favicon
    β”œβ”€β”€ 3d-assets/            # GLTF/FBX avatar model files
    └── js/
        β”œβ”€β”€ app.js            # Global App object, auth state, dark mode toggle
        β”œβ”€β”€ api.js            # API wrapper (all fetch calls)
        β”œβ”€β”€ router.js         # Hash-based SPA router (#/page routing)
        β”œβ”€β”€ components/
        β”‚   β”œβ”€β”€ sidebar.js        # Sidebar nav + user info + logout
        β”‚   β”œβ”€β”€ modal.js          # Global modal helper
        β”‚   β”œβ”€β”€ toast.js          # Toast notification helper
        β”‚   β”œβ”€β”€ notifications.js  # Notification panel (positioned correctly above sidebar)
        β”‚   β”œβ”€β”€ onboarding.js     # First-time user onboarding flow
        β”‚   β”œβ”€β”€ avatar3d.js       # Three.js 3D avatar renderer + animation (54KB)
        β”‚   β”œβ”€β”€ lipsync-engine.js # Phoneme-based lip-sync engine for avatar
        β”‚   └── three-converter.js # Three.js GLB/FBX loader helper
        └── pages/
            β”œβ”€β”€ login.js          # Login page
            β”œβ”€β”€ signup.js         # Signup page
            β”œβ”€β”€ dashboard.js      # Dashboard β€” stats, recent conversations
            β”œβ”€β”€ agents.js         # Agent list page β€” CRUD, filter, search
            β”œβ”€β”€ agent-wizard.js   # 4-step agent creation wizard
            β”œβ”€β”€ agent-detail.js   # Agent detail β€” tabs: Overview, Conversations, Reports, Share, Anchoring, Report Chat
            β”œβ”€β”€ chat.js           # Text chat interface with agent
            β”œβ”€β”€ video-call.js     # 3D Avatar video call (51KB) β€” TTS, STT, lip-sync
            β”œβ”€β”€ conversations.js  # All conversations list page
            β”œβ”€β”€ reports.js        # Reports list page (history of all report cards)
            β”œβ”€β”€ share-access.js   # Public share link landing page (no auth required)
            β”œβ”€β”€ anchoring.js      # Anchoring teleprompter β€” owner/local view
            β”œβ”€β”€ anchoring-remote.js # Anchoring remote control view
            └── templates.js      # Agent template data (now used inline in agent wizard)
```

---

## πŸ—ƒοΈ Database Models (backend/models.py)

| Model          | Key Fields                                                                                 |
|----------------|-------------------------------------------------------------------------------------------|
| `User`         | id, name, email, password_hash, avatar_color                                              |
| `Agent`        | id, user_id, name, role, goal, opening_message, task_description, rules (JSON), tone, knowledge, output_config (JSON), icon, color, status, agent_type (`conversation`/`anchoring`), is_default, script_content |
| `Conversation` | id, agent_id, user_id, share_link_id, participant_name/email, mode (`text`/`video`), status (`active`/`completed`), started_at, ended_at |
| `Message`      | id, conversation_id, role (`user`/`agent`), content, timestamp                           |
| `ShareLink`    | id, agent_id, token, password_hash, expires_at, max_uses, current_uses, require_name/email |
| `Report`       | id, conversation_id, overall_score, criteria_scores (JSON), summary, feedback, recommendations, transcript (JSON) |

---

## πŸ”Œ Backend API Routes (backend/app.py)

### Auth
| Method | Endpoint              | Description          |
|--------|-----------------------|----------------------|
| POST   | `/api/auth/signup`    | Register new user    |
| POST   | `/api/auth/login`     | Login                |
| POST   | `/api/auth/logout`    | Logout               |
| GET    | `/api/auth/me`        | Current user info    |

### Agents
| Method | Endpoint                          | Description                        |
|--------|-----------------------------------|------------------------------------|
| GET    | `/api/agents`                     | List user's agents                 |
| POST   | `/api/agents`                     | Create agent                       |
| GET    | `/api/agents/<id>`                | Get agent                          |
| PUT    | `/api/agents/<id>`                | Update agent                       |
| DELETE | `/api/agents/<id>`                | Delete agent                       |
| GET    | `/api/agents/<id>/share-links`    | List share links                   |
| POST   | `/api/agents/<id>/share-links`    | Create share link                  |
| POST   | `/api/agents/<id>/report-chat`    | AI chat about agent's report data  |
| POST   | `/api/agents/<id>/export-report`  | Export report as DOCX              |

### Conversations
| Method | Endpoint                                    | Description              |
|--------|---------------------------------------------|--------------------------|
| GET    | `/api/conversations`                        | List conversations       |
| POST   | `/api/conversations`                        | Create conversation      |
| GET    | `/api/conversations/<id>`                   | Get conversation + msgs  |
| DELETE | `/api/conversations/<id>`                   | Delete conversation      |
| POST   | `/api/conversations/<id>/messages`          | Send a message           |
| POST   | `/api/conversations/<id>/end`               | End conversation + generate report |

### Reports
| Method | Endpoint                              | Description                    |
|--------|---------------------------------------|--------------------------------|
| GET    | `/api/reports`                        | List all reports               |
| GET    | `/api/reports/<id>`                   | Get single report              |
| POST   | `/api/reports/generate/<conv_id>`     | Generate report for conv       |

### Public Share
| Method | Endpoint                        | Description                       |
|--------|---------------------------------|-----------------------------------|
| GET    | `/api/share/<token>`            | Get share link info               |
| POST   | `/api/share/<token>/verify`     | Verify share link password        |
| POST   | `/api/share/<token>/start`      | Start public share conversation   |
| POST   | `/api/share/<token>/message`    | Send message in public share conv |

### Anchoring
| Method | Endpoint                                        | Description                    |
|--------|-------------------------------------------------|--------------------------------|
| POST   | `/api/anchoring/<agent_id>/start`               | Start anchoring session        |
| POST   | `/api/anchoring/<agent_id>/control`             | Pause / resume / stop          |
| GET    | `/api/anchoring/<agent_id>/status`              | Get current line / status      |
| POST   | `/api/anchoring/tts`                            | TTS for a line of the script   |

### Other
| Method | Endpoint               | Description         |
|--------|------------------------|---------------------|
| POST   | `/api/upload`          | Upload image file   |
| POST   | `/api/speech-to-text`  | STT (Whisper)       |
| POST   | `/api/tts`             | Edge TTS            |

---

## πŸ–₯️ Frontend Pages (SPA Routes)

| Hash Route           | JS File              | Description                                         |
|----------------------|----------------------|-----------------------------------------------------|
| `#/login`            | login.js             | Login form                                          |
| `#/signup`           | signup.js            | Signup form                                         |
| `#/dashboard`        | dashboard.js         | Summary stats, recent conversations                 |
| `#/agents`           | agents.js            | Agent list with search, filter, create              |
| `#/agents/new`       | agent-wizard.js      | 4-step creation wizard                              |
| `#/agents/:id`       | agent-detail.js      | Tabbed detail: Overview / Conversations / Reports / Share / Anchoring / Report Chat |
| `#/chat/:convId`     | chat.js              | Text chat UI                                        |
| `#/video-call/:convId` | video-call.js      | 3D avatar video call                                |
| `#/conversations`    | conversations.js     | All conversations history                           |
| `#/reports`          | reports.js           | All reports history (card list)                     |
| `#/share/:token`     | share-access.js      | Public shareable agent chat (no auth needed)        |
| `#/anchoring/:id`    | anchoring.js         | Local anchoring teleprompter                        |
| `#/anchoring-remote/:id` | anchoring-remote.js | Remote anchoring control                        |

---

## ▢️ How to Run

```bash
# 1. Install Python dependencies
pip install -r requirements.txt

# 2. Configure your API key
# Set the GEMINI_API_KEY environment variable (or edit backend/config.py)
export GEMINI_API_KEY="your-api-key-here"

# 3. Start the backend server
cd backend
python app.py
# Server starts at http://localhost:5000

# 4. Open browser
#    Navigate to: http://localhost:5000
```

---

## βš™οΈ Configuration (backend/config.py)

| Setting                 | Default                            | Notes                              |
|-------------------------|------------------------------------|------------------------------------|
| `SECRET_KEY`            | `personaflow-secret-key-...`       | Change in production               |
| `SQLALCHEMY_DATABASE_URI` | `sqlite:///personaflow.db`       | DB stored in `backend/instance/`   |
| `GEMINI_API_KEY`        | `AIzaSyBiL-wOiS8Rj5h3YuC...`       | Your Gemini API Key                |
| `GEMINI_MODEL`          | `gemini-2.5-flash`                 | LLM model name                     |
| `KOKORO_DEFAULT_VOICE`  | `af_heart`                         | Default Kokoro TTS voice           |
| `PERMANENT_SESSION_LIFETIME` | `86400` (24 hours)            | Session expiry                     |

---

## πŸ”‘ Key Architecture Decisions

- **SPA with hash routing**: `router.js` intercepts all `hashchange` events and renders the correct page module. No page reloads.
- **Auth**: Session-based (Flask sessions). Protected routes use `@login_required` decorator. Frontend checks `/api/auth/me` on load.
- **AI calls**: All AI interactions go through `ai_service.py` β†’ Gemini REST API. System prompt building is handled by `agent_behavior.py`.
- **Agent Behavior Engine** (`agent_behavior.py`): Centralizes all prompt-building logic. Auto-detects role type (INTERVIEWER / TEACHER / COACH / EVALUATOR / DEFAULT) and injects role-specific behavioral instructions. Enforces strict scope via `output_config.strict_mode`. Applies response-length rules based on conversation `mode` β€” video calls get 1–2 sentence responses; text chat gets 3–4 sentences. Supports `output_config` behavior fields: `strict_mode`, `allow_out_of_scope`, `max_answer_length`, `behavior`.
- **Agent types**: Two types β€” `conversation` (standard chat bot) and `anchoring` (teleprompter script reader). Anchoring agents have `script_content` and in-memory state tracking (`anchoring_states` dict in `app.py`).
- **Reports**: Auto-generated on conversation end if `output_config.evaluation` is enabled. Can also be manually generated via the API. Stored in `Report` model.
- **Report Chat**: `/api/agents/<id>/report-chat` feeds all the agent's conversations + reports as context to the LLM so users can ask analytical questions.
- **Video Call**: Uses Three.js for 3D avatar rendering. Text-to-speech via **Kokoro TTS** (local neural). Speech-to-text via Whisper + Google Web Speech API. Custom phoneme-based lip-sync engine (`lipsync-engine.js`). Kokoro pipeline is initialized as a singleton at first TTS call to avoid reload overhead.
- **Dark mode**: Stored in `localStorage` as `personaflow-theme`. Applied via `[data-theme=dark]` attribute on `<html>`. Applied immediately in a blocking `<script>` before body renders to prevent flash.
- **Share links**: Tokenized URLs (`/api/share/<token>`). Support password protection, expiry, max uses, and required name/email fields.
- **Notifications panel**: Rendered as a fixed overlay, positioned to appear above the sidebar (z-index managed carefully).

---

## πŸ› Known Issues / Recent Work History

- **Report Chat** was moved from Reports page β†’ Agent Detail page (agent-specific context). The `agent-detail.js` "Report Chat" tab contains the chat UI.
- **Reports page** (`reports.js`) shows only the report history list (no embedded chat).
- **Agent Creation Flow**: The `agent-wizard.js` now features a "choice screen" before the wizard starts, offering "Build from Scratch" (with inspiration idea cards) or "Use a Template" (rendering the template gallery inline).
- **Templates**: The dedicated `#/templates` page was removed from the sidebar. Templates are exclusively accessed via the new Create Agent choice screen.
- **TTS (Avatar Voice)**: Migrated from `edge-tts` (Microsoft) to **Kokoro TTS** (local neural TTS). The `/api/avatar/talk` endpoint now uses `KPipeline` from the `kokoro` Python package. Outputs `.wav` audio. On first run, Kokoro auto-downloads its model weights (~327MB). No API key required.
  - **Note on Windows Installation**: Kokoro has a strict dependency on `numpy==1.26.4` which fails on Python 3.13. It can be installed by bypassing dependencies (`pip install kokoro --no-deps`) and installing `numpy`, `torch`, `misaki`, etc., manually.
  - **Note on Misaki Compatibility**: The G2P library `misaki` (version 0.7.4) crashes with a `TypeError` when encountering unknown tokens (`t.phonemes is None`). This was manually patched in `misaki/en.py` to fallback to an unknown token instead of crashing.
- **Agent Behavior Engine**: New `backend/agent_behavior.py` centralizes all system-prompt building. Extracted from `ai_service.py`. Role-specific prompt sections (Interviewer, Teacher, Coach, Evaluator, Default). `output_config` JSON now supports `strict_mode`, `allow_out_of_scope`, `max_answer_length`, and `behavior` fields.
- **Response Length (video vs text)**: `ai_service.generate_response()` now accepts a `mode` parameter. Video calls pass `mode='video'` (from `conversation.mode`) β†’ agent replies in 1–2 sentences for natural interrupt-handling. Text chat uses `mode='text'` β†’ 3–4 sentences max.
- **3D Avatar lip-sync**: Complex phoneme mapping via `lipsync-engine.js`. Debugging history in conversation logs.
- **Microphone Mute/Unmute**: Fixed a bug where toggling the microphone off and on would leave the SpeechRecognition engine in a halted state. The `startListening()` method now properly handles re-initializing the Web Speech API `SpeechRecognition` object if it fails to start.
- **Notifications panel**: Was clipping behind sidebar β€” fixed with proper z-index and positioning.
- **Anchoring**: In-memory state per `agent_id` (`anchoring_states` dict in `app.py`). Script read line-by-line via `/api/anchoring/<agent_id>/status` polling.
- **Share Link UI**: Redesigned the "Create Share Link" modal to improve UX, grouping password, expiry, and max uses into a cleaner layout, and converting to a toggle switch for the password field.
- **Code QA**: Reviewed backend API routes and fixed minor transcript generation logic consistency in manual report generation.