File size: 7,233 Bytes
dc3879e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# AI Chatbot Integration Guide

[From]: Phase III Integration Setup

This guide explains how to integrate and test the AI chatbot feature.

## Prerequisites

1. **Python 3.13+** installed
2. **UV** package manager installed
3. **Gemini API key** from [Google AI Studio](https://aistudio.google.com)
4. **PostgreSQL database** (Neon or local)

## Setup Steps

### 1. Backend Configuration

#### Environment Variables

Add to your `backend/.env` file:

```bash
# Database
DATABASE_URL=postgresql://user:password@host/database

# Gemini API (Required for AI chatbot)
GEMINI_API_KEY=your-gemini-api-key-here
GEMINI_MODEL=gemini-2.0-flash-exp

# JWT
JWT_SECRET=your-jwt-secret-here
JWT_ALGORITHM=HS256

# CORS
FRONTEND_URL=http://localhost:3000

# Environment
ENVIRONMENT=development
```

#### Get Gemini API Key

1. Go to [Google AI Studio](https://aistudio.google.com)
2. Sign in with your Google account
3. Click "Get API Key"
4. Copy the API key
5. Add it to your `.env` file as `GEMINI_API_KEY`

**Note**: Gemini API has a free tier that's sufficient for development and testing.

### 2. Database Migration

The chatbot requires two additional tables: `conversation` and `message`.

Run the migration:

```bash
cd backend
python migrations/run_migration.py
```

Expected output:
```
✅ 2/2 migrations completed successfully
🎉 All migrations completed!
```

### 3. Install Dependencies

```bash
cd backend
uv sync
```

This installs:
- `openai>=1.0.0` - OpenAI SDK (for AsyncOpenAI adapter)
- `agents` - OpenAI Agents SDK
- All other dependencies

### 4. Validate Integration

Run the integration validation script:

```bash
cd backend
python scripts/validate_chat_integration.py
```

This checks:
- ✅ Dependencies installed
- ✅ Environment variables configured
- ✅ Database tables exist
- ✅ MCP tools registered
- ✅ AI agent initialized
- ✅ Chat API routes registered

### 5. Start the Backend Server

```bash
cd backend
uv run python main.py
```

Expected output:
```
INFO:     Started server process
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8000
```

### 6. Test the Chat API

#### Option A: Interactive API Docs

Open browser: `http://localhost:8000/docs`

Find the `POST /api/{user_id}/chat` endpoint and test it:

**Request:**
```json
{
  "message": "Create a task to buy groceries"
}
```

**Expected Response:**
```json
{
  "response": "I'll create a task titled 'Buy groceries' for you.",
  "conversation_id": "uuid-here",
  "tasks": []
}
```

#### Option B: cURL

```bash
curl -X POST "http://localhost:8000/api/{user_id}/chat" \
  -H "Content-Type: application/json" \
  -d '{
    "message": "Create a task to buy groceries"
  }'
```

Replace `{user_id}` with a valid user UUID.

#### Option C: Python Test Script

```python
import requests
import uuid

# Replace with actual user ID from your database
user_id = "your-user-uuid-here"

response = requests.post(
    f"http://localhost:8000/api/{user_id}/chat",
    json={"message": "Create a task to buy groceries"}
)

print(response.json())
```

### 7. Frontend Integration (Optional)

If you have the frontend running:

1. Start the frontend:
   ```bash
   cd frontend
   pnpm dev
   ```

2. Open browser: `http://localhost:3000/chat`

3. Test the chat interface with messages like:
   - "Create a task to buy groceries"
   - "What are my tasks?"
   - "Show me my pending tasks"
   - "Create a high priority task to finish the report by Friday"

## API Endpoints

### Chat Endpoint

**POST** `/api/{user_id}/chat`

**Request Body:**
```json
{
  "message": "Create a task to buy groceries",
  "conversation_id": "optional-uuid-to-continue-conversation"
}
```

**Response:**
```json
{
  "response": "I'll create a task titled 'Buy groceries' for you.",
  "conversation_id": "uuid",
  "tasks": []
}
```

**Error Responses:**

- **400 Bad Request**: Invalid message (empty or >10,000 characters)
- **429 Too Many Requests**: Daily message limit exceeded (100/day)
- **503 Service Unavailable**: AI service not configured or unreachable
- **504 Gateway Timeout**: AI service timeout

## Troubleshooting

### "AI service not configured"

**Cause**: `GEMINI_API_KEY` not set in `.env`

**Fix**:
1. Get API key from https://aistudio.google.com
2. Add to `.env`: `GEMINI_API_KEY=your-key-here`
3. Restart server

### "Database error: relation 'conversation' does not exist"

**Cause**: Migration not run

**Fix**:
```bash
cd backend
python migrations/run_migration.py
```

### "Daily message limit exceeded"

**Cause**: User has sent 100+ messages today

**Fix**: Wait until midnight UTC or use a different user ID for testing

### Import errors for `agents` or `openai`

**Cause**: Dependencies not installed

**Fix**:
```bash
cd backend
uv sync
```

## Testing Checklist

- [ ] Environment variables configured (especially `GEMINI_API_KEY`)
- [ ] Database migrations run successfully
- [ ] Validation script passes all checks
- [ ] Backend server starts without errors
- [ ] Can access API docs at http://localhost:8000/docs
- [ ] Can send message via `/api/{user_id}/chat` endpoint
- [ ] AI responds with task creation confirmation
- [ ] Can list tasks via chat
- [ ] Conversation persists across requests (using `conversation_id`)
- [ ] Frontend chat page works (if applicable)

## Rate Limiting

The chatbot enforces a limit of **100 messages per user per day** (NFR-011).

This includes both user and assistant messages in conversations.

The limit resets at midnight UTC.

## Architecture Overview

```
Frontend (React)

ChatInterface.tsx → POST /api/{user_id}/chat

Backend (FastAPI)

chat.py endpoint
    ├→ Rate limiting check (T021)
    ├→ Get/create conversation (T016)
    ├→ Persist user message (T017)
    ├→ Load conversation history (T016)
    ├→ Run AI agent (T014)
    │   ↓
    │   Agent → MCP Tools
    │       ├→ add_task (T013)
    │       └→ list_tasks (T024, T027)
    └→ Persist AI response (T018)
```

## MCP Tools

The AI agent has access to two MCP tools:

### add_task

Creates a new task.

**Parameters:**
- `user_id` (required): User UUID
- `title` (required): Task title
- `description` (optional): Task description
- `due_date` (optional): Due date (ISO 8601 or relative)
- `priority` (optional): "low", "medium", or "high"

### list_tasks

Lists and filters tasks.

**Parameters:**
- `user_id` (required): User UUID
- `status` (optional): "all", "pending", or "completed"
- `due_within_days` (optional): Filter by due date
- `limit` (optional): Max tasks to return (1-100, default 50)

## Next Steps

After successful integration:

1. **Test User Story 1**: Create tasks via natural language
2. **Test User Story 2**: List and filter tasks via natural language
3. **Monitor rate limiting**: Ensure 100/day limit works
4. **Test error handling**: Try without API key, with invalid user ID, etc.
5. **Proceed to User Story 3**: Task updates via natural language

## Support

For issues or questions:
- Check the validation script output: `python scripts/validate_chat_integration.py`
- Review API docs: http://localhost:8000/docs
- Check backend logs for detailed error messages