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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# Domify Academy Super Bot - Backend Documentation

## Overview

The backend is built with **Node.js + Express + tRPC** and integrates with **NVIDIA APIs** for LLM and image generation. It provides a robust, scalable foundation for the Grok-inspired AI chatbot.

---

## Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         Frontend (React)                β”‚
β”‚  (Ask | Imagine mode switcher)          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚ tRPC API calls
               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      Backend (Node.js + Express)        β”‚
β”‚                                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚  tRPC Routers                   β”‚   β”‚
β”‚  β”‚  β”œβ”€ chat.send                   β”‚   β”‚
β”‚  β”‚  β”œβ”€ imagine.generate            β”‚   β”‚
β”‚  β”‚  └─ search.online               β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚  LLM Engine                     β”‚   β”‚
β”‚  β”‚  β”œβ”€ Llama-3 70B (primary)       β”‚   β”‚
β”‚  β”‚  β”œβ”€ Fallback models             β”‚   β”‚
β”‚  β”‚  └─ Reasoning generation        β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚  Image Generation               β”‚   β”‚
β”‚  β”‚  β”œβ”€ SDXL                        β”‚   β”‚
β”‚  β”‚  β”œβ”€ Flux                        β”‚   β”‚
β”‚  β”‚  └─ Video conversion            β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚  Middleware                     β”‚   β”‚
β”‚  β”‚  β”œβ”€ Rate limiting               β”‚   β”‚
β”‚  β”‚  β”œβ”€ Request logging             β”‚   β”‚
β”‚  β”‚  β”œβ”€ Caching                     β”‚   β”‚
β”‚  β”‚  └─ Error handling              β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β–Ό          β–Ό          β–Ό
  MySQL    NVIDIA API  DuckDuckGo
  (DB)     (LLM/IMG)    (Search)
```

---

## Key Features

### 1. LLM Integration with Smart Fallback

**Primary Model**: Llama-3 70B
**Fallback Chain**: Llama-2 70B β†’ Mistral Large β†’ Llama-3 8B

```typescript
// Automatic fallback if primary model is busy
const response = await generateResponseWithReasoning(
  userPrompt,
  searchResults,
  enableThinking
);
```

### 2. DeepSeek-Style Reasoning

Generate internal thought process before final answer:

```typescript
// With reasoning enabled
const { reasoning, response } = await generateResponseWithReasoning(
  "What is quantum computing?",
  undefined,
  true  // enableThinking
);
```

### 3. Rate Limiting

Token bucket algorithm prevents API abuse:

- **30 requests/minute** per user
- **5 burst requests** allowed
- **Automatic cleanup** of old buckets

```typescript
const { allowed, remainingTokens } = checkRateLimit(userId, "chat");
if (!allowed) {
  // Rate limit exceeded
}
```

### 4. Search Integration

DuckDuckGo search for "Search Online" mode:

```typescript
const results = await searchOnline("latest AI news", 5);
const formatted = formatSearchResults(results);
```

### 5. Database Management

Conversation history, messages, images, and feedback stored in MySQL:

```typescript
// Save a message
await saveMessage(conversationId, "assistant", content, reasoning);

