[Redesign] Backend - Conversation-based Chat System #59
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Detailed Implementation Guide for Backend Chat System
Summary
Redesign backend to support conversation-based chat system with anonymous and authenticated users.
Current State (Before)
Target State (After)
Implementation Phases
Phase 1: Database Models
1.1 Conversation Model
Create
src/backend/app/db/models/conversation.py:Check: Verify UUID import works, no circular imports with User/Bot models
1.2 Update Message Model
Add to existing
Messagemodel:Check: Run alembic migration to create new tables
1.3 Anonymous Tracking Model
Create
src/backend/app/db/models/anonymous_user.py:Migration needed:
alembic revision --autogenerate -m "add conversation and anonymous user tables"Phase 2: API Endpoints
2.1 Conversation Endpoints
Create
src/backend/app/api/conversations.py:2.2 Bot Management Tools (for Assistant)
Add to
src/backend/app/services/ai_agent/agent.py:Phase 3: Rate Limiting & Anonymous Limits
3.1 Rate Limiting Service
Create
src/backend/app/services/rate_limiter.py:3.2 Anonymous Token Management
In
src/backend/app/api/conversations.py:3.3 Anonymous Chat Flow
Phase 4: Integration with Existing Code
4.1 Update Chat Endpoint
Update existing
/api/chator create new flow that uses Conversation model.4.2 Update ConversationalAgent
Make sure agent can access conversation context:
4.3 Bot Tools Registration
Register the new tools with the agent:
What to Check / Verify
Database
alembic upgrade headpython -c "from app.db.models import Conversation, Message, AnonymousUser"API Endpoints
Rate Limiting
Anonymous User Limits
How to Test
Unit Tests
Integration Tests
How to Debug
If Conversation Not Found
If Rate Limit Not Working
If Anonymous Limits Not Applied
Invariants (What NOT to Change)
Files to Create/Modify
db/models/conversation.pydb/models/anonymous_user.pyservices/rate_limiter.pyapi/conversations.pydb/models/message.py(add conversation_id)db/models/__init__.py(export new models)main.py(register router)services/ai_agent/tools.py(add bot tools)Migration Required
Dependencies
Priority: HIGH
Acceptance Criteria
Notes for Developer
⚠️ Prerequisite
This issue assumes issue #63 (Refactor) is COMPLETED.
The code examples reference the refactored structure:
services/ai_agent/agent.py(not conversational.py)services/ai_agent/tools.py(tool registry)services/ai_agent/help.py(help formatters)services/ai_agent/client.py(MiniMax client)If #63 is not done yet, complete it first.