Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions CONVERSATION_HISTORY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
# Conversation History Implementation

This document describes the conversation history feature that has been added to the application.

## Overview

The application now supports persistent conversation history using SQLite database. Users can:
- Save conversations automatically
- View all previous conversations
- Resume any previous conversation
- Delete conversations
- Start new conversations

## Backend Changes

### 1. Database Layer (`backend/src/agent/database.py`)

Created a `ConversationDatabase` class that manages:
- **Conversations table**: Stores conversation metadata (id, title, timestamps)
- **Messages table**: Stores individual messages with role (human/ai) and content
- SQLite database with proper indexing for performance

Key methods:
- `create_conversation()`: Create a new conversation
- `get_conversation()`: Get conversation by ID
- `get_all_conversations()`: List all conversations
- `add_message()`: Add a message to a conversation
- `get_messages()`: Get all messages for a conversation
- `delete_conversation()`: Delete a conversation
- `update_conversation_title()`: Update conversation title

### 2. API Endpoints (`backend/src/agent/app.py`)

Added REST API endpoints:

```
GET /api/conversations - List all conversations
GET /api/conversation/{id} - Get specific conversation
POST /api/conversation - Create new conversation
POST /api/conversation/{id}/message - Add message to conversation
GET /api/conversation/{id}/messages - Get all messages
DELETE /api/conversation/{id} - Delete conversation
PATCH /api/conversation/{id}/title - Update title
```

### 3. State Management (`backend/src/agent/state.py`)

Added `conversation_id` field to `OverallState` to track which conversation the agent is processing.

### 4. Utilities (`backend/src/agent/utils.py`)

Added `load_conversation_history()` function to load previous messages from database and convert them to LangChain message format.

## Frontend Changes

### 1. ConversationHistory Component (`frontend/src/components/ConversationHistory.tsx`)

A modal component that displays:
- List of all conversations with titles and timestamps
- Message count for each conversation
- Delete button for each conversation
- Click to resume any conversation
- Beautiful space-themed UI matching the app design

Features:
- Formatted timestamps (Today, Yesterday, date)
- Current conversation highlighting
- Confirmation before deletion
- Loading and error states

### 2. App.tsx Updates

Integrated conversation history with:
- **History Button**: Fixed position button to open history modal
- **New Chat Button**: Start a fresh conversation
- **Auto-save**: Messages are automatically saved to database
- **Resume**: Load previous conversations with all messages
- **State Management**: Track current conversation ID

Key functions added:
- `createNewConversation()`: Create a new conversation when user starts chatting
- `saveMessage()`: Save each message (human and AI) to database
- `loadConversation()`: Load a previous conversation and restore state

## Usage

### Starting a New Conversation

1. Open the app (welcome screen)
2. Type a message and submit
3. A new conversation is automatically created
4. All messages are saved as they arrive

### Viewing History

1. Click the "History" button (top-left)
2. See list of all previous conversations
3. Click any conversation to resume it
4. Delete unwanted conversations with the trash icon

### Resuming a Conversation

1. Open History
2. Click on a conversation
3. All previous messages load
4. Continue the conversation from where you left off

### Starting Fresh

Click "New Chat" button to start a new conversation while keeping the previous one saved.

## Database Schema

### Conversations Table
```sql
CREATE TABLE conversations (
id TEXT PRIMARY KEY,
title TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
metadata TEXT
)
```

### Messages Table
```sql
CREATE TABLE messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
conversation_id TEXT,
role TEXT,
content TEXT,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
metadata TEXT,
FOREIGN KEY (conversation_id) REFERENCES conversations(id) ON DELETE CASCADE
)
```

## File Structure

```
backend/src/agent/
├── database.py # New: SQLite database manager
├── app.py # Updated: Added API endpoints
├── state.py # Updated: Added conversation_id field
└── utils.py # Updated: Added load_conversation_history()

frontend/src/
├── components/
│ ├── ConversationHistory.tsx # New: History modal component
│ └── ...
└── App.tsx # Updated: Integrated conversation history
```

## Future Enhancements

Potential improvements:
1. Search conversations by content
2. Rename conversations with custom titles
3. Export conversations
4. Share conversations
5. Conversation folders/tags
6. Automatic title generation from first message
7. Conversation analytics

## Notes

- Database file is created at `backend/conversations.db`
- Messages are saved immediately after being sent/received
- Deleting a conversation cascades to delete all its messages
- The database is persistent across app restarts
121 changes: 121 additions & 0 deletions GETTING_STARTED.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Starting the Application with Conversation History

## Quick Start

### 1. Start the Backend Server

Open a terminal in the `backend` directory and run:

```bash
cd backend
langgraph dev
```

This will start the LangGraph server on `http://127.0.0.1:2024` with the new conversation history API endpoints.

**Note:** Make sure you have:
- Installed backend dependencies: `pip install .`
- Set up your `.env` file with `GEMINI_API_KEY`

### 2. Start the Frontend Development Server

Open another terminal in the `frontend` directory and run:

```bash
cd frontend
npm run dev
```

This will start the Vite dev server on `http://localhost:5173/app/`

The frontend is now configured to proxy API requests to the backend server on port 2024.

### 3. Access the Application

Open your browser and navigate to: `http://localhost:5173/app/`

## Features You Can Now Use

### History Button
- Click the "History" button (top-left) to view all your saved conversations
- Each conversation shows:
- Title
- Timestamp (formatted as "Today", "Yesterday", or date)
- Message count
- Current conversation highlighted

### Starting a Conversation
- Type your question in the welcome screen
- A new conversation is automatically created
- All messages are saved to the SQLite database (`backend/conversations.db`)

### Resuming a Conversation
1. Click "History" button
2. Click on any conversation from the list
3. All previous messages load automatically
4. Continue the conversation where you left off

### Starting a New Chat
- Click "New Chat" button (appears when you're in a conversation)
- Starts a fresh conversation
- Previous conversation remains saved

### Deleting Conversations
- Hover over any conversation in the history
- Click the trash icon that appears
- Confirm deletion

## Troubleshooting

### "Failed to create conversation" Error

This error means the frontend can't reach the backend API. Check:

1. **Backend is running**: Make sure `langgraph dev` is running in the backend directory
2. **Correct port**: Backend should be on port 2024 (check terminal output)
3. **Vite proxy**: The `frontend/vite.config.ts` should proxy `/api` to `http://127.0.0.1:2024`

### WebSocket Connection Errors

These are Vite HMR warnings and won't affect the application functionality. They occur because the app is served under `/app/` path.

### Database Errors

If you see SQLite errors:
1. Make sure the `backend` directory is writable
2. The database file `conversations.db` will be created automatically
3. Delete `conversations.db` to reset all conversations

## Database Location

Conversations are stored in: `backend/conversations.db`

You can:
- Inspect the database with any SQLite browser
- Delete it to start fresh
- Back it up to preserve conversations

## API Endpoints

The backend now serves these additional endpoints:

```
GET /api/conversations - List all conversations
GET /api/conversation/{id} - Get specific conversation
POST /api/conversation - Create new conversation
POST /api/conversation/{id}/message - Add message to conversation
GET /api/conversation/{id}/messages - Get all messages
DELETE /api/conversation/{id} - Delete conversation
PATCH /api/conversation/{id}/title - Update title
```

## Testing the Database

Run the test script to verify the database is working:

```bash
cd backend
python test_database.py
```

You should see: `🎉 All tests passed!`
Binary file added backend/conversations.db
Binary file not shown.
Loading