--- 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/` | Get agent | | PUT | `/api/agents/` | Update agent | | DELETE | `/api/agents/` | Delete agent | | GET | `/api/agents//share-links` | List share links | | POST | `/api/agents//share-links` | Create share link | | POST | `/api/agents//report-chat` | AI chat about agent's report data | | POST | `/api/agents//export-report` | Export report as DOCX | ### Conversations | Method | Endpoint | Description | |--------|---------------------------------------------|--------------------------| | GET | `/api/conversations` | List conversations | | POST | `/api/conversations` | Create conversation | | GET | `/api/conversations/` | Get conversation + msgs | | DELETE | `/api/conversations/` | Delete conversation | | POST | `/api/conversations//messages` | Send a message | | POST | `/api/conversations//end` | End conversation + generate report | ### Reports | Method | Endpoint | Description | |--------|---------------------------------------|--------------------------------| | GET | `/api/reports` | List all reports | | GET | `/api/reports/` | Get single report | | POST | `/api/reports/generate/` | Generate report for conv | ### Public Share | Method | Endpoint | Description | |--------|---------------------------------|-----------------------------------| | GET | `/api/share/` | Get share link info | | POST | `/api/share//verify` | Verify share link password | | POST | `/api/share//start` | Start public share conversation | | POST | `/api/share//message` | Send message in public share conv | ### Anchoring | Method | Endpoint | Description | |--------|-------------------------------------------------|--------------------------------| | POST | `/api/anchoring//start` | Start anchoring session | | POST | `/api/anchoring//control` | Pause / resume / stop | | GET | `/api/anchoring//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//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 ``. Applied immediately in a blocking `