// Get conversation history
const messages = await getConversationMessages(conversationId);
```

### 6. Google Sheets Integration

Log feedback for analytics:

```typescript
await logFeedbackToSheets({
  userId,
  rating: "like",
  comment: "Great response!",
  timestamp: new Date().toISOString()
});
```

---

## API Endpoints (tRPC Procedures)

### Chat Procedures

#### `chat.send` (Protected)

Send a message and get a response.

**Input:**
```typescript
{
  prompt: string;           // User message
  enableSearch: boolean;    // Enable web search
  enableThinking: boolean;  // Enable reasoning
  history: Array<{          // Conversation history
    role: "user" | "assistant";
    content: string;
  }>;
}
```

**Output:**
```typescript
{
  success: boolean;
  response: string;         // LLM response
  reasoning: string;        // Internal thoughts
  model: string;            // Model used
  tokensUsed: number;       // Token count
}
```

### Image Generation Procedures

#### `imagine.generate` (Protected)

Generate an image from a prompt.

**Input:**
```typescript
{
  prompt: string;  // Image description
}
```

**Output:**
```typescript
{
  success: boolean;
  imageUrl: string;  // Generated image URL
  prompt: string;
}
```

### Search Procedures

#### `search.online` (Public)

Search the web using DuckDuckGo.

**Input:**
```typescript
{
  query: string;           // Search query
  maxResults: number;      // Max results (default: 5)
}
```

**Output:**
```typescript
{
  success: boolean;
  results: Array<{
    title: string;
    url: string;
    snippet: string;
  }>;
}
```

---

## Database Schema

### Users Table

```sql
CREATE TABLE users (
  id INT PRIMARY KEY AUTO_INCREMENT,
  openId VARCHAR(64) UNIQUE NOT NULL,
  email VARCHAR(320),
  name TEXT,
  tier VARCHAR(50) DEFAULT 'free',
  role ENUM('user', 'admin') DEFAULT 'user',
  ipAddress VARCHAR(45),
  createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
```

### Conversations Table

```sql
CREATE TABLE conversations (
  id INT PRIMARY KEY AUTO_INCREMENT,
  userId INT NOT NULL,
  title TEXT,
  mode ENUM('ask', 'imagine') DEFAULT 'ask',
  createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
```

### Messages Table

```sql
CREATE TABLE messages (
  id INT PRIMARY KEY AUTO_INCREMENT,
  conversationId INT NOT NULL,
  role ENUM('user', 'assistant') NOT NULL,
  content LONGTEXT NOT NULL,
  reasoning TEXT,
  metadata JSON,
  createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```

### Images Table

```sql
CREATE TABLE images (
  id INT PRIMARY KEY AUTO_INCREMENT,
  userId INT NOT NULL,
  conversationId INT,
  prompt TEXT NOT NULL,
  url TEXT NOT NULL,
  metadata JSON,
  createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```

### Feedback Table

```sql
CREATE TABLE feedback (
  id INT PRIMARY KEY AUTO_INCREMENT,
  userId INT NOT NULL,
  messageId INT,
  imageId INT,
  rating ENUM('like', 'dislike') NOT NULL,
  comment TEXT,
  createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
```

---

## File Structure

```
server/
β”œβ”€β”€ llm.ts                 # LLM engine with NVIDIA integration
β”œβ”€β”€ search.ts              # DuckDuckGo search integration
β”œβ”€β”€ rateLimit.ts           # Rate limiting middleware
β”œβ”€β”€ db.ts                  # Database helper functions
β”œβ”€β”€ googleSheets.ts        # Google Sheets logging
β”œβ”€β”€ middleware.ts          # Industrial-standard middleware
β”œβ”€β”€ routers.ts             # tRPC procedure definitions
└── _core/
    β”œβ”€β”€ index.ts           # Server entry point
    β”œβ”€β”€ context.ts         # tRPC context
    β”œβ”€β”€ trpc.ts            # tRPC setup
    β”œβ”€β”€ llm.ts             # Built-in LLM helper
    β”œβ”€β”€ imageGeneration.ts # Built-in image generation
    └── env.ts             # Environment variables
```

---

## Environment Variables

### Required

| Variable | Description |
|----------|-------------|
| `DATABASE_URL` | MySQL connection string |
| `NVIDIA_API_KEY` | NVIDIA API key |
| `JWT_SECRET` | Session token secret |

### Optional

| Variable | Description | Default |
|----------|-------------|---------|
| `GOOGLE_SHEETS_API_KEY` | Google Sheets API key | (disabled) |
| `GOOGLE_SHEETS_ID` | Google Sheet ID | (disabled) |
| `RATE_LIMIT_REQUESTS` | Requests per minute | 30 |
| `RATE_LIMIT_WINDOW` | Rate limit window (seconds) | 3600 |

---

## Industrial-Standard Features

### 1. Caching

In-memory cache for frequently accessed data:

```typescript
cacheManager.set("key", data, 300); // 5 minute TTL
const cached = cacheManager.get("key");
```

### 2. Performance Monitoring

Track operation durations:

```typescript
performanceMonitor.record("llm_call", 1234); // ms
const stats = performanceMonitor.getStats("llm_call");
```

### 3. Request Logging

Automatic request/response logging with performance metrics.

### 4. Error Handling

Comprehensive error handling with proper HTTP status codes.

### 5. Health Checks

Endpoint for monitoring application health:

```
GET /api/health
```

Response:
```json
{
  "status": "healthy",
  "uptime": 3600,
  "database": "connected",
  "cache": "active",
  "memoryUsage": 256
}
```

### 6. Security Headers

Automatic security headers on all responses:

- `X-Content-Type-Options: nosniff`
- `X-Frame-Options: DENY`
- `X-XSS-Protection: 1; mode=block`
- `Strict-Transport-Security: max-age=31536000`

---

## Development

### Local Setup

```bash
# Install dependencies
pnpm install

# Set up environment variables
cp .env.example .env
# Edit .env with your values

# Run database migrations
pnpm run db:push

# Start development server
pnpm run dev
```

### Testing

```bash
# Run tests
pnpm run test

# Watch mode
pnpm run test:watch
```

### Type Checking

```bash
# Check TypeScript
pnpm run check
```

---

## Deployment

See `DEPLOYMENT.md` for complete deployment instructions.

### Quick Deploy to Hugging Face

```bash
# 1. Create a new Space on Hugging Face
# 2. Push code to the Space repository
git push origin main

# 3. Set environment variables in Space settings
# 4. Hugging Face automatically builds and deploys
```

---

## Monitoring

### Logs

Check application logs in Hugging Face Space:

```
Logs tab β†’ Filter by date/time β†’ Search for errors
```

### Metrics

Monitor key metrics:

- **Response time**: Average LLM response time
- **Error rate**: Percentage of failed requests
- **Cache hit rate**: Percentage of cached responses
- **Database performance**: Query execution time

### Alerts

Set up alerts for:

- High error rate (>5%)
- Slow responses (>5s)
- Database connection failures
- Memory usage >80%

---

## Troubleshooting

### Issue: "Rate limit exceeded"

**Cause**: User has exceeded request limit

**Solution**:
1. Wait for rate limit window to reset
2. Upgrade user tier for higher limits
3. Adjust `RATE_LIMIT_REQUESTS` if needed

### Issue: "NVIDIA API error"

**Cause**: Invalid API key or quota exceeded

**Solution**:
1. Verify `NVIDIA_API_KEY` is correct
2. Check NVIDIA API dashboard for quota
3. Wait for quota reset or upgrade plan

### Issue: "Database connection failed"

**Cause**: Invalid connection string or network issue

**Solution**:
1. Verify `DATABASE_URL` format
2. Check database is running and accessible
3. Verify firewall rules allow connection

### Issue: "Out of memory"

**Cause**: Memory leak or insufficient resources

**Solution**:
1. Restart the application
2. Review recent code changes
3. Upgrade Space compute resources

---

## Best Practices

1. **Always use rate limiting** to prevent abuse
2. **Cache frequently accessed data** to improve performance
3. **Log all errors** for debugging and monitoring
4. **Use environment variables** for configuration
5. **Validate user input** before processing
6. **Handle errors gracefully** with proper HTTP status codes
7. **Monitor performance** and optimize bottlenecks
8. **Keep dependencies updated** for security

---

## Support

For issues or questions:

1. Check logs in Hugging Face Space
2. Review `DEPLOYMENT.md` for deployment issues
3. Check `ARCHITECTURE.md` for design details
4. Contact NVIDIA support for API issues

---

## License

MIT License - See LICENSE file for details