Compare commits
180 Commits
fix/issue-
...
fix/issue-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f46cad4379 | ||
|
|
1b8761d1f4 | ||
| 958dc3bb1f | |||
|
|
5ae8d76bde | ||
| a9679bbb5d | |||
|
|
b1ddad0808 | ||
|
|
f705269e34 | ||
| 8acce849f4 | |||
|
|
2d125ede22 | ||
|
|
7a64632a63 | ||
|
|
bb62e53093 | ||
|
|
cf74251ad0 | ||
|
|
1efc0eaba6 | ||
|
|
f4f6168f68 | ||
|
|
62bcd6e099 | ||
|
|
6b8912a7eb | ||
|
|
2c3b6ef073 | ||
|
|
613ec0dc1f | ||
|
|
7bdd49a56c | ||
|
|
e92506a787 | ||
|
|
696d3934d5 | ||
|
|
466fdf1fe9 | ||
|
|
39a27caf05 | ||
|
|
61b9da295b | ||
|
|
38e45b9fd0 | ||
|
|
e41d07486b | ||
| 7e03101e7b | |||
|
|
70dfba2ffc | ||
|
|
6d204b537d | ||
|
|
2b7f54703e | ||
|
|
99dded8d16 | ||
|
|
29b7634c34 | ||
|
|
fd5c2b56d7 | ||
|
|
632e1bf524 | ||
|
|
5ae1165ad9 | ||
|
|
283573f5a8 | ||
|
|
90fa66bd39 | ||
|
|
84d8a6f4a6 | ||
|
|
a8e0baf0c0 | ||
|
|
6c39e4e89d | ||
|
|
bba773251a | ||
|
|
3013326ded | ||
|
|
a82185de60 | ||
|
|
cadea23e40 | ||
|
|
984656c83c | ||
|
|
1505bc9913 | ||
|
|
dd61c32ea7 | ||
|
|
01ec8bc539 | ||
|
|
a253aae766 | ||
|
|
13e899c851 | ||
|
|
384f84e772 | ||
|
|
cd1a41d1d7 | ||
|
|
6a20cc174f | ||
|
|
ce8a29c0a4 | ||
|
|
f425ae08d7 | ||
|
|
d4400f5dcd | ||
|
|
1591fcb1ca | ||
|
|
b0131aa566 | ||
|
|
52adc93b25 | ||
|
|
79c3ec7d16 | ||
|
|
3505cf4ade | ||
|
|
1b1358353f | ||
|
|
726e579f5f | ||
|
|
b111e4d79f | ||
|
|
0d63a10ac8 | ||
|
|
19f28fc599 | ||
|
|
5f7667992e | ||
|
|
cd4583ca90 | ||
|
|
6cadb7a67b | ||
|
|
02e0b0ccab | ||
|
|
29ec67cced | ||
|
|
c86e71c3a3 | ||
|
|
44fb840731 | ||
|
|
6a5694f74b | ||
|
|
680a9322e3 | ||
|
|
9973b8f6e2 | ||
|
|
30476e782b | ||
|
|
02ca452655 | ||
|
|
cb9558d54f | ||
|
|
638e17eb73 | ||
|
|
69a8b06462 | ||
|
|
15e72b009c | ||
|
|
19ba0c7cc6 | ||
|
|
847890b634 | ||
|
|
6658a418cc | ||
|
|
5c9e46e693 | ||
|
|
194c4f8a62 | ||
|
|
7afcb983e8 | ||
|
|
caef4b36ed | ||
|
|
3bf2877df2 | ||
|
|
145c6710d1 | ||
|
|
3c8c85aefc | ||
|
|
39b2b558a5 | ||
|
|
7795753aaa | ||
|
|
36dcfdb6e2 | ||
|
|
48fc323dac | ||
|
|
0af2de7209 | ||
|
|
e82b8b3549 | ||
|
|
6f23b322d3 | ||
|
|
297a185215 | ||
|
|
f86ff75525 | ||
|
|
6f9564790f | ||
|
|
f43eb11f6f | ||
|
|
446da96ce4 | ||
|
|
922ef89c1e | ||
|
|
a601ebb08b | ||
|
|
bb40193fc3 | ||
|
|
3a7d3a3732 | ||
|
|
0f558a5e8e | ||
|
|
9e9ff6fa7f | ||
|
|
4c48932ece | ||
|
|
bfc85648db | ||
|
|
925920eee1 | ||
|
|
299e74cffa | ||
|
|
2b875cfa27 | ||
|
|
ae612ad725 | ||
|
|
08912019c2 | ||
|
|
44453877b3 | ||
|
|
ad4a1e89d5 | ||
|
|
57fa200ba9 | ||
|
|
db4fb83243 | ||
|
|
560b61c431 | ||
|
|
c6baadf8b8 | ||
|
|
937cc2da60 | ||
| 32cd7184ea | |||
|
|
765e390b9b | ||
| 21ce282cae | |||
|
|
4fa9b0456a | ||
| af9900d0ba | |||
|
|
b3ab004447 | ||
| d394bc0857 | |||
|
|
dfa806ab53 | ||
| 3493775b7f | |||
|
|
82645dfb3b | ||
| c17fa243a1 | |||
|
|
a55ed9cc04 | ||
| d1408b74b4 | |||
|
|
4197475eed | ||
| 87bac8894a | |||
|
|
bef4479675 | ||
| 75970c57e3 | |||
|
|
f23044465a | ||
| a6e4d28aa7 | |||
|
|
8693946cb8 | ||
| a2f549c056 | |||
|
|
ad6e57655d | ||
| ac5e9d8b81 | |||
|
|
81f3342365 | ||
| 6adad0701d | |||
|
|
405b35c3ba | ||
| dd25d38e7e | |||
|
|
da8327c0e0 | ||
| 8d33ea9a44 | |||
|
|
d81464b869 | ||
| 55b008d4e8 | |||
|
|
04e4c1a487 | ||
| feb65131fa | |||
|
|
50af4e0722 | ||
|
|
786e964e32 | ||
| 41b699f9ee | |||
|
|
ccc0404cd1 | ||
|
|
0a2e347fdb | ||
| 2561759b78 | |||
| b6f99aa8fe | |||
|
|
3806af3e23 | ||
| a892a403fb | |||
|
|
0bb5d9a5d6 | ||
| 875427a0c1 | |||
|
|
5eb623f022 | ||
| a59a1ccd97 | |||
|
|
965efa122b | ||
| 0fb16f06e4 | |||
|
|
a461005015 | ||
| b0311bc96f | |||
|
|
a280217254 | ||
| 0cc3327991 | |||
|
|
429d46c6d0 | ||
| a2f0c9a0e9 | |||
|
|
42640679c7 | ||
| f59e595ffd |
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
[submodule "ave-cloud-skill"]
|
||||
path = ave-cloud-skill
|
||||
url = https://github.com/AveCloud/ave-cloud-skill.git
|
||||
1
ave-cloud-skill
Submodule
1
ave-cloud-skill
Submodule
Submodule ave-cloud-skill added at 5eaef99e15
223
deployment/DEPLOYMENT.md
Normal file
223
deployment/DEPLOYMENT.md
Normal file
@@ -0,0 +1,223 @@
|
||||
# Deployment Guide
|
||||
|
||||
This document describes how to deploy the Randebu Trading Bot application to a production server.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Debian server with 8GB RAM, 4 cores
|
||||
- Python 3.10+
|
||||
- Node.js 18+
|
||||
- Nginx
|
||||
- SSL certificate (Let's Encrypt)
|
||||
- SSH access to server
|
||||
|
||||
## Server Structure
|
||||
|
||||
```
|
||||
/var/www/
|
||||
└── bot/
|
||||
├── backend/ # Backend application (FastAPI)
|
||||
├── frontend/ # Frontend static files (SvelteKit build)
|
||||
└── data/ # SQLite database and app data
|
||||
```
|
||||
|
||||
## Step-by-Step Deployment
|
||||
|
||||
### 1. Clone Repository
|
||||
|
||||
```bash
|
||||
ssh user@your-server
|
||||
sudo mkdir -p /var/www/bot
|
||||
sudo chown -R $USER:$USER /var/www/bot
|
||||
cd /var/www/bot
|
||||
git clone https://git.example.com/shoko/randebu.git .
|
||||
```
|
||||
|
||||
### 2. Setup Backend
|
||||
|
||||
```bash
|
||||
cd /var/www/bot/src/backend
|
||||
|
||||
python3 -m venv venv
|
||||
source venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
|
||||
mkdir -p /var/www/bot/data
|
||||
```
|
||||
|
||||
### 3. Configure Environment
|
||||
|
||||
Copy and configure the environment file:
|
||||
|
||||
```bash
|
||||
cp src/backend/.env.example /var/www/bot/data/.env
|
||||
nano /var/www/bot/data/.env
|
||||
```
|
||||
|
||||
Update these values:
|
||||
- `SECRET_KEY` - Generate a secure key
|
||||
- `DATABASE_URL` - Update path to `/var/www/bot/data/app.db`
|
||||
- `MINIMAX_API_KEY` - Your API key
|
||||
- `AVE_API_KEY` - Your API key
|
||||
|
||||
### 4. Build Frontend
|
||||
|
||||
```bash
|
||||
cd /var/www/bot/src/frontend
|
||||
npm install
|
||||
npm run build
|
||||
|
||||
# Move build to expected location
|
||||
mkdir -p /var/www/bot/frontend
|
||||
cp -r build/* /var/www/bot/frontend/
|
||||
```
|
||||
|
||||
### 5. Configure Nginx
|
||||
|
||||
Copy the nginx template and modify as needed:
|
||||
|
||||
```bash
|
||||
sudo cp /var/www/bot/deployment/scripts/nginx-template.conf /etc/nginx/sites-available/bot.yourdomain.com
|
||||
sudo nano /etc/nginx/sites-available/bot.yourdomain.com
|
||||
```
|
||||
|
||||
Update `bot.yourdomain.com` with your actual domain.
|
||||
|
||||
Enable the site:
|
||||
|
||||
```bash
|
||||
sudo ln -s /etc/nginx/sites-available/bot.yourdomain.com /etc/nginx/sites-enabled/
|
||||
sudo nginx -t
|
||||
sudo systemctl reload nginx
|
||||
```
|
||||
|
||||
### 6. Setup SSL Certificate
|
||||
|
||||
```bash
|
||||
sudo apt install certbot python3-certbot-nginx
|
||||
sudo certbot --nginx -d bot.yourdomain.com
|
||||
```
|
||||
|
||||
### 7. Configure Systemd Service
|
||||
|
||||
Copy and configure the systemd service:
|
||||
|
||||
```bash
|
||||
sudo cp /var/www/bot/deployment/scripts/systemd-template.service /etc/systemd/system/ave-backend.service
|
||||
sudo nano /etc/systemd/system/ave-backend.service
|
||||
```
|
||||
|
||||
Update `your-user` and `/var/www/bot` paths as needed.
|
||||
|
||||
### 8. Start Backend Service
|
||||
|
||||
```bash
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable ave-backend
|
||||
sudo systemctl start ave-backend
|
||||
sudo systemctl status ave-backend
|
||||
```
|
||||
|
||||
### 9. Configure Firewall
|
||||
|
||||
```bash
|
||||
sudo ufw allow 22/tcp
|
||||
sudo ufw allow 80/tcp
|
||||
sudo ufw allow 443/tcp
|
||||
sudo ufw enable
|
||||
```
|
||||
|
||||
### 10. Verify Deployment
|
||||
|
||||
1. Visit `https://bot.yourdomain.com` - should show frontend
|
||||
2. Visit `https://bot.yourdomain.com/api/...` - should hit backend API
|
||||
3. Check backend logs: `sudo journalctl -u ave-backend -f`
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
/var/www/bot/
|
||||
├── deployment/ # Deployment scripts and templates
|
||||
│ ├── DEPLOYMENT.md # This file
|
||||
│ └── scripts/
|
||||
│ ├── nginx-template.conf
|
||||
│ ├── systemd-template.service
|
||||
│ └── deploy.sh # Automated deployment script
|
||||
├── src/
|
||||
│ ├── backend/ # FastAPI application
|
||||
│ │ ├── app/
|
||||
│ │ │ ├── api/ # API routes
|
||||
│ │ │ ├── core/ # Core functionality
|
||||
│ │ │ ├── db/ # Database models
|
||||
│ │ │ └── services/ # Business logic
|
||||
│ │ ├── run.py
|
||||
│ │ └── requirements.txt
|
||||
│ └── frontend/ # SvelteKit application
|
||||
│ ├── src/
|
||||
│ └── package.json
|
||||
├── data/ # Runtime data (gitignored)
|
||||
│ ├── app.db # SQLite database
|
||||
│ └── .env # Environment variables
|
||||
└── frontend/ # Built frontend static files
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Backend won't start
|
||||
|
||||
Check logs:
|
||||
```bash
|
||||
sudo journalctl -u ave-backend -n 100
|
||||
```
|
||||
|
||||
Common issues:
|
||||
- Missing environment variables - check `.env` file
|
||||
- Port 8000 already in use - check configuration
|
||||
- Database path incorrect - verify paths
|
||||
|
||||
### Nginx errors
|
||||
|
||||
Test configuration:
|
||||
```bash
|
||||
sudo nginx -t
|
||||
```
|
||||
|
||||
Check error logs:
|
||||
```bash
|
||||
sudo tail -f /var/log/nginx/error.log
|
||||
```
|
||||
|
||||
### SSL certificate issues
|
||||
|
||||
Renew certificate:
|
||||
```bash
|
||||
sudo certbot renew
|
||||
```
|
||||
|
||||
Check certificate status:
|
||||
```bash
|
||||
sudo certbot certificates
|
||||
```
|
||||
|
||||
## Useful Commands
|
||||
|
||||
| Action | Command |
|
||||
|--------|---------|
|
||||
| Restart backend | `sudo systemctl restart ave-backend` |
|
||||
| View backend logs | `sudo journalctl -u ave-backend -f` |
|
||||
| Check nginx status | `sudo systemctl status nginx` |
|
||||
| Reload nginx | `sudo systemctl reload nginx` |
|
||||
| Check port 8000 | `curl http://localhost:8000/health` |
|
||||
|
||||
## Rolling Updates
|
||||
|
||||
To update the application:
|
||||
|
||||
```bash
|
||||
cd /var/www/bot
|
||||
git pull
|
||||
cd src/backend && source venv/bin/activate && pip install -r requirements.txt
|
||||
sudo systemctl restart ave-backend
|
||||
```
|
||||
|
||||
For frontend updates, rebuild and copy static files to `/var/www/bot/frontend`.
|
||||
47
deployment/scripts/deploy.sh
Executable file
47
deployment/scripts/deploy.sh
Executable file
@@ -0,0 +1,47 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
DEPLOY_DIR="/var/www/bot"
|
||||
DOMAIN="bot.yourdomain.com"
|
||||
GIT_REPO="https://git.example.com/shoko/randebu.git"
|
||||
BRANCH="main"
|
||||
|
||||
echo "=== Randebu Deployment Script ==="
|
||||
echo "Deploy directory: $DEPLOY_DIR"
|
||||
echo "Domain: $DOMAIN"
|
||||
echo ""
|
||||
|
||||
cd "$DEPLOY_DIR"
|
||||
|
||||
echo "[1/6] Pulling latest code..."
|
||||
git pull origin "$BRANCH"
|
||||
|
||||
echo "[2/6] Updating backend dependencies..."
|
||||
cd "$DEPLOY_DIR/src/backend"
|
||||
source venv/bin/activate
|
||||
pip install -r requirements.txt
|
||||
|
||||
echo "[3/6] Rebuilding frontend..."
|
||||
cd "$DEPLOY_DIR/src/frontend"
|
||||
npm install
|
||||
npm run build
|
||||
mkdir -p "$DEPLOY_DIR/frontend"
|
||||
cp -r build/* "$DEPLOY_DIR/frontend/"
|
||||
|
||||
echo "[4/6] Restarting backend service..."
|
||||
sudo systemctl restart ave-backend
|
||||
sleep 2
|
||||
sudo systemctl status ave-backend --no-pager
|
||||
|
||||
echo "[5/6] Testing endpoints..."
|
||||
curl -s "http://localhost:8000/health" && echo ""
|
||||
curl -s -o /dev/null -w "Frontend: %{http_code}\n" "https://$DOMAIN/" || true
|
||||
|
||||
echo "[6/6] Verifying SSL..."
|
||||
sudo certbot certificates 2>/dev/null | grep -A2 "$DOMAIN" || echo "No certificate found for $DOMAIN"
|
||||
|
||||
echo ""
|
||||
echo "=== Deployment Complete ==="
|
||||
echo "Backend: https://$DOMAIN/api/"
|
||||
echo "Frontend: https://$DOMAIN/"
|
||||
echo "Backend logs: sudo journalctl -u ave-backend -f"
|
||||
62
deployment/scripts/nginx-template.conf
Normal file
62
deployment/scripts/nginx-template.conf
Normal file
@@ -0,0 +1,62 @@
|
||||
server {
|
||||
listen 80;
|
||||
server_name bot.yourdomain.com;
|
||||
|
||||
return 301 https://$server_name$request_uri;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 443 ssl http2;
|
||||
server_name bot.yourdomain.com;
|
||||
|
||||
ssl_certificate /etc/letsencrypt/live/bot.yourdomain.com/fullchain.pem;
|
||||
ssl_certificate_key /etc/letsencrypt/live/bot.yourdomain.com/privkey.pem;
|
||||
ssl_protocols TLSv1.2 TLSv1.3;
|
||||
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
|
||||
ssl_prefer_server_ciphers off;
|
||||
ssl_session_cache shared:SSL:10m;
|
||||
ssl_session_timeout 1d;
|
||||
|
||||
root /var/www/bot/frontend;
|
||||
index index.html;
|
||||
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
|
||||
location /api/ {
|
||||
proxy_pass http://127.0.0.1:8000/api/;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection 'upgrade';
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_cache_bypass $http_upgrade;
|
||||
proxy_read_timeout 300s;
|
||||
proxy_connect_timeout 75s;
|
||||
proxy_send_timeout 300s;
|
||||
}
|
||||
|
||||
location /ws/ {
|
||||
proxy_pass http://127.0.0.1:8000/ws/;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
|
||||
expires 1y;
|
||||
add_header Cache-Control "public, immutable";
|
||||
}
|
||||
|
||||
gzip on;
|
||||
gzip_vary on;
|
||||
gzip_min_length 1024;
|
||||
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
|
||||
}
|
||||
23
deployment/scripts/systemd-template.service
Normal file
23
deployment/scripts/systemd-template.service
Normal file
@@ -0,0 +1,23 @@
|
||||
[Unit]
|
||||
Description=Randebu Trading Bot Backend
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=your-user
|
||||
WorkingDirectory=/var/www/bot/src/backend
|
||||
Environment="PATH=/var/www/bot/src/backend/venv/bin"
|
||||
ExecStart=/var/www/bot/src/backend/venv/bin/python /var/www/bot/src/backend/run.py
|
||||
Restart=always
|
||||
RestartSec=10
|
||||
TimeoutStartSec=300
|
||||
TimeoutStopSec=300
|
||||
|
||||
EnvironmentFile=/var/www/bot/data/.env
|
||||
|
||||
StandardOutput=journal
|
||||
StandardError=journal
|
||||
SyslogIdentifier=ave-backend
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
521
docs/AUDIT_REPORT.md
Normal file
521
docs/AUDIT_REPORT.md
Normal file
@@ -0,0 +1,521 @@
|
||||
# Randebu Trading Bot - Product & Technical Audit Report
|
||||
|
||||
> **Date:** 2026-04-09
|
||||
> **Phase:** Phase 1 Implementation Complete - Pre-Testing Review
|
||||
> **Purpose:** Document current state, issues found, and recommendations for next steps
|
||||
|
||||
---
|
||||
|
||||
## 1. Product Overview
|
||||
|
||||
### 1.1 What is Randebu?
|
||||
|
||||
Randebu is an AI-powered trading bot platform where users create and manage automated trading strategies through natural language chat—similar to ChatGPT, but specialized for creating trading bots.
|
||||
|
||||
### 1.2 Core User Flow
|
||||
|
||||
```
|
||||
User Registration → Create Bot → Chat with AI to Define Strategy
|
||||
→ Backtest Strategy → Simulate Trading → (Future) Live Trading
|
||||
```
|
||||
|
||||
### 1.3 Phase 1 Scope
|
||||
|
||||
| Feature | Status |
|
||||
|---------|--------|
|
||||
| BNB Chain only | ✅ Intended (not yet enforced) |
|
||||
| Backtest engine | ✅ Implemented |
|
||||
| Simulation engine | ✅ Implemented |
|
||||
| Natural language strategy parsing | ✅ Implemented |
|
||||
| User authentication | ✅ Implemented |
|
||||
| Multi-bot support (max 3) | ✅ Implemented |
|
||||
| Dummy wallet (database record) | ✅ Implemented |
|
||||
|
||||
### 1.4 Tech Stack
|
||||
|
||||
| Layer | Technology |
|
||||
|-------|------------|
|
||||
| Frontend | Svelte 5 + TypeScript |
|
||||
| Backend | Python FastAPI |
|
||||
| AI Agent | CrewAI + MiniMax LLM |
|
||||
| Database | SQLite |
|
||||
| Trading Data | AVE Cloud API |
|
||||
|
||||
---
|
||||
|
||||
## 2. Critical Issues (Must Fix Before Testing)
|
||||
|
||||
These issues will cause complete pipeline failure if not addressed.
|
||||
|
||||
### 2.1 Database Tables Never Created
|
||||
|
||||
**Location:** `src/backend/app/main.py`, `src/backend/run.py`
|
||||
|
||||
**Problem:** The application starts but never creates the database tables. There is no:
|
||||
- Alembic migration setup
|
||||
- `Base.metadata.create_all()` call on startup
|
||||
- Database initialization script
|
||||
|
||||
**Impact:** First database operation will fail with "table not found" error.
|
||||
|
||||
**Current State:**
|
||||
```python
|
||||
# core/database.py defines Base, but nothing calls:
|
||||
# Base.metadata.create_all(engine)
|
||||
```
|
||||
|
||||
**Fix Required:** Add database initialization on application startup.
|
||||
|
||||
---
|
||||
|
||||
### 2.2 Strategy Config Schema Mismatch
|
||||
|
||||
**Location:** Multiple files - see mapping below
|
||||
|
||||
**Problem:** The LLM outputs one schema format, but the backtest and simulation engines expect a completely different format. This is a **complete pipeline break** - strategies parsed by AI will never trigger any trades in backtesting.
|
||||
|
||||
#### Schema Flow Diagram
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────────────┐
|
||||
│ LLM OUTPUT (llm_connector.py) - What AI actually produces │
|
||||
├─────────────────────────────────────────────────────────────────────────┤
|
||||
│ { │
|
||||
│ "type": "price_drop", │
|
||||
│ "params": { │
|
||||
│ "token": "PEPE", │
|
||||
│ "threshold_percent": 5 │
|
||||
│ } │
|
||||
│ } │
|
||||
└─────────────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────────────────┐
|
||||
│ BACKEND VALIDATOR (crew.py - StrategyValidator.validate()) │
|
||||
├─────────────────────────────────────────────────────────────────────────┤
|
||||
│ # Validator expects params.threshold_percent - THIS WORKS │
|
||||
│ if "threshold_percent" not in params: │
|
||||
│ errors.append(f"Condition {i}: missing 'threshold_percent'") │
|
||||
└─────────────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼ (But engines look for flat fields)
|
||||
┌─────────────────────────────────────────────────────────────────────────┐
|
||||
│ BACKTEST ENGINE (services/backtest/engine.py - _check_condition()) │
|
||||
├─────────────────────────────────────────────────────────────────────────┤
|
||||
│ # What engine actually looks for: │
|
||||
│ threshold = condition.get("threshold", 0) # ❌ Returns 0! │
|
||||
│ token = condition.get("token") # ❌ Wrong path! │
|
||||
│ timeframe = condition.get("timeframe") # ❌ Not in params! │
|
||||
│ │
|
||||
│ # Result: Conditions NEVER trigger because field names don't match │
|
||||
└─────────────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────────────────┐
|
||||
│ SIMULATE ENGINE (services/simulate/engine.py - _check_condition()) │
|
||||
├─────────────────────────────────────────────────────────────────────────┤
|
||||
│ # Same issue as backtest engine │
|
||||
│ threshold = condition.get("threshold", 0) # ❌ Returns 0 │
|
||||
└─────────────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────────────────┐
|
||||
│ FRONTEND TYPES (src/frontend/src/lib/api/types.ts) │
|
||||
├─────────────────────────────────────────────────────────────────────────┤
|
||||
│ interface Condition { │
|
||||
│ type: 'price_drop' | 'price_rise' | 'volume_spike' | 'price_level';│
|
||||
│ token: string; # Flat - no params wrapper │
|
||||
│ threshold?: number; # Not threshold_percent! │
|
||||
│ timeframe?: string; # Exists here │
|
||||
│ } │
|
||||
└─────────────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
#### Field Mapping Table
|
||||
|
||||
| Component | Token Field | Threshold Field | Timeframe Field |
|
||||
|-----------|-----------|-----------------|-----------------|
|
||||
| LLM Output | `params.token` | `params.threshold_percent` | N/A |
|
||||
| Validator | `params.token` | `params.threshold_percent` | N/A |
|
||||
| Backtest Engine | `token` | `threshold` | `timeframe` |
|
||||
| Simulate Engine | `token` | `threshold` | `timeframe` |
|
||||
| Frontend Types | `token` | `threshold` | `timeframe` |
|
||||
|
||||
**Fix Required:** Normalize to ONE consistent schema across the entire pipeline. Recommended: Use the flat structure (token, threshold, timeframe) as it's simpler and already used by engines and frontend.
|
||||
|
||||
---
|
||||
|
||||
### 2.3 Bot Creation Will Fail
|
||||
|
||||
**Location:**
|
||||
- `src/backend/app/db/schemas.py` (BotCreate)
|
||||
- `src/frontend/src/lib/api/client.ts` (bots.create)
|
||||
|
||||
**Problem:**
|
||||
|
||||
| Issue | Details |
|
||||
|-------|---------|
|
||||
| Backend requires | `strategy_config: dict` (REQUIRED) |
|
||||
| Backend requires | `llm_config: dict` (REQUIRED) |
|
||||
| Frontend sends | Only `name` and optional `description` |
|
||||
|
||||
**Impact:** Users cannot create bots through the frontend - API will return validation error.
|
||||
|
||||
**Fix Required:** Either:
|
||||
1. Make `strategy_config` and `llm_config` optional in backend with default values
|
||||
2. OR update frontend to send default config values
|
||||
|
||||
---
|
||||
|
||||
### 2.4 Config Endpoints Return Empty Data
|
||||
|
||||
**Location:** `src/backend/app/api/config.py`
|
||||
|
||||
```python
|
||||
@router.get("/chains")
|
||||
def get_chains():
|
||||
return {"chains": []} # ❌ Always empty
|
||||
|
||||
@router.get("/tokens")
|
||||
def get_tokens():
|
||||
return {"tokens": []} # ❌ Always empty
|
||||
```
|
||||
|
||||
**Impact:** Frontend cannot populate dropdowns for chain/token selection.
|
||||
|
||||
**Fix Required:** Return BSC (BNB Chain) as the only supported chain in Phase 1, and query AVE API for available tokens.
|
||||
|
||||
---
|
||||
|
||||
## 3. Major Issues
|
||||
|
||||
### 3.1 Risk Management Not Implemented
|
||||
|
||||
**Location:**
|
||||
- `src/backend/app/db/models.py` (schema supports it)
|
||||
- `src/backend/app/services/backtest/engine.py`
|
||||
- `src/backend/app/services/simulate/engine.py`
|
||||
|
||||
**Problem:** The database schema and frontend UI support `risk_management` configuration:
|
||||
```typescript
|
||||
interface RiskManagement {
|
||||
stop_loss_percent?: number;
|
||||
take_profit_percent?: number;
|
||||
}
|
||||
```
|
||||
|
||||
However, neither the backtest nor simulation engines actually check or use stop-loss/take-profit logic during trade execution. The config is saved but ignored.
|
||||
|
||||
**Fix Required:** Implement actual stop-loss and take-profit checks in both engines.
|
||||
|
||||
---
|
||||
|
||||
### 3.2 Duplicate AveCloudClient Implementations
|
||||
|
||||
**Location:**
|
||||
- `src/backend/app/services/ave/client.py`
|
||||
- `src/backend/app/services/backtest/ave_client.py`
|
||||
|
||||
**Problem:** Two different AveCloudClient classes with different methods:
|
||||
|
||||
| `services/ave/client.py` | `services/backtest/ave_client.py` |
|
||||
|--------------------------|-----------------------------------|
|
||||
| `get_tokens()` | ❌ Missing |
|
||||
| `get_batch_prices()` | ✅ `get_batch_prices()` |
|
||||
| `get_token_details()` | ❌ Missing |
|
||||
| `get_klines()` | ✅ `get_klines()` |
|
||||
| `get_trending_tokens()` | ❌ Missing |
|
||||
| `get_token_risk()` | ❌ Missing |
|
||||
| `get_chain_quote()` | ❌ Missing |
|
||||
| `get_chain_swap()` | ❌ Missing |
|
||||
| ❌ Missing | `get_token_price()` |
|
||||
|
||||
Additionally, the simulate engine imports from the wrong location:
|
||||
```python
|
||||
# services/simulate/engine.py
|
||||
from ..backtest.ave_client import AveCloudClient # ❌ Wrong import
|
||||
```
|
||||
|
||||
**Fix Required:** Consolidate into ONE AveCloudClient class.
|
||||
|
||||
---
|
||||
|
||||
### 3.3 Silent Error Handling in Simulation
|
||||
|
||||
**Location:** `src/backend/app/services/simulate/engine.py`
|
||||
|
||||
```python
|
||||
try:
|
||||
# ... API calls ...
|
||||
except Exception as e:
|
||||
pass # ❌ Silently swallows ALL errors!
|
||||
```
|
||||
|
||||
**Impact:** If AVE API fails or returns bad data, the simulation continues silently with no logging or user feedback.
|
||||
|
||||
**Fix Required:** Add proper error logging and user-facing error messages.
|
||||
|
||||
---
|
||||
|
||||
### 3.4 No Chain Validation for Phase 1
|
||||
|
||||
**Problem:** You mentioned limiting to BNB Chain only for Phase 1, but:
|
||||
- No backend validation enforces this
|
||||
- Users can specify any chain in backtest/simulate config
|
||||
- The config endpoints return empty arrays
|
||||
|
||||
**Fix Required:** Add chain validation that only allows "bsc" for Phase 1.
|
||||
|
||||
---
|
||||
|
||||
### 3.5 In-Memory Token Blacklist
|
||||
|
||||
**Location:** `src/backend/app/api/auth.py`
|
||||
|
||||
```python
|
||||
TOKEN_BLACKLIST = set() # ❌ In-memory only
|
||||
```
|
||||
|
||||
**Problems:**
|
||||
- Resets when server restarts
|
||||
- Doesn't work with multiple workers/processes
|
||||
- Logout doesn't truly invalidate tokens in production
|
||||
|
||||
**Fix Required:** Use Redis or database-backed token blacklist for production.
|
||||
|
||||
---
|
||||
|
||||
### 3.6 Conversation History Not Passed to Crew
|
||||
|
||||
**Location:** `src/backend/app/api/bots.py`
|
||||
|
||||
```python
|
||||
history_for_crew = conversation_history[-10:] # Gets history
|
||||
crew = get_trading_crew() # ❌ Doesn't pass history!
|
||||
result = crew.chat(user_message, history_for_crew)
|
||||
```
|
||||
|
||||
The history is fetched but not actually used by the agent - each chat starts fresh.
|
||||
|
||||
**Fix Required:** Pass conversation history to the crew agent.
|
||||
|
||||
---
|
||||
|
||||
### 3.7 No Rate Limiting Applied
|
||||
|
||||
**Location:** `src/backend/app/main.py`
|
||||
|
||||
```python
|
||||
app.state.limiter = limiter # Set up but not used on most endpoints
|
||||
```
|
||||
|
||||
The rate limiter is initialized but only applied to the login endpoint. Other endpoints have no protection.
|
||||
|
||||
**Fix Required:** Apply rate limiting to sensitive endpoints.
|
||||
|
||||
---
|
||||
|
||||
### 3.8 CORS Wide Open
|
||||
|
||||
**Location:** `src/backend/app/main.py`
|
||||
|
||||
```python
|
||||
allow_origins=["*"] # ❌ Should be restricted to frontend domain
|
||||
```
|
||||
|
||||
**Fix Required:** Limit CORS to the frontend domain in production.
|
||||
|
||||
---
|
||||
|
||||
### 3.9 No WebSocket for Real-Time Updates
|
||||
|
||||
**Problem:** Users must poll the API to see:
|
||||
- Backtest progress
|
||||
- Simulation signals (new signals only appear on refresh)
|
||||
|
||||
**Impact:** Poor UX during long-running operations.
|
||||
|
||||
**Fix Required:** Add WebSocket support for real-time updates (Phase 2 or later).
|
||||
|
||||
---
|
||||
|
||||
## 4. Minor Issues
|
||||
|
||||
### 4.1 Unused Dependencies
|
||||
|
||||
**Location:** `src/backend/requirements.txt`
|
||||
|
||||
```python
|
||||
anthropic>=0.18.0 # Included but project uses MiniMax
|
||||
```
|
||||
|
||||
**Fix Required:** Remove unused dependency.
|
||||
|
||||
---
|
||||
|
||||
### 4.2 Missing .env Example
|
||||
|
||||
**Problem:** No `.env.example` file to guide deployment.
|
||||
|
||||
**Fix Required:** Create `.env.example` with all required variables documented.
|
||||
|
||||
---
|
||||
|
||||
### 4.3 No Input Sanitization
|
||||
|
||||
User-provided data (bot names, chat messages) isn't sanitized before storage or display.
|
||||
|
||||
**Fix Required:** Add input validation and sanitization.
|
||||
|
||||
---
|
||||
|
||||
### 4.4 Inconsistent Error Responses
|
||||
|
||||
Some endpoints return `{"detail": "..."}` (FastAPI default), others return custom error shapes.
|
||||
|
||||
**Fix Required:** Standardize error response format.
|
||||
|
||||
---
|
||||
|
||||
### 4.5 No Integration Tests
|
||||
|
||||
No tests that verify the full pipeline (chat → config → backtest).
|
||||
|
||||
**Fix Required:** Add integration tests.
|
||||
|
||||
---
|
||||
|
||||
## 5. Missing Documentation Files
|
||||
|
||||
The following should be created:
|
||||
|
||||
1. **`.env.example`** - All environment variables with descriptions
|
||||
2. **`docs/STRATEGY_SCHEMA.md`** - Single source of truth for strategy config schema
|
||||
3. **`docs/API_SCHEMA.md`** - API contract documentation
|
||||
4. **`init_db.py`** - Database initialization script
|
||||
|
||||
---
|
||||
|
||||
## 6. Recommendations Summary
|
||||
|
||||
### Priority Matrix
|
||||
|
||||
| Priority | Issue | Effort | Impact |
|
||||
|----------|-------|--------|--------|
|
||||
| **P0** | Database tables not created | Small | App crashes on startup |
|
||||
| **P0** | Bot creation fails | Small | Users can't create bots |
|
||||
| **P0** | Strategy schema mismatch | Medium | Backtesting completely broken |
|
||||
| **P0** | Config endpoints empty | Small | No chain/token selection |
|
||||
| **P1** | Risk management not implemented | Medium | No stop-loss/take-profit |
|
||||
| **P1** | Chain validation missing | Small | Can use non-BSC chains |
|
||||
| **P1** | Silent error handling | Small | Hard to debug issues |
|
||||
| **P2** | Duplicate AveCloudClient | Medium | Maintenance burden |
|
||||
| **P2** | CORS restricted | Small | Security hardening |
|
||||
| **P2** | Token blacklist (production) | Medium | Security |
|
||||
| **P2** | Rate limiting | Medium | DoS protection |
|
||||
| **P3** | WebSocket support | Large | UX improvement |
|
||||
| **P3** | Integration tests | Medium | Code quality |
|
||||
|
||||
---
|
||||
|
||||
## 7. AVE Cloud Integration Notes
|
||||
|
||||
### Rate Limit Strategy
|
||||
|
||||
| Tier | TPS | Recommended Approach |
|
||||
|------|-----|---------------------|
|
||||
| Free | 1 | Aggressive caching, batch requests |
|
||||
| Normal | 5 | Moderate caching |
|
||||
| Pro | 20 | Minimal caching |
|
||||
|
||||
### Caching Recommendations
|
||||
|
||||
1. **Token prices:** Cache for 30-60 seconds
|
||||
2. **Trending tokens:** Cache for 5-10 minutes
|
||||
3. **Token details:** Cache for 5-10 minutes
|
||||
4. **Risk assessments:** Cache for 15-30 minutes
|
||||
|
||||
### No Testnet Warning
|
||||
|
||||
AVE Cloud has **no testnet**. All API calls use real money:
|
||||
- Use quote/dry-run mode for testing
|
||||
- Start with minimal amounts ($1-10)
|
||||
- Contact AVE support about sandbox options
|
||||
|
||||
---
|
||||
|
||||
## 8. Next Steps
|
||||
|
||||
### Immediate (Before Testing)
|
||||
|
||||
1. Add database initialization to startup
|
||||
2. Fix bot creation (frontend or backend)
|
||||
3. **Normalize strategy schema** - Choose flat structure, update all components
|
||||
4. Populate config endpoints with BSC + default tokens
|
||||
5. Add BSC-only chain validation
|
||||
|
||||
### Short Term
|
||||
|
||||
6. Implement risk management (stop-loss/take-profit)
|
||||
7. Consolidate AveCloudClient
|
||||
8. Add proper error handling
|
||||
9. Create .env.example
|
||||
10. Add input sanitization
|
||||
|
||||
### Medium Term
|
||||
|
||||
11. Add WebSocket for real-time updates
|
||||
12. Implement production token blacklist (Redis)
|
||||
13. Apply rate limiting
|
||||
14. Restrict CORS
|
||||
15. Add integration tests
|
||||
|
||||
---
|
||||
|
||||
## 9. Files Reference
|
||||
|
||||
### Key Backend Files
|
||||
|
||||
| File | Purpose |
|
||||
|------|---------|
|
||||
| `src/backend/app/main.py` | FastAPI app initialization |
|
||||
| `src/backend/app/api/bots.py` | Bot CRUD + chat endpoint |
|
||||
| `src/backend/app/api/backtest.py` | Backtest API |
|
||||
| `src/backend/app/api/simulate.py` | Simulation API |
|
||||
| `src/backend/app/api/ave.py` | AVE Cloud proxy endpoints |
|
||||
| `src/backend/app/api/config.py` | Config endpoints |
|
||||
| `src/backend/app/db/schemas.py` | Pydantic schemas |
|
||||
| `src/backend/app/db/models.py` | SQLAlchemy models |
|
||||
| `src/backend/app/services/ai_agent/crew.py` | CrewAI agents |
|
||||
| `src/backend/app/services/ai_agent/llm_connector.py` | MiniMax LLM |
|
||||
| `src/backend/app/services/backtest/engine.py` | Backtest logic |
|
||||
| `src/backend/app/services/simulate/engine.py` | Simulation logic |
|
||||
| `src/backend/app/services/ave/client.py` | AVE Cloud client |
|
||||
|
||||
### Key Frontend Files
|
||||
|
||||
| File | Purpose |
|
||||
|------|---------|
|
||||
| `src/frontend/src/lib/api/client.ts` | API client |
|
||||
| `src/frontend/src/lib/api/types.ts` | TypeScript types |
|
||||
| `src/frontend/src/routes/bot/[id]/+page.svelte` | Bot chat page |
|
||||
| `src/frontend/src/routes/bot/[id]/backtest/+page.svelte` | Backtest page |
|
||||
| `src/frontend/src/routes/bot/[id]/simulate/+page.svelte` | Simulation page |
|
||||
| `src/frontend/src/lib/components/ChatInterface.svelte` | Chat UI |
|
||||
| `src/frontend/src/lib/components/StrategyPreview.svelte` | Strategy display |
|
||||
|
||||
---
|
||||
|
||||
## 10. Audit Complete
|
||||
|
||||
This audit was conducted by reviewing:
|
||||
- All source code in `src/backend/` and `src/frontend/`
|
||||
- Documentation in `docs/`
|
||||
- Database models and schemas
|
||||
- API endpoints and their implementations
|
||||
|
||||
The product has a **solid architectural foundation** and addresses a real market need. The core issues are manageable - primarily schema standardization and missing initialization code.
|
||||
|
||||
---
|
||||
|
||||
*End of Audit Report*
|
||||
27
docs/ISSUES.md
Normal file
27
docs/ISSUES.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# Open Issues
|
||||
|
||||
## Frontend
|
||||
|
||||
### Token Address Confirmation Dialog
|
||||
- **Priority**: High
|
||||
- **Status**: Open
|
||||
- **Description**: When user configures a trading strategy via chat and mentions a token (e.g., "buy PEPE"), the AI asks for the token contract address. The frontend should show a confirmation dialog allowing user to:
|
||||
1. See the token the AI detected (PEPE)
|
||||
2. Enter/confirm the BSC contract address
|
||||
3. Save the strategy with the confirmed address
|
||||
|
||||
**Related Files**:
|
||||
- Frontend: `src/frontend/src/routes/bot/[id]/+page.svelte`
|
||||
- Backend: `src/backend/app/services/ai_agent/conversational.py`
|
||||
|
||||
**Acceptance Criteria**:
|
||||
- [ ] Modal/dialog appears when AI detects a token without address
|
||||
- [ ] User can enter the contract address (0x...)
|
||||
- [ ] Strategy is saved only after user confirmation
|
||||
- [ ] Clear error handling if address is invalid
|
||||
|
||||
---
|
||||
|
||||
## Backend
|
||||
|
||||
*No open backend issues*
|
||||
279
docs/STRATEGY_SCHEMA.md
Normal file
279
docs/STRATEGY_SCHEMA.md
Normal file
@@ -0,0 +1,279 @@
|
||||
# Strategy Config Schema
|
||||
|
||||
> **Status:** DRAFT - Needs to be normalized with implementation
|
||||
> **Purpose:** Single source of truth for strategy configuration format
|
||||
|
||||
---
|
||||
|
||||
## 1. Overview
|
||||
|
||||
This document defines the structure of the `strategy_config` JSON object that represents a trading bot's strategy. This config is:
|
||||
- Generated by the AI from natural language input
|
||||
- Validated by the backend
|
||||
- Used by backtest and simulation engines
|
||||
- Displayed in the frontend
|
||||
|
||||
---
|
||||
|
||||
## 2. Schema Version
|
||||
|
||||
**Current Version:** 1.0
|
||||
**Status:** Flat structure (NOT nested in `params`)
|
||||
|
||||
> **IMPORTANT:** The current implementation has a mismatch where the LLM outputs a nested `params` structure but the engines expect flat fields. This document defines the **TARGET** schema to normalize all components.
|
||||
|
||||
---
|
||||
|
||||
## 3. Full Schema
|
||||
|
||||
```json
|
||||
{
|
||||
"version": "1.0",
|
||||
"conditions": [
|
||||
{
|
||||
"type": "price_drop",
|
||||
"token": "PEPE",
|
||||
"chain": "bsc",
|
||||
"threshold": 5,
|
||||
"timeframe": "1h"
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"type": "buy",
|
||||
"amount_percent": 10,
|
||||
"token": "PEPE"
|
||||
}
|
||||
],
|
||||
"risk_management": {
|
||||
"stop_loss_percent": 3,
|
||||
"take_profit_percent": 10
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. Field Definitions
|
||||
|
||||
### 4.1 Root Fields
|
||||
|
||||
| Field | Type | Required | Description |
|
||||
|-------|------|----------|-------------|
|
||||
| `version` | string | No | Schema version (for future compatibility) |
|
||||
| `conditions` | array | Yes | List of trigger conditions |
|
||||
| `actions` | array | Yes | List of actions to execute when conditions are met |
|
||||
| `risk_management` | object | No | Risk management settings |
|
||||
|
||||
### 4.2 Condition Object
|
||||
|
||||
| Field | Type | Required | Description |
|
||||
|-------|------|----------|-------------|
|
||||
| `type` | string | Yes | Condition type (see supported types below) |
|
||||
| `token` | string | Yes | Token symbol or address (e.g., "PEPE" or "0x123...-bsc") |
|
||||
| `chain` | string | No | Blockchain chain (default: "bsc") |
|
||||
| `threshold` | number | For price_drop/rise/volume_spike | Percentage threshold (e.g., 5 = 5%) |
|
||||
| `price` | number | For price_level | Price level to trigger on |
|
||||
| `direction` | string | For price_level | "above" or "below" |
|
||||
| `timeframe` | string | No | Time window for calculation (e.g., "1h", "15m") |
|
||||
|
||||
#### Supported Condition Types
|
||||
|
||||
| Type | Description | Required Fields |
|
||||
|------|-------------|-----------------|
|
||||
| `price_drop` | Triggers when token price drops by threshold % | token, threshold |
|
||||
| `price_rise` | Triggers when token price rises by threshold % | token, threshold |
|
||||
| `volume_spike` | Triggers when trading volume increases by threshold % | token, threshold |
|
||||
| `price_level` | Triggers when price crosses a specific level | token, price, direction |
|
||||
|
||||
### 4.3 Action Object
|
||||
|
||||
| Field | Type | Required | Description |
|
||||
|-------|------|----------|-------------|
|
||||
| `type` | string | Yes | Action type (buy, sell, hold, notify) |
|
||||
| `amount_percent` | number | For buy/sell | Percentage of portfolio to trade |
|
||||
| `token` | string | No | Token to trade (defaults to condition token) |
|
||||
|
||||
#### Supported Action Types
|
||||
|
||||
| Type | Description | Required Fields |
|
||||
|------|-------------|-----------------|
|
||||
| `buy` | Purchase tokens | amount_percent |
|
||||
| `sell` | Sell tokens | amount_percent |
|
||||
| `hold` | Do nothing (log only) | - |
|
||||
| `notify` | Send notification to user | - |
|
||||
|
||||
### 4.4 Risk Management Object
|
||||
|
||||
| Field | Type | Required | Description |
|
||||
|-------|------|----------|-------------|
|
||||
| `stop_loss_percent` | number | No | Exit trade if loss exceeds this % |
|
||||
| `take_profit_percent` | number | No | Exit trade if profit reaches this % |
|
||||
|
||||
---
|
||||
|
||||
## 5. Examples
|
||||
|
||||
### 5.1 Simple Buy on Price Drop
|
||||
|
||||
> "Buy PEPE when it drops 5% in 1 hour"
|
||||
|
||||
```json
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"type": "price_drop",
|
||||
"token": "PEPE",
|
||||
"chain": "bsc",
|
||||
"threshold": 5,
|
||||
"timeframe": "1h"
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"type": "buy",
|
||||
"amount_percent": 10
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 5.2 Buy on Price Rise with Stop Loss
|
||||
|
||||
> "Buy when PEPE rises 10%, but stop loss at 3%"
|
||||
|
||||
```json
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"type": "price_rise",
|
||||
"token": "PEPE",
|
||||
"threshold": 10,
|
||||
"timeframe": "4h"
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"type": "buy",
|
||||
"amount_percent": 20
|
||||
}
|
||||
],
|
||||
"risk_management": {
|
||||
"stop_loss_percent": 3
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 5.3 Sell on Price Level
|
||||
|
||||
> "Sell when PEPE reaches $0.0001"
|
||||
|
||||
```json
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"type": "price_level",
|
||||
"token": "PEPE",
|
||||
"price": 0.0001,
|
||||
"direction": "above"
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"type": "sell",
|
||||
"amount_percent": 100
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 5.4 Volume Spike Alert
|
||||
|
||||
> "Notify me when PEPE volume spikes 50%"
|
||||
|
||||
```json
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"type": "volume_spike",
|
||||
"token": "PEPE",
|
||||
"threshold": 50,
|
||||
"timeframe": "1h"
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"type": "notify"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. Validation Rules
|
||||
|
||||
### 6.1 Conditions
|
||||
|
||||
- At least one condition is required
|
||||
- Each condition must have a valid `type`
|
||||
- Token must be specified
|
||||
- Threshold must be positive number (for applicable types)
|
||||
- Price level must be specified for `price_level` type
|
||||
- Direction must be "above" or "below" for `price_level` type
|
||||
|
||||
### 6.2 Actions
|
||||
|
||||
- At least one action is required
|
||||
- Each action must have a valid `type`
|
||||
- `amount_percent` must be between 0 and 100
|
||||
|
||||
### 6.3 Risk Management
|
||||
|
||||
- `stop_loss_percent` must be positive
|
||||
- `take_profit_percent` must be positive
|
||||
|
||||
---
|
||||
|
||||
## 7. Implementation Status
|
||||
|
||||
### Components Using This Schema
|
||||
|
||||
| Component | Status | Notes |
|
||||
|-----------|--------|-------|
|
||||
| Backend Validator (crew.py) | ❌ Mismatch | Uses nested `params` structure |
|
||||
| Backtest Engine | ❌ Mismatch | Uses flat structure (correct) |
|
||||
| Simulate Engine | ❌ Mismatch | Uses flat structure (correct) |
|
||||
| Frontend Types | ✅ Match | Uses flat structure |
|
||||
| Frontend StrategyPreview | ✅ Match | Uses flat structure |
|
||||
|
||||
### Normalization Required
|
||||
|
||||
The LLM output parser should be updated to output flat structure (not nested in `params`) to match what the engines and frontend expect.
|
||||
|
||||
---
|
||||
|
||||
## 8. Future Extensions
|
||||
|
||||
### Potential Condition Types (Phase 2+)
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `rsi_oversold` | RSI indicator below threshold |
|
||||
| `rsi_overbought` | RSI indicator above threshold |
|
||||
| `ma_crossover` | Moving average crossover |
|
||||
| `bollinger_breakout` | Bollinger Band breakout |
|
||||
| `news_sentiment` | Based on news sentiment analysis |
|
||||
|
||||
### Potential Action Types (Phase 2+)
|
||||
|
||||
| Type | Description |
|
||||
|------|-------------|
|
||||
| `dca_buy` | Dollar cost averaging buy |
|
||||
| `trailing_stop` | Trailing stop loss |
|
||||
| `smart_rebalance` | Portfolio rebalancing |
|
||||
|
||||
---
|
||||
|
||||
*Document Version: 1.0*
|
||||
*Last Updated: 2026-04-09*
|
||||
@@ -1,11 +1,68 @@
|
||||
# Randebu Trading Bot - Environment Variables Template
|
||||
# Copy this file to .env and fill in your values
|
||||
|
||||
# =============================================================================
|
||||
# DATABASE
|
||||
# =============================================================================
|
||||
|
||||
# SQLite database path (relative or absolute)
|
||||
# Example: sqlite:///./data/app.db
|
||||
DATABASE_URL=sqlite:///./data/app.db
|
||||
|
||||
# =============================================================================
|
||||
# AUTHENTICATION
|
||||
# =============================================================================
|
||||
|
||||
# Secret key for JWT token signing
|
||||
# Generate with: python -c "import secrets; print(secrets.token_hex(32))"
|
||||
SECRET_KEY=your-super-secret-key-change-in-production
|
||||
|
||||
# JWT algorithm (HS256 is recommended)
|
||||
JWT_ALGORITHM=HS256
|
||||
|
||||
# Token expiration time in minutes (1440 = 24 hours)
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES=1440
|
||||
|
||||
# =============================================================================
|
||||
# MINIMAX LLM
|
||||
# =============================================================================
|
||||
|
||||
# MiniMax API key (get from https://platform.minimax.chat/)
|
||||
MINIMAX_API_KEY=your-minimax-api-key
|
||||
MINIMAX_MODEL=MiniMax-Text-01
|
||||
AVE_API_KEY=your-ave-cloud-api-key
|
||||
|
||||
# MiniMax model to use
|
||||
# Common options: MiniMax-Text-01, MiniMax-M2.1
|
||||
MINIMAX_MODEL=MiniMax-M2.7
|
||||
|
||||
# =============================================================================
|
||||
# AVE CLOUD API
|
||||
# =============================================================================
|
||||
|
||||
# AVE Cloud API key (get from https://cloud.ave.ai/)
|
||||
AVE_API_KEY=your-ave-api-key
|
||||
|
||||
# AVE Cloud plan tier
|
||||
# Options: free, normal, pro
|
||||
# Note: Free tier has 1 TPS limit, Pro required for WebSocket
|
||||
AVE_API_PLAN=free
|
||||
|
||||
# =============================================================================
|
||||
# SERVER CONFIGURATION
|
||||
# =============================================================================
|
||||
|
||||
# Server host (0.0.0.0 for all interfaces)
|
||||
HOST=0.0.0.0
|
||||
|
||||
# Server port
|
||||
PORT=8000
|
||||
|
||||
# Debug mode (set to false in production)
|
||||
DEBUG=false
|
||||
|
||||
# =============================================================================
|
||||
# FRONTEND CONFIGURATION (for reference)
|
||||
# =============================================================================
|
||||
|
||||
# Frontend environment variables (set in frontend .env file):
|
||||
# VITE_API_URL=https://bot.yourdomain.com/api
|
||||
# VITE_WS_URL=wss://bot.yourdomain.com/ws
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Request
|
||||
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import timedelta
|
||||
from typing import Annotated
|
||||
from typing import Optional, Annotated
|
||||
|
||||
from ..core.database import get_db
|
||||
from ..core.security import (
|
||||
@@ -12,41 +11,170 @@ from ..core.security import (
|
||||
verify_token,
|
||||
)
|
||||
from ..core.config import get_settings
|
||||
from ..db.schemas import UserCreate, UserResponse, Token
|
||||
from ..core.limiter import limiter
|
||||
from ..db.schemas import (
|
||||
UserCreate,
|
||||
LoginRequest,
|
||||
UserResponse,
|
||||
Token,
|
||||
UserSettings,
|
||||
UserSettingsUpdate,
|
||||
)
|
||||
from ..db.models import User
|
||||
|
||||
router = APIRouter()
|
||||
settings = get_settings()
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/auth/token")
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/auth/login")
|
||||
|
||||
# Custom optional token extractor that doesn't raise on missing token
|
||||
def get_optional_token(request: Request) -> Optional[str]:
|
||||
"""Extract bearer token from Authorization header, returning None if not present."""
|
||||
auth_header = request.headers.get("Authorization")
|
||||
if auth_header and auth_header.startswith("Bearer "):
|
||||
return auth_header[7:] # Remove "Bearer " prefix
|
||||
return None
|
||||
|
||||
TOKEN_BLACKLIST = set()
|
||||
|
||||
|
||||
@router.post("/register", response_model=UserResponse)
|
||||
def get_current_user(
|
||||
token: Annotated[str, Depends(oauth2_scheme)], db: Session = Depends(get_db)
|
||||
) -> User:
|
||||
if token in TOKEN_BLACKLIST:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Token has been revoked",
|
||||
)
|
||||
payload = verify_token(token)
|
||||
if payload is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid or expired token",
|
||||
)
|
||||
user_id = payload.get("sub")
|
||||
if user_id is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Invalid token payload",
|
||||
)
|
||||
user = db.query(User).filter(User.id == user_id).first()
|
||||
if user is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="User not found",
|
||||
)
|
||||
return user
|
||||
|
||||
|
||||
def get_optional_user(
|
||||
request: Request,
|
||||
db: Session = Depends(get_db),
|
||||
) -> Optional[User]:
|
||||
"""Get current user, returning None if not authenticated."""
|
||||
token = get_optional_token(request)
|
||||
|
||||
if not token:
|
||||
return None
|
||||
|
||||
if token in TOKEN_BLACKLIST:
|
||||
return None
|
||||
|
||||
payload = verify_token(token)
|
||||
if payload is None:
|
||||
return None
|
||||
|
||||
user_id = payload.get("sub")
|
||||
if user_id is None:
|
||||
return None
|
||||
|
||||
user = db.query(User).filter(User.id == user_id).first()
|
||||
return user
|
||||
|
||||
|
||||
@router.post(
|
||||
"/register", response_model=Token, status_code=status.HTTP_201_CREATED
|
||||
)
|
||||
def register(user: UserCreate, db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
existing_user = db.query(User).filter(User.email == user.email).first()
|
||||
if existing_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Email already registered",
|
||||
)
|
||||
hashed_password = get_password_hash(user.password)
|
||||
db_user = User(
|
||||
email=user.email,
|
||||
password_hash=hashed_password,
|
||||
)
|
||||
db.add(db_user)
|
||||
db.commit()
|
||||
db.refresh(db_user)
|
||||
|
||||
# Generate and return access token so frontend can proceed immediately
|
||||
access_token = create_access_token(data={"sub": db_user.id})
|
||||
return Token(access_token=access_token, token_type="bearer")
|
||||
|
||||
|
||||
@router.post("/login")
|
||||
@router.post("/login", response_model=Token)
|
||||
@limiter.limit("5/minute")
|
||||
def login(
|
||||
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
|
||||
request: Request,
|
||||
login_data: LoginRequest,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
)
|
||||
user = db.query(User).filter(User.email == login_data.username).first()
|
||||
if not user or not verify_password(login_data.password, user.password_hash):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect email or password",
|
||||
)
|
||||
access_token = create_access_token(data={"sub": user.id})
|
||||
return Token(access_token=access_token, token_type="bearer")
|
||||
|
||||
|
||||
@router.post("/logout")
|
||||
def logout(token: Annotated[str, Depends(oauth2_scheme)]):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
)
|
||||
def logout(
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
token: Annotated[str, Depends(oauth2_scheme)],
|
||||
):
|
||||
TOKEN_BLACKLIST.add(token)
|
||||
return {"message": "Successfully logged out"}
|
||||
|
||||
|
||||
@router.get("/me", response_model=UserResponse)
|
||||
def get_me(
|
||||
token: Annotated[str, Depends(oauth2_scheme)], db: Session = Depends(get_db)
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
)
|
||||
return current_user
|
||||
|
||||
|
||||
@router.get("/settings", response_model=UserSettings)
|
||||
def get_settings_endpoint(
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
):
|
||||
return UserSettings(email=current_user.email)
|
||||
|
||||
|
||||
@router.patch("/settings", response_model=UserSettings)
|
||||
def update_settings(
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
settings_update: UserSettingsUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
if settings_update.email:
|
||||
existing = (
|
||||
db.query(User)
|
||||
.filter(User.email == settings_update.email, User.id != current_user.id)
|
||||
.first()
|
||||
)
|
||||
if existing:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Email already in use",
|
||||
)
|
||||
current_user.email = settings_update.email
|
||||
if settings_update.password:
|
||||
current_user.password_hash = get_password_hash(settings_update.password)
|
||||
db.commit()
|
||||
db.refresh(current_user)
|
||||
return UserSettings(email=current_user.email)
|
||||
|
||||
265
src/backend/app/api/ave.py
Normal file
265
src/backend/app/api/ave.py
Normal file
@@ -0,0 +1,265 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Request
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import Annotated, Optional
|
||||
import httpx
|
||||
|
||||
from .auth import get_current_user
|
||||
from ..core.database import get_db
|
||||
from ..core.config import get_settings
|
||||
from ..db.models import User
|
||||
from ..services.ave import AveCloudClient, check_tier_access
|
||||
from ..db.schemas import (
|
||||
AveBatchPricesRequest,
|
||||
AveKlinesRequest,
|
||||
AveChainQuoteRequest,
|
||||
AveChainSwapRequest,
|
||||
)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def get_ave_client() -> AveCloudClient:
|
||||
settings = get_settings()
|
||||
return AveCloudClient(api_key=settings.AVE_API_KEY, plan=settings.AVE_API_PLAN)
|
||||
|
||||
|
||||
@router.get("/tokens")
|
||||
async def search_tokens(
|
||||
query: Optional[str] = None,
|
||||
chain: Optional[str] = None,
|
||||
limit: int = 20,
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
client = get_ave_client()
|
||||
try:
|
||||
tokens = await client.get_tokens(query=query, chain=chain, limit=limit)
|
||||
return {"tokens": tokens}
|
||||
except httpx.HTTPStatusError as e:
|
||||
if e.response.status_code == 429:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
||||
detail="Rate limit exceeded. Please try again later.",
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=e.response.status_code,
|
||||
detail=f"AVE API error: {e.response.text}",
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to fetch tokens: {str(e)}",
|
||||
)
|
||||
|
||||
|
||||
@router.post("/tokens/price")
|
||||
async def get_batch_prices(
|
||||
request: AveBatchPricesRequest,
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
client = get_ave_client()
|
||||
try:
|
||||
prices = await client.get_batch_prices(request.token_ids)
|
||||
return {"prices": prices}
|
||||
except httpx.HTTPStatusError as e:
|
||||
if e.response.status_code == 429:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
||||
detail="Rate limit exceeded. Please try again later.",
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=e.response.status_code,
|
||||
detail=f"AVE API error: {e.response.text}",
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to fetch batch prices: {str(e)}",
|
||||
)
|
||||
|
||||
|
||||
@router.get("/tokens/{token_id}")
|
||||
async def get_token_details(
|
||||
token_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
client = get_ave_client()
|
||||
try:
|
||||
token = await client.get_token_details(token_id)
|
||||
if token is None:
|
||||
return {"token": None, "upsell_message": None}
|
||||
return {"token": token}
|
||||
except httpx.HTTPStatusError as e:
|
||||
if e.response.status_code == 429:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
||||
detail="Rate limit exceeded. Please try again later.",
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=e.response.status_code,
|
||||
detail=f"AVE API error: {e.response.text}",
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to fetch token details: {str(e)}",
|
||||
)
|
||||
|
||||
|
||||
@router.get("/klines/{token_id}")
|
||||
async def get_klines(
|
||||
token_id: str,
|
||||
interval: str = "1h",
|
||||
limit: int = 100,
|
||||
start_time: Optional[int] = None,
|
||||
end_time: Optional[int] = None,
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
client = get_ave_client()
|
||||
try:
|
||||
klines = await client.get_klines(
|
||||
token_id=token_id,
|
||||
interval=interval,
|
||||
limit=limit,
|
||||
start_time=start_time,
|
||||
end_time=end_time,
|
||||
)
|
||||
return {"klines": klines}
|
||||
except httpx.HTTPStatusError as e:
|
||||
if e.response.status_code == 429:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
||||
detail="Rate limit exceeded. Please try again later.",
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=e.response.status_code,
|
||||
detail=f"AVE API error: {e.response.text}",
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to fetch klines: {str(e)}",
|
||||
)
|
||||
|
||||
|
||||
@router.get("/tokens/trending")
|
||||
async def get_trending_tokens(
|
||||
chain: Optional[str] = None,
|
||||
limit: int = 20,
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
client = get_ave_client()
|
||||
try:
|
||||
tokens = await client.get_trending_tokens(chain=chain, limit=limit)
|
||||
return {"tokens": tokens}
|
||||
except httpx.HTTPStatusError as e:
|
||||
if e.response.status_code == 429:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
||||
detail="Rate limit exceeded. Please try again later.",
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=e.response.status_code,
|
||||
detail=f"AVE API error: {e.response.text}",
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to fetch trending tokens: {str(e)}",
|
||||
)
|
||||
|
||||
|
||||
@router.get("/contracts/{contract_id}")
|
||||
async def get_token_risk(
|
||||
contract_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
client = get_ave_client()
|
||||
try:
|
||||
risk = await client.get_token_risk(contract_id)
|
||||
if risk is None:
|
||||
return {"risk": None, "upsell_message": None}
|
||||
return {"risk": risk}
|
||||
except httpx.HTTPStatusError as e:
|
||||
if e.response.status_code == 429:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
||||
detail="Rate limit exceeded. Please try again later.",
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=e.response.status_code,
|
||||
detail=f"AVE API error: {e.response.text}",
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to fetch token risk: {str(e)}",
|
||||
)
|
||||
|
||||
|
||||
@router.post("/chain/quote")
|
||||
async def get_chain_quote(
|
||||
request: AveChainQuoteRequest,
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
client = get_ave_client()
|
||||
try:
|
||||
quote = await client.get_chain_quote(
|
||||
chain=request.chain,
|
||||
from_token=request.from_token,
|
||||
to_token=request.to_token,
|
||||
amount=request.amount,
|
||||
slippage=request.slippage,
|
||||
)
|
||||
if quote is None:
|
||||
return {"quote": None, "upsell_message": None}
|
||||
return {"quote": quote}
|
||||
except httpx.HTTPStatusError as e:
|
||||
if e.response.status_code == 429:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
||||
detail="Rate limit exceeded. Please try again later.",
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=e.response.status_code,
|
||||
detail=f"AVE API error: {e.response.text}",
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to fetch chain quote: {str(e)}",
|
||||
)
|
||||
|
||||
|
||||
@router.post("/chain/swap")
|
||||
async def get_chain_swap(
|
||||
request: AveChainSwapRequest,
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
client = get_ave_client()
|
||||
try:
|
||||
swap = await client.get_chain_swap(
|
||||
chain=request.chain,
|
||||
from_token=request.from_token,
|
||||
to_token=request.to_token,
|
||||
amount=request.amount,
|
||||
slippage=request.slippage,
|
||||
wallet_address=request.wallet_address,
|
||||
)
|
||||
if swap is None:
|
||||
return {"swap": None, "upsell_message": None}
|
||||
return {"swap": swap}
|
||||
except httpx.HTTPStatusError as e:
|
||||
if e.response.status_code == 429:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_429_TOO_MANY_REQUESTS,
|
||||
detail="Rate limit exceeded. Please try again later.",
|
||||
)
|
||||
raise HTTPException(
|
||||
status_code=e.response.status_code,
|
||||
detail=f"AVE API error: {e.response.text}",
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to fetch chain swap: {str(e)}",
|
||||
)
|
||||
@@ -1,36 +1,340 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
import uuid
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, BackgroundTasks, Request
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
from typing import List, Dict, Any, Optional, Union
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
from .auth import get_optional_user, get_current_user
|
||||
from ..core.database import get_db
|
||||
from ..core.config import get_settings
|
||||
from ..db.schemas import BacktestCreate, BacktestResponse
|
||||
from ..db.models import Bot, Backtest, Signal, User, AnonymousUser
|
||||
from ..services.rate_limiter import RateLimiter
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
running_backtests: Dict[str, Any] = {}
|
||||
executor = ThreadPoolExecutor(max_workers=4)
|
||||
|
||||
@router.post("/bots/{bot_id}/backtest", response_model=BacktestResponse)
|
||||
def start_backtest(bot_id: str, config: BacktestCreate, db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
|
||||
def run_backtest_sync(
|
||||
backtest_id: str, db_url: str, bot_id: str, config: Dict[str, Any]
|
||||
):
|
||||
import asyncio
|
||||
import json
|
||||
from ..services.backtest.engine import BacktestEngine
|
||||
from ..core.database import SessionLocal
|
||||
|
||||
async def _run():
|
||||
engine = BacktestEngine(config)
|
||||
engine.run_id = backtest_id
|
||||
running_backtests[backtest_id] = engine
|
||||
try:
|
||||
results = await engine.run()
|
||||
|
||||
# Convert datetime objects to ISO strings for JSON serialization
|
||||
def convert_datetime(obj):
|
||||
if isinstance(obj, datetime):
|
||||
return obj.isoformat()
|
||||
elif isinstance(obj, dict):
|
||||
return {k: convert_datetime(v) for k, v in obj.items()}
|
||||
elif isinstance(obj, list):
|
||||
return [convert_datetime(i) for i in obj]
|
||||
return obj
|
||||
|
||||
results = convert_datetime(results)
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
backtest = db.query(Backtest).filter(Backtest.id == backtest_id).first()
|
||||
if backtest:
|
||||
backtest.status = engine.status
|
||||
backtest.ended_at = datetime.utcnow()
|
||||
backtest.result = results
|
||||
db.commit()
|
||||
|
||||
for signal in engine.signals:
|
||||
signal_data = convert_datetime(signal)
|
||||
db_signal = Signal(
|
||||
id=signal_data["id"],
|
||||
bot_id=signal_data["bot_id"],
|
||||
run_id=signal_data["run_id"],
|
||||
signal_type=signal_data["signal_type"],
|
||||
token=signal_data["token"],
|
||||
price=signal_data["price"],
|
||||
confidence=signal_data.get("confidence"),
|
||||
reasoning=signal_data.get("reasoning"),
|
||||
executed=signal_data.get("executed", False),
|
||||
created_at=signal["created_at"], # Use original datetime, not converted string
|
||||
)
|
||||
db.add(db_signal)
|
||||
db.commit()
|
||||
finally:
|
||||
db.close()
|
||||
finally:
|
||||
if backtest_id in running_backtests:
|
||||
del running_backtests[backtest_id]
|
||||
|
||||
asyncio.run(_run())
|
||||
|
||||
|
||||
@router.post(
|
||||
"/bots/{bot_id}/backtest",
|
||||
response_model=BacktestResponse,
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
)
|
||||
async def start_backtest(
|
||||
bot_id: str,
|
||||
config: BacktestCreate,
|
||||
background_tasks: BackgroundTasks,
|
||||
current_user: Optional[User] = Depends(get_optional_user),
|
||||
db: Session = Depends(get_db),
|
||||
request: Request = None,
|
||||
):
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Bot not found"
|
||||
)
|
||||
|
||||
# Check authorization
|
||||
if current_user:
|
||||
# Authenticated user - must own the bot
|
||||
if bot.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="Not authorized"
|
||||
)
|
||||
else:
|
||||
# Anonymous user - can only run backtests on anonymous bots (user_id = None)
|
||||
if bot.user_id is not None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You need to be logged in to run backtests on this bot"
|
||||
)
|
||||
|
||||
# Rate limit anonymous backtests
|
||||
anonymous_token = request.cookies.get("anonymous_token") if request else None
|
||||
if anonymous_token:
|
||||
try:
|
||||
RateLimiter.check_anonymous_backtest_limit(db, anonymous_token)
|
||||
except HTTPException as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You've reached the maximum of 1 backtest as an anonymous user. Please login or create an account for unlimited backtests."
|
||||
)
|
||||
|
||||
settings = get_settings()
|
||||
backtest_id = str(uuid.uuid4())
|
||||
|
||||
backtest_config = {
|
||||
"bot_id": bot_id,
|
||||
"token": config.token,
|
||||
"chain": config.chain,
|
||||
"timeframe": config.timeframe,
|
||||
"start_date": config.start_date,
|
||||
"end_date": config.end_date,
|
||||
"strategy_config": bot.strategy_config,
|
||||
"ave_api_key": settings.AVE_API_KEY,
|
||||
"ave_api_plan": settings.AVE_API_PLAN,
|
||||
"initial_balance": 10000.0,
|
||||
}
|
||||
|
||||
backtest = Backtest(
|
||||
id=backtest_id,
|
||||
bot_id=bot_id,
|
||||
started_at=datetime.utcnow(),
|
||||
status="running",
|
||||
config={
|
||||
"token": config.token,
|
||||
"chain": config.chain,
|
||||
"timeframe": config.timeframe,
|
||||
"start_date": config.start_date,
|
||||
"end_date": config.end_date,
|
||||
},
|
||||
)
|
||||
db.add(backtest)
|
||||
db.commit()
|
||||
db.refresh(backtest)
|
||||
|
||||
# Increment anonymous backtest count
|
||||
if not current_user and anonymous_token:
|
||||
RateLimiter.increment_backtest_count(db, anonymous_token)
|
||||
|
||||
db_url = str(settings.DATABASE_URL)
|
||||
background_tasks.add_task(
|
||||
run_backtest_sync, backtest_id, db_url, bot_id, backtest_config
|
||||
)
|
||||
|
||||
return backtest
|
||||
|
||||
|
||||
@router.get("/bots/{bot_id}/backtest/{run_id}", response_model=BacktestResponse)
|
||||
def get_backtest(bot_id: str, run_id: str, db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
)
|
||||
def get_backtest(
|
||||
bot_id: str,
|
||||
run_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Bot not found"
|
||||
)
|
||||
if bot.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="Not authorized"
|
||||
)
|
||||
|
||||
backtest = (
|
||||
db.query(Backtest)
|
||||
.filter(Backtest.id == run_id, Backtest.bot_id == bot_id)
|
||||
.first()
|
||||
)
|
||||
if not backtest:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Backtest not found"
|
||||
)
|
||||
|
||||
# Add progress from running engine if available
|
||||
if backtest.status == "running" and run_id in running_backtests:
|
||||
engine = running_backtests[run_id]
|
||||
backtest.progress = engine.progress
|
||||
|
||||
return backtest
|
||||
|
||||
|
||||
@router.get("/bots/{bot_id}/backtest/{run_id}/trades")
|
||||
def get_backtest_trades(
|
||||
bot_id: str,
|
||||
run_id: str,
|
||||
page: int = 1,
|
||||
per_page: int = 5,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""Get paginated trade history for a specific backtest.
|
||||
|
||||
Args:
|
||||
page: Page number (1-indexed)
|
||||
per_page: Number of trades per page (default 5, max 20)
|
||||
"""
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Bot not found"
|
||||
)
|
||||
if bot.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="Not authorized"
|
||||
)
|
||||
|
||||
backtest = (
|
||||
db.query(Backtest)
|
||||
.filter(Backtest.id == run_id, Backtest.bot_id == bot_id)
|
||||
.first()
|
||||
)
|
||||
if not backtest:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Backtest not found"
|
||||
)
|
||||
|
||||
# Get trades from result
|
||||
result = backtest.result or {}
|
||||
# Handle case where result might be a JSON string
|
||||
if isinstance(result, str):
|
||||
import json
|
||||
result = json.loads(result)
|
||||
all_trades = result.get("trades", []) or []
|
||||
total_trades = len(all_trades)
|
||||
|
||||
# Validate pagination params
|
||||
per_page = min(max(per_page, 1), 20) # Clamp between 1 and 20
|
||||
page = max(page, 1)
|
||||
|
||||
# Calculate pagination
|
||||
total_pages = max(1, (total_trades + per_page - 1) // per_page) if total_trades > 0 else 1
|
||||
start_idx = (page - 1) * per_page
|
||||
end_idx = start_idx + per_page
|
||||
|
||||
# Get page of trades (return empty list if start_idx >= total_trades)
|
||||
paginated_trades = all_trades[start_idx:end_idx] if start_idx < total_trades else []
|
||||
|
||||
return {
|
||||
"backtest_id": run_id,
|
||||
"trades": paginated_trades,
|
||||
"total_trades": total_trades,
|
||||
"page": page,
|
||||
"per_page": per_page,
|
||||
"total_pages": total_pages,
|
||||
"has_next": page < total_pages,
|
||||
"has_prev": page > 1,
|
||||
}
|
||||
|
||||
@router.get("/bots/{bot_id}/backtests", response_model=List[BacktestResponse])
|
||||
def list_backtests(bot_id: str, db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
def list_backtests(
|
||||
bot_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Bot not found"
|
||||
)
|
||||
if bot.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="Not authorized"
|
||||
)
|
||||
|
||||
backtests = (
|
||||
db.query(Backtest)
|
||||
.filter(Backtest.bot_id == bot_id)
|
||||
.order_by(Backtest.started_at.desc())
|
||||
.limit(5)
|
||||
.all()
|
||||
)
|
||||
return backtests
|
||||
|
||||
|
||||
@router.post("/bots/{bot_id}/backtest/{run_id}/stop")
|
||||
def stop_backtest(bot_id: str, run_id: str, db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
def stop_backtest(
|
||||
bot_id: str,
|
||||
run_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Bot not found"
|
||||
)
|
||||
if bot.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="Not authorized"
|
||||
)
|
||||
|
||||
backtest = (
|
||||
db.query(Backtest)
|
||||
.filter(Backtest.id == run_id, Backtest.bot_id == bot_id)
|
||||
.first()
|
||||
)
|
||||
if not backtest:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Backtest not found"
|
||||
)
|
||||
|
||||
if run_id in running_backtests:
|
||||
engine = running_backtests[run_id]
|
||||
engine.running = False # Direct sync access to running flag
|
||||
backtest.status = "stopped"
|
||||
backtest.ended_at = datetime.utcnow()
|
||||
db.commit()
|
||||
elif backtest.status == "running":
|
||||
# Engine already finished but status not updated
|
||||
backtest.status = "stopped"
|
||||
backtest.ended_at = datetime.utcnow()
|
||||
db.commit()
|
||||
|
||||
return {"status": "stopping", "run_id": run_id}
|
||||
|
||||
@@ -1,57 +1,298 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
from typing import List, Annotated, Optional
|
||||
|
||||
from .auth import get_current_user, get_optional_user
|
||||
from ..core.database import get_db
|
||||
from ..db.schemas import BotCreate, BotUpdate, BotResponse
|
||||
from ..core.config import get_settings
|
||||
from ..db.schemas import (
|
||||
BotCreate,
|
||||
BotUpdate,
|
||||
BotResponse,
|
||||
BotConversationCreate,
|
||||
BotConversationResponse,
|
||||
BotChatRequest,
|
||||
BotChatResponse,
|
||||
)
|
||||
from ..db.models import Bot, BotConversation, User
|
||||
from ..services.ai_agent.crew import get_trading_crew
|
||||
from ..services.ai_agent import get_conversational_agent
|
||||
|
||||
router = APIRouter()
|
||||
MAX_BOTS_PER_USER = 3
|
||||
|
||||
|
||||
@router.get("", response_model=List[BotResponse])
|
||||
def list_bots(db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
)
|
||||
def list_bots(
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
bots = db.query(Bot).filter(Bot.user_id == current_user.id).all()
|
||||
return bots
|
||||
|
||||
|
||||
@router.post("", response_model=BotResponse)
|
||||
def create_bot(bot: BotCreate, db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
@router.post("", response_model=BotResponse, status_code=status.HTTP_201_CREATED)
|
||||
def create_bot(
|
||||
bot_data: BotCreate,
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
user_bot_count = db.query(Bot).filter(Bot.user_id == current_user.id).count()
|
||||
if user_bot_count >= MAX_BOTS_PER_USER:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"Maximum of {MAX_BOTS_PER_USER} bots per user exceeded",
|
||||
)
|
||||
|
||||
existing_bot = (
|
||||
db.query(Bot)
|
||||
.filter(Bot.user_id == current_user.id, Bot.name == bot_data.name)
|
||||
.first()
|
||||
)
|
||||
if existing_bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Bot name must be unique per user",
|
||||
)
|
||||
|
||||
db_bot = Bot(
|
||||
user_id=current_user.id,
|
||||
name=bot_data.name,
|
||||
description=bot_data.description,
|
||||
strategy_config=bot_data.strategy_config,
|
||||
llm_config=bot_data.llm_config,
|
||||
)
|
||||
db.add(db_bot)
|
||||
db.commit()
|
||||
db.refresh(db_bot)
|
||||
return db_bot
|
||||
|
||||
|
||||
@router.get("/{bot_id}", response_model=BotResponse)
|
||||
def get_bot(bot_id: str, db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
)
|
||||
def get_bot(
|
||||
bot_id: str,
|
||||
current_user: Optional[User] = Depends(get_optional_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Bot not found",
|
||||
)
|
||||
|
||||
# Check authorization
|
||||
if current_user:
|
||||
# Authenticated user - must own the bot
|
||||
if bot.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not authorized to access this bot",
|
||||
)
|
||||
else:
|
||||
# Anonymous user - can only access anonymous bots (user_id = None)
|
||||
if bot.user_id is not None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not authorized to access this bot",
|
||||
)
|
||||
return bot
|
||||
|
||||
|
||||
@router.put("/{bot_id}", response_model=BotResponse)
|
||||
def update_bot(bot_id: str, bot: BotUpdate, db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
def update_bot(
|
||||
bot_id: str,
|
||||
bot_data: BotUpdate,
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Bot not found",
|
||||
)
|
||||
if bot.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not authorized to update this bot",
|
||||
)
|
||||
|
||||
if bot_data.name is not None:
|
||||
existing_bot = (
|
||||
db.query(Bot)
|
||||
.filter(
|
||||
Bot.user_id == current_user.id,
|
||||
Bot.name == bot_data.name,
|
||||
Bot.id != bot_id,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if existing_bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Bot name must be unique per user",
|
||||
)
|
||||
bot.name = bot_data.name
|
||||
|
||||
if bot_data.description is not None:
|
||||
bot.description = bot_data.description
|
||||
if bot_data.strategy_config is not None:
|
||||
bot.strategy_config = bot_data.strategy_config
|
||||
if bot_data.llm_config is not None:
|
||||
bot.llm_config = bot_data.llm_config
|
||||
if bot_data.status is not None:
|
||||
bot.status = bot_data.status
|
||||
|
||||
db.commit()
|
||||
db.refresh(bot)
|
||||
return bot
|
||||
|
||||
|
||||
@router.delete("/{bot_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_bot(
|
||||
bot_id: str,
|
||||
current_user: Annotated[User, Depends(get_current_user)],
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Bot not found",
|
||||
)
|
||||
if bot.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not authorized to delete this bot",
|
||||
)
|
||||
db.delete(bot)
|
||||
db.commit()
|
||||
|
||||
|
||||
@router.post("/{bot_id}/chat", response_model=BotChatResponse)
|
||||
def chat(
|
||||
bot_id: str,
|
||||
request: BotChatRequest,
|
||||
current_user: Optional[User] = Depends(get_optional_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Bot not found",
|
||||
)
|
||||
# Check authorization
|
||||
if current_user:
|
||||
# Authenticated user - must own the bot
|
||||
if bot.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not authorized to chat with this bot",
|
||||
)
|
||||
else:
|
||||
# Anonymous user - can only chat with anonymous bots (user_id = None)
|
||||
if bot.user_id is not None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not authorized to chat with this bot",
|
||||
)
|
||||
|
||||
conversation_history = (
|
||||
db.query(BotConversation)
|
||||
.filter(BotConversation.bot_id == bot_id)
|
||||
.order_by(BotConversation.created_at)
|
||||
.all()
|
||||
)
|
||||
history_for_agent = [
|
||||
{"role": conv.role, "content": conv.content}
|
||||
for conv in conversation_history[-10:]
|
||||
]
|
||||
|
||||
user_message = request.message
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.warning(f"chat endpoint: current_user={current_user}, user_id={current_user.id if current_user else None}")
|
||||
|
||||
# Use ConversationalAgent for natural chat with tool-calling
|
||||
agent = get_conversational_agent(
|
||||
bot_id=bot_id,
|
||||
user_id=current_user.id if current_user else None
|
||||
)
|
||||
logger.warning(f"chat endpoint: agent.user_id={agent.user_id}")
|
||||
|
||||
result = agent.chat(user_message, history_for_agent)
|
||||
|
||||
assistant_content = result.get("response", "I couldn't process your request.")
|
||||
|
||||
# Save conversation
|
||||
db_conversation = BotConversation(
|
||||
bot_id=bot_id,
|
||||
role="user",
|
||||
content=user_message,
|
||||
)
|
||||
db.add(db_conversation)
|
||||
|
||||
db_assistant = BotConversation(
|
||||
bot_id=bot_id,
|
||||
role="assistant",
|
||||
content=assistant_content,
|
||||
)
|
||||
db.add(db_assistant)
|
||||
db.commit()
|
||||
db.refresh(db_assistant)
|
||||
|
||||
# If strategy was updated via tool, refresh bot data
|
||||
if result.get("strategy_updated"):
|
||||
db.refresh(bot)
|
||||
|
||||
return BotChatResponse(
|
||||
response=assistant_content,
|
||||
thinking=result.get("thinking"),
|
||||
strategy_config=bot.strategy_config if result.get("strategy_updated") else None,
|
||||
success=result.get("success", False),
|
||||
strategy_needs_confirmation=result.get("strategy_needs_confirmation", False),
|
||||
strategy_data=result.get("strategy_data")
|
||||
if result.get("strategy_needs_confirmation")
|
||||
else None,
|
||||
token_search_results=result.get("token_search_results"),
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/{bot_id}")
|
||||
def delete_bot(bot_id: str, db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/{bot_id}/chat")
|
||||
def chat(bot_id: str, message: dict, db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{bot_id}/history")
|
||||
def get_history(bot_id: str, db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
@router.get("/{bot_id}/history", response_model=List[BotConversationResponse])
|
||||
def get_history(
|
||||
bot_id: str,
|
||||
current_user: Optional[User] = Depends(get_optional_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Bot not found",
|
||||
)
|
||||
# Check authorization
|
||||
if current_user:
|
||||
# Authenticated user - must own the bot
|
||||
if bot.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not authorized to access this bot's history",
|
||||
)
|
||||
else:
|
||||
# Anonymous user - can only access anonymous bots (user_id = None)
|
||||
if bot.user_id is not None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Not authorized to access this bot's history",
|
||||
)
|
||||
|
||||
conversations = (
|
||||
db.query(BotConversation)
|
||||
.filter(BotConversation.bot_id == bot_id)
|
||||
.order_by(BotConversation.created_at)
|
||||
.all()
|
||||
)
|
||||
return conversations
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from ..core.config import get_settings
|
||||
from ..services.ave import AveCloudClient
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/chains")
|
||||
def get_chains():
|
||||
return {"chains": []}
|
||||
return {"chains": ["bsc"]}
|
||||
|
||||
|
||||
@router.get("/tokens")
|
||||
def get_tokens():
|
||||
return {"tokens": []}
|
||||
async def get_tokens():
|
||||
settings = get_settings()
|
||||
client = AveCloudClient(api_key=settings.AVE_API_KEY, plan=settings.AVE_API_PLAN)
|
||||
tokens = await client.get_tokens(chain="bsc", limit=20)
|
||||
return {"tokens": tokens}
|
||||
|
||||
350
src/backend/app/api/conversations.py
Normal file
350
src/backend/app/api/conversations.py
Normal file
@@ -0,0 +1,350 @@
|
||||
import secrets
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Request, Response
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List, Optional, Annotated
|
||||
|
||||
from ..core.database import get_db
|
||||
from ..db.models import Conversation, Message, User, AnonymousUser, Bot
|
||||
from ..db.schemas import ChatRequest
|
||||
from ..api.auth import get_optional_user
|
||||
from ..services.rate_limiter import RateLimiter
|
||||
from ..services.ai_agent import get_conversational_agent
|
||||
|
||||
router = APIRouter(prefix="/api/conversations", tags=["conversations"])
|
||||
|
||||
|
||||
def get_or_create_anonymous_token(
|
||||
request: Request, response: Response, db: Session
|
||||
) -> str:
|
||||
token = request.cookies.get("anonymous_token")
|
||||
if not token:
|
||||
token = secrets.token_urlsafe(32)
|
||||
response.set_cookie(
|
||||
key="anonymous_token",
|
||||
value=token,
|
||||
max_age=60 * 60 * 24 * 365,
|
||||
httponly=True,
|
||||
)
|
||||
anon = AnonymousUser(id=token)
|
||||
db.add(anon)
|
||||
db.commit()
|
||||
return token
|
||||
|
||||
|
||||
@router.get("")
|
||||
def list_conversations(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: Optional[User] = Depends(get_optional_user),
|
||||
):
|
||||
if current_user:
|
||||
return (
|
||||
db.query(Conversation)
|
||||
.filter(Conversation.user_id == current_user.id)
|
||||
.order_by(Conversation.updated_at.desc())
|
||||
.all()
|
||||
)
|
||||
return []
|
||||
|
||||
|
||||
@router.post("")
|
||||
def create_conversation(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: Optional[User] = Depends(get_optional_user),
|
||||
request: Request = None,
|
||||
response: Response = None,
|
||||
):
|
||||
anonymous_token = None
|
||||
if not current_user and request:
|
||||
anonymous_token = get_or_create_anonymous_token(request, response, db)
|
||||
|
||||
conversation = Conversation(
|
||||
user_id=current_user.id if current_user else None,
|
||||
anonymous_token=anonymous_token,
|
||||
)
|
||||
db.add(conversation)
|
||||
db.commit()
|
||||
db.refresh(conversation)
|
||||
return conversation
|
||||
|
||||
|
||||
@router.get("/{conversation_id}")
|
||||
def get_conversation(
|
||||
conversation_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: Optional[User] = Depends(get_optional_user),
|
||||
):
|
||||
conversation = (
|
||||
db.query(Conversation).filter(Conversation.id == conversation_id).first()
|
||||
)
|
||||
if not conversation:
|
||||
raise HTTPException(status_code=404, detail="Conversation not found")
|
||||
|
||||
if conversation.user_id and current_user and conversation.user_id != current_user.id:
|
||||
raise HTTPException(status_code=403, detail="Access denied")
|
||||
|
||||
# Get messages for this conversation
|
||||
messages = (
|
||||
db.query(Message)
|
||||
.filter(Message.conversation_id == conversation_id)
|
||||
.order_by(Message.created_at)
|
||||
.all()
|
||||
)
|
||||
|
||||
# Build response with messages
|
||||
return {
|
||||
"id": conversation.id,
|
||||
"user_id": conversation.user_id,
|
||||
"bot_id": conversation.bot_id,
|
||||
"title": conversation.title,
|
||||
"created_at": conversation.created_at.isoformat() if conversation.created_at else None,
|
||||
"updated_at": conversation.updated_at.isoformat() if conversation.updated_at else None,
|
||||
"messages": [
|
||||
{
|
||||
"id": msg.id,
|
||||
"conversation_id": msg.conversation_id,
|
||||
"role": msg.role,
|
||||
"content": msg.content,
|
||||
"created_at": msg.created_at.isoformat() if msg.created_at else None,
|
||||
}
|
||||
for msg in messages
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@router.delete("/{conversation_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_conversation(
|
||||
conversation_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: Optional[User] = Depends(get_optional_user),
|
||||
):
|
||||
conversation = (
|
||||
db.query(Conversation).filter(Conversation.id == conversation_id).first()
|
||||
)
|
||||
if not conversation:
|
||||
raise HTTPException(status_code=404, detail="Conversation not found")
|
||||
|
||||
if conversation.user_id and current_user and conversation.user_id != current_user.id:
|
||||
raise HTTPException(status_code=403, detail="Access denied")
|
||||
|
||||
db.delete(conversation)
|
||||
db.commit()
|
||||
|
||||
|
||||
@router.post("/{conversation_id}/set-bot")
|
||||
def set_bot_for_conversation(
|
||||
conversation_id: str,
|
||||
bot_id: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: Optional[User] = Depends(get_optional_user),
|
||||
request: Request = None,
|
||||
):
|
||||
conversation = (
|
||||
db.query(Conversation).filter(Conversation.id == conversation_id).first()
|
||||
)
|
||||
if not conversation:
|
||||
raise HTTPException(status_code=404, detail="Conversation not found")
|
||||
|
||||
if conversation.user_id and current_user and conversation.user_id != current_user.id:
|
||||
raise HTTPException(status_code=403, detail="Access denied")
|
||||
|
||||
if not current_user:
|
||||
anonymous_token = request.cookies.get("anonymous_token") if request else None
|
||||
if anonymous_token:
|
||||
RateLimiter.check_anonymous_bot_limit(db, anonymous_token)
|
||||
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(status_code=404, detail="Bot not found")
|
||||
|
||||
if current_user and bot.user_id != current_user.id:
|
||||
raise HTTPException(status_code=403, detail="Not authorized to use this bot")
|
||||
|
||||
conversation.bot_id = bot_id
|
||||
db.commit()
|
||||
|
||||
if not current_user and request:
|
||||
anonymous_token = request.cookies.get("anonymous_token")
|
||||
if anonymous_token:
|
||||
RateLimiter.set_bot_created(db, anonymous_token)
|
||||
|
||||
return {"status": "updated", "bot_id": bot_id}
|
||||
|
||||
|
||||
@router.post("/{conversation_id}/chat")
|
||||
def chat_in_conversation(
|
||||
conversation_id: str,
|
||||
body: ChatRequest,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: Optional[User] = Depends(get_optional_user),
|
||||
request: Request = None,
|
||||
response: Response = None,
|
||||
):
|
||||
conversation = (
|
||||
db.query(Conversation).filter(Conversation.id == conversation_id).first()
|
||||
)
|
||||
if not conversation:
|
||||
raise HTTPException(status_code=404, detail="Conversation not found")
|
||||
|
||||
if conversation.user_id and current_user and conversation.user_id != current_user.id:
|
||||
raise HTTPException(status_code=403, detail="Access denied")
|
||||
|
||||
warning = None
|
||||
user_is_authenticated = current_user is not None
|
||||
|
||||
# Get anonymous_token from cookies or from the conversation itself
|
||||
anonymous_token = None
|
||||
if not current_user:
|
||||
RateLimiter.check_system_limit(db)
|
||||
|
||||
# First try to get from conversation (more reliable)
|
||||
anonymous_token = conversation.anonymous_token
|
||||
|
||||
# If not on conversation, try cookies
|
||||
if not anonymous_token and request:
|
||||
anonymous_token = request.cookies.get("anonymous_token")
|
||||
|
||||
# If still not found, create new one
|
||||
if not anonymous_token:
|
||||
anonymous_token = get_or_create_anonymous_token(request, response, db)
|
||||
# Also set it on the conversation for future use
|
||||
conversation.anonymous_token = anonymous_token
|
||||
db.commit()
|
||||
|
||||
# Debug logging
|
||||
import logging
|
||||
logging.info(f"Anonymous chat: token={anonymous_token}, checking limit")
|
||||
anon = RateLimiter.check_anonymous_limit(db, anonymous_token)
|
||||
if anon:
|
||||
logging.info(f"Anonymous user found: chat_count={anon.chat_count}")
|
||||
else:
|
||||
logging.info("Anonymous user NOT found in DB")
|
||||
RateLimiter.increment_chat_count(db, anonymous_token)
|
||||
|
||||
if anon and anon.chat_count > 40:
|
||||
warning = "Your progress is not saved."
|
||||
|
||||
# Always save the user's message first
|
||||
user_msg = Message(
|
||||
conversation_id=conversation_id,
|
||||
role="user",
|
||||
content=body.message,
|
||||
)
|
||||
db.add(user_msg)
|
||||
|
||||
# Get conversation history for context
|
||||
conversation_history = (
|
||||
db.query(Message)
|
||||
.filter(Message.conversation_id == conversation_id)
|
||||
.order_by(Message.created_at)
|
||||
.all()
|
||||
)
|
||||
history_for_agent = [
|
||||
{"role": msg.role, "content": msg.content} for msg in conversation_history[-10:]
|
||||
]
|
||||
|
||||
# Get user_id
|
||||
user_id = current_user.id if current_user else None
|
||||
|
||||
# Debug logging
|
||||
print(f"DEBUG: conversation_id={conversation_id}")
|
||||
print(f"DEBUG: conversation.bot_id={conversation.bot_id}")
|
||||
print(f"DEBUG: conversation.anonymous_token={conversation.anonymous_token[:20] if conversation.anonymous_token else None}")
|
||||
print(f"DEBUG: anonymous_token variable={anonymous_token[:20] if anonymous_token else None}")
|
||||
|
||||
# If no bot is set, use a general-purpose agent (without bot-specific context)
|
||||
if not conversation.bot_id:
|
||||
# Use the conversational agent with user context
|
||||
agent = get_conversational_agent(
|
||||
user_id=user_id,
|
||||
conversation_id=conversation_id,
|
||||
anonymous_token=anonymous_token,
|
||||
)
|
||||
result = agent.chat(body.message, history_for_agent)
|
||||
assistant_content = result.get("response", "I couldn't process your request.")
|
||||
|
||||
# Refresh conversation to get updated bot_id (in case agent set it)
|
||||
db.refresh(conversation)
|
||||
|
||||
# Save the assistant's response
|
||||
assistant_msg = Message(
|
||||
conversation_id=conversation_id,
|
||||
role="assistant",
|
||||
content=assistant_content,
|
||||
)
|
||||
db.add(assistant_msg)
|
||||
db.commit()
|
||||
|
||||
# Fetch updated conversation with messages
|
||||
conversation_history = (
|
||||
db.query(Message)
|
||||
.filter(Message.conversation_id == conversation_id)
|
||||
.order_by(Message.created_at)
|
||||
.all()
|
||||
)
|
||||
|
||||
return {
|
||||
"id": conversation.id,
|
||||
"user_id": conversation.user_id,
|
||||
"bot_id": conversation.bot_id,
|
||||
"title": conversation.title,
|
||||
"created_at": conversation.created_at.isoformat() if conversation.created_at else None,
|
||||
"updated_at": conversation.updated_at.isoformat() if conversation.updated_at else None,
|
||||
"messages": [
|
||||
{
|
||||
"id": msg.id,
|
||||
"conversation_id": msg.conversation_id,
|
||||
"role": msg.role,
|
||||
"content": msg.content,
|
||||
"created_at": msg.created_at.isoformat() if msg.created_at else None,
|
||||
}
|
||||
for msg in conversation_history
|
||||
],
|
||||
}
|
||||
|
||||
# Bot is set - process with the AI agent
|
||||
agent = get_conversational_agent(
|
||||
bot_id=conversation.bot_id,
|
||||
user_id=user_id,
|
||||
conversation_id=conversation_id,
|
||||
anonymous_token=anonymous_token,
|
||||
)
|
||||
result = agent.chat(body.message, history_for_agent)
|
||||
|
||||
assistant_content = result.get("response", "I couldn't process your request.")
|
||||
|
||||
# Save the assistant's response
|
||||
assistant_msg = Message(
|
||||
conversation_id=conversation_id,
|
||||
role="assistant",
|
||||
content=assistant_content,
|
||||
)
|
||||
db.add(assistant_msg)
|
||||
|
||||
db.commit()
|
||||
|
||||
# Fetch updated conversation with messages
|
||||
conversation_history = (
|
||||
db.query(Message)
|
||||
.filter(Message.conversation_id == conversation_id)
|
||||
.order_by(Message.created_at)
|
||||
.all()
|
||||
)
|
||||
|
||||
return {
|
||||
"id": conversation.id,
|
||||
"user_id": conversation.user_id,
|
||||
"bot_id": conversation.bot_id,
|
||||
"title": conversation.title,
|
||||
"created_at": conversation.created_at.isoformat() if conversation.created_at else None,
|
||||
"updated_at": conversation.updated_at.isoformat() if conversation.updated_at else None,
|
||||
"messages": [
|
||||
{
|
||||
"id": msg.id,
|
||||
"conversation_id": msg.conversation_id,
|
||||
"role": msg.role,
|
||||
"content": msg.content,
|
||||
"created_at": msg.created_at.isoformat() if msg.created_at else None,
|
||||
}
|
||||
for msg in conversation_history
|
||||
],
|
||||
}
|
||||
@@ -1,38 +1,322 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
import uuid
|
||||
import asyncio
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, BackgroundTasks
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
from typing import List, Dict, Any, Optional
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
from .auth import get_current_user
|
||||
from ..core.database import get_db
|
||||
from ..core.config import get_settings
|
||||
from ..db.schemas import SimulationCreate, SimulationResponse
|
||||
from ..db.models import Bot, Simulation, Signal, User
|
||||
from ..services.ave.client import AveCloudClient
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
running_simulations: Dict[str, Any] = {}
|
||||
executor = ThreadPoolExecutor(max_workers=4)
|
||||
|
||||
@router.post("/bots/{bot_id}/simulate", response_model=SimulationResponse)
|
||||
def start_simulation(
|
||||
bot_id: str, config: SimulationCreate, db: Session = Depends(get_db)
|
||||
|
||||
def run_simulation_sync(
|
||||
simulation_id: str, db_url: str, bot_id: str, config: Dict[str, Any]
|
||||
):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
import asyncio
|
||||
import time
|
||||
from ..services.simulate.engine import SimulateEngine
|
||||
from ..core.database import SessionLocal
|
||||
|
||||
async def _run():
|
||||
engine = SimulateEngine(config)
|
||||
engine.run_id = simulation_id
|
||||
running_simulations[simulation_id] = engine
|
||||
|
||||
# Serialize signals for JSON storage (convert datetime to string)
|
||||
def serialize_signal(s):
|
||||
created = s.get("created_at")
|
||||
if hasattr(created, "isoformat"):
|
||||
created = created.isoformat()
|
||||
return {
|
||||
**s,
|
||||
"created_at": created
|
||||
}
|
||||
|
||||
def save_progress():
|
||||
"""Save current progress to database."""
|
||||
db = SessionLocal()
|
||||
try:
|
||||
simulation = (
|
||||
db.query(Simulation).filter(Simulation.id == simulation_id).first()
|
||||
)
|
||||
if simulation:
|
||||
simulation.status = engine.status
|
||||
simulation.signals = [serialize_signal(s) for s in engine.signals]
|
||||
simulation.klines = [
|
||||
{"time": k.get("time"), "close": k.get("close")}
|
||||
for k in engine.klines
|
||||
]
|
||||
simulation.trade_log = engine.trade_log
|
||||
# Save portfolio data
|
||||
simulation.portfolio = {
|
||||
"initial_balance": engine.config.get("initial_balance", 10000),
|
||||
"current_balance": engine.current_balance,
|
||||
"position": engine.position,
|
||||
"position_token": engine.position_token,
|
||||
"entry_price": engine.entry_price,
|
||||
"current_price": engine.last_close,
|
||||
}
|
||||
db.commit()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
async def run_with_progress_save():
|
||||
"""Run simulation and save progress periodically."""
|
||||
last_save_time = time.time()
|
||||
save_interval = 5 # Save every 5 seconds
|
||||
|
||||
while engine.running and engine.status == "running":
|
||||
await asyncio.sleep(1) # Check every second
|
||||
|
||||
current_time = time.time()
|
||||
if current_time - last_save_time >= save_interval:
|
||||
save_progress()
|
||||
last_save_time = current_time
|
||||
|
||||
# Final save when done
|
||||
save_progress()
|
||||
|
||||
try:
|
||||
# Run both simulation and progress saving concurrently
|
||||
await asyncio.gather(
|
||||
engine.run(),
|
||||
run_with_progress_save()
|
||||
)
|
||||
finally:
|
||||
# Save final state
|
||||
save_progress()
|
||||
if simulation_id in running_simulations:
|
||||
del running_simulations[simulation_id]
|
||||
|
||||
asyncio.run(_run())
|
||||
|
||||
|
||||
@router.post(
|
||||
"/bots/{bot_id}/simulate",
|
||||
response_model=SimulationResponse,
|
||||
status_code=status.HTTP_201_CREATED,
|
||||
)
|
||||
async def start_simulation(
|
||||
bot_id: str,
|
||||
config: SimulationCreate,
|
||||
background_tasks: BackgroundTasks,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Bot not found"
|
||||
)
|
||||
if bot.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="Not authorized"
|
||||
)
|
||||
|
||||
# Check if there's already a running simulation for this bot
|
||||
existing_simulation = (
|
||||
db.query(Simulation)
|
||||
.filter(Simulation.bot_id == bot_id, Simulation.status == "running")
|
||||
.first()
|
||||
)
|
||||
if existing_simulation:
|
||||
# Stop the existing simulation first
|
||||
if existing_simulation.id in running_simulations:
|
||||
running_simulations[existing_simulation.id].stop()
|
||||
del running_simulations[existing_simulation.id]
|
||||
existing_simulation.status = "stopped"
|
||||
db.commit()
|
||||
|
||||
settings = get_settings()
|
||||
simulation_id = str(uuid.uuid4())
|
||||
|
||||
# Create AVE client for klines fetching
|
||||
ave_client = AveCloudClient(
|
||||
api_key=settings.AVE_API_KEY,
|
||||
plan=settings.AVE_API_PLAN,
|
||||
)
|
||||
|
||||
simulation_config = {
|
||||
"bot_id": bot_id,
|
||||
"token": config.token,
|
||||
"chain": config.chain,
|
||||
"kline_interval": config.kline_interval,
|
||||
"auto_execute": False, # Always paper trade
|
||||
"strategy_config": bot.strategy_config,
|
||||
"ave_api_key": settings.AVE_API_KEY,
|
||||
"ave_api_plan": settings.AVE_API_PLAN,
|
||||
}
|
||||
|
||||
simulation = Simulation(
|
||||
id=simulation_id,
|
||||
bot_id=bot_id,
|
||||
started_at=datetime.utcnow(),
|
||||
status="running",
|
||||
config={
|
||||
"token": config.token,
|
||||
"chain": config.chain,
|
||||
"kline_interval": config.kline_interval,
|
||||
},
|
||||
signals=[],
|
||||
klines=[],
|
||||
)
|
||||
db.add(simulation)
|
||||
db.commit()
|
||||
db.refresh(simulation)
|
||||
|
||||
# Fetch klines SYNCHRONOUSLY so user can see chart immediately
|
||||
try:
|
||||
token_id = f"{config.token}-{config.chain}"
|
||||
|
||||
# Calculate time range (last 1 hour)
|
||||
import time
|
||||
end_time = int(time.time() * 1000)
|
||||
start_time = end_time - (60 * 60 * 1000) # 1 hour ago
|
||||
|
||||
klines_data = await ave_client.get_klines(
|
||||
token_id,
|
||||
interval=config.kline_interval,
|
||||
start_time=start_time,
|
||||
end_time=end_time,
|
||||
limit=500
|
||||
)
|
||||
klines_for_chart = [
|
||||
{"time": k.get("time"), "close": k.get("close")}
|
||||
for k in sorted(klines_data, key=lambda x: x.get("time", 0))
|
||||
]
|
||||
# Update simulation with klines
|
||||
simulation.klines = klines_for_chart
|
||||
db.commit()
|
||||
db.refresh(simulation)
|
||||
logger.info(f"Fetched {len(klines_for_chart)} klines for simulation {simulation_id}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to fetch klines: {e}")
|
||||
|
||||
# Run simulation in background for signal processing
|
||||
background_tasks.add_task(
|
||||
run_simulation_sync, simulation_id, str(settings.DATABASE_URL), bot_id, simulation_config
|
||||
)
|
||||
|
||||
return simulation
|
||||
|
||||
|
||||
@router.get("/bots/{bot_id}/simulate/{run_id}", response_model=SimulationResponse)
|
||||
def get_simulation(bot_id: str, run_id: str, db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
def get_simulation(
|
||||
bot_id: str,
|
||||
run_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Bot not found"
|
||||
)
|
||||
if bot.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="Not authorized"
|
||||
)
|
||||
|
||||
simulation = (
|
||||
db.query(Simulation)
|
||||
.filter(Simulation.id == run_id, Simulation.bot_id == bot_id)
|
||||
.first()
|
||||
)
|
||||
if not simulation:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Simulation not found"
|
||||
)
|
||||
|
||||
if run_id in running_simulations:
|
||||
engine = running_simulations[run_id]
|
||||
simulation.signals = engine.get_signals()
|
||||
|
||||
return simulation
|
||||
|
||||
|
||||
@router.get("/bots/{bot_id}/simulations", response_model=List[SimulationResponse])
|
||||
def list_simulations(bot_id: str, db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
def list_simulations(
|
||||
bot_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Bot not found"
|
||||
)
|
||||
if bot.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="Not authorized"
|
||||
)
|
||||
|
||||
simulations = (
|
||||
db.query(Simulation)
|
||||
.filter(Simulation.bot_id == bot_id)
|
||||
.order_by(Simulation.started_at.desc())
|
||||
.all()
|
||||
)
|
||||
|
||||
for sim in simulations:
|
||||
if sim.id in running_simulations:
|
||||
engine = running_simulations[sim.id]
|
||||
sim.signals = engine.get_signals()
|
||||
# Include klines from running engine for chart display
|
||||
if hasattr(engine, 'klines'):
|
||||
sim.klines = [{"time": k.get("time"), "close": k.get("close")} for k in engine.klines]
|
||||
|
||||
return simulations
|
||||
|
||||
|
||||
@router.post("/bots/{bot_id}/simulate/{run_id}/stop")
|
||||
def stop_simulation(bot_id: str, run_id: str, db: Session = Depends(get_db)):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Not implemented"
|
||||
def stop_simulation(
|
||||
bot_id: str,
|
||||
run_id: str,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
bot = db.query(Bot).filter(Bot.id == bot_id).first()
|
||||
if not bot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Bot not found"
|
||||
)
|
||||
if bot.user_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="Not authorized"
|
||||
)
|
||||
|
||||
simulation = (
|
||||
db.query(Simulation)
|
||||
.filter(Simulation.id == run_id, Simulation.bot_id == bot_id)
|
||||
.first()
|
||||
)
|
||||
if not simulation:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Simulation not found"
|
||||
)
|
||||
|
||||
# Always update status to stopped, even if engine is not in memory
|
||||
simulation.status = "stopped"
|
||||
|
||||
# Try to stop the engine if it's still in memory
|
||||
if run_id in running_simulations:
|
||||
engine = running_simulations[run_id]
|
||||
engine.stop()
|
||||
del running_simulations[run_id]
|
||||
|
||||
db.commit()
|
||||
|
||||
return {"status": "stopped", "run_id": run_id}
|
||||
|
||||
1
src/backend/app/ave
Symbolic link
1
src/backend/app/ave
Symbolic link
@@ -0,0 +1 @@
|
||||
../../ave-cloud-skill/scripts/ave
|
||||
4
src/backend/app/core/limiter.py
Normal file
4
src/backend/app/core/limiter.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from slowapi import Limiter
|
||||
from slowapi.util import get_remote_address
|
||||
|
||||
limiter = Limiter(key_func=get_remote_address)
|
||||
@@ -10,6 +10,7 @@ from sqlalchemy import (
|
||||
ForeignKey,
|
||||
Index,
|
||||
JSON,
|
||||
Integer,
|
||||
)
|
||||
from sqlalchemy.orm import relationship
|
||||
from ..core.database import Base
|
||||
@@ -25,17 +26,21 @@ class User(Base):
|
||||
id = Column(String, primary_key=True, default=generate_uuid)
|
||||
email = Column(String, unique=True, nullable=False)
|
||||
password_hash = Column(String, nullable=False)
|
||||
tier = Column(String, default="free")
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
bots = relationship("Bot", back_populates="user", cascade="all, delete-orphan")
|
||||
conversations = relationship(
|
||||
"Conversation", back_populates="user", cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
|
||||
class Bot(Base):
|
||||
__tablename__ = "bots"
|
||||
|
||||
id = Column(String, primary_key=True, default=generate_uuid)
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=True) # nullable for anonymous bots
|
||||
name = Column(String, nullable=False)
|
||||
description = Column(Text)
|
||||
strategy_config = Column(JSON, nullable=False)
|
||||
@@ -46,6 +51,9 @@ class Bot(Base):
|
||||
|
||||
user = relationship("User", back_populates="bots")
|
||||
conversations = relationship(
|
||||
"Conversation", back_populates="bot", cascade="all, delete-orphan"
|
||||
)
|
||||
bot_conversations = relationship(
|
||||
"BotConversation", back_populates="bot", cascade="all, delete-orphan"
|
||||
)
|
||||
backtests = relationship(
|
||||
@@ -57,6 +65,47 @@ class Bot(Base):
|
||||
signals = relationship("Signal", back_populates="bot", cascade="all, delete-orphan")
|
||||
|
||||
|
||||
class Conversation(Base):
|
||||
__tablename__ = "conversations"
|
||||
|
||||
id = Column(String, primary_key=True, default=generate_uuid)
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=True)
|
||||
anonymous_token = Column(String(64), nullable=True)
|
||||
bot_id = Column(String, ForeignKey("bots.id"), nullable=True)
|
||||
title = Column(String(255), default="New Conversation")
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
user = relationship("User", back_populates="conversations")
|
||||
bot = relationship("Bot", back_populates="conversations")
|
||||
messages = relationship(
|
||||
"Message", back_populates="conversation", cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
|
||||
class Message(Base):
|
||||
__tablename__ = "messages"
|
||||
|
||||
id = Column(String, primary_key=True, default=generate_uuid)
|
||||
conversation_id = Column(String, ForeignKey("conversations.id"), nullable=True)
|
||||
role = Column(String, nullable=False)
|
||||
content = Column(Text, nullable=False)
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
|
||||
conversation = relationship("Conversation", back_populates="messages")
|
||||
|
||||
|
||||
class AnonymousUser(Base):
|
||||
__tablename__ = "anonymous_users"
|
||||
|
||||
id = Column(String(64), primary_key=True)
|
||||
chat_count = Column(Integer, default=0)
|
||||
bot_created = Column(Boolean, default=False)
|
||||
backtest_count = Column(Integer, default=0)
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
|
||||
class BotConversation(Base):
|
||||
__tablename__ = "bot_conversations"
|
||||
|
||||
@@ -66,7 +115,7 @@ class BotConversation(Base):
|
||||
content = Column(Text, nullable=False)
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
|
||||
bot = relationship("Bot", back_populates="conversations")
|
||||
bot = relationship("Bot", back_populates="bot_conversations")
|
||||
|
||||
|
||||
class Backtest(Base):
|
||||
@@ -92,6 +141,9 @@ class Simulation(Base):
|
||||
status = Column(String, nullable=False)
|
||||
config = Column(JSON, nullable=False)
|
||||
signals = Column(JSON)
|
||||
klines = Column(JSON) # Price data for chart display
|
||||
trade_log = Column(JSON) # Trade activity log
|
||||
portfolio = Column(JSON) # Portfolio data
|
||||
|
||||
bot = relationship("Bot", back_populates="simulations")
|
||||
|
||||
@@ -114,7 +166,10 @@ class Signal(Base):
|
||||
|
||||
|
||||
Index("idx_bots_user_id", Bot.user_id)
|
||||
Index("idx_conversations_bot_id", BotConversation.bot_id)
|
||||
Index("idx_conversations_user_id", Conversation.user_id)
|
||||
Index("idx_conversations_bot_id", Conversation.bot_id)
|
||||
Index("idx_messages_conversation_id", Message.conversation_id)
|
||||
Index("idx_bot_conversations_bot_id", BotConversation.bot_id)
|
||||
Index("idx_backtests_bot_id", Backtest.bot_id)
|
||||
Index("idx_simulations_bot_id", Simulation.bot_id)
|
||||
Index("idx_signals_bot_id", Signal.bot_id)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from pydantic import BaseModel, EmailStr
|
||||
from typing import Optional, List, Any
|
||||
from pydantic import BaseModel, EmailStr, field_validator
|
||||
from typing import Optional, List, Any, Dict
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
@@ -8,6 +8,11 @@ class UserCreate(BaseModel):
|
||||
password: str
|
||||
|
||||
|
||||
class LoginRequest(BaseModel):
|
||||
username: EmailStr
|
||||
password: str
|
||||
|
||||
|
||||
class UserResponse(BaseModel):
|
||||
id: str
|
||||
email: str
|
||||
@@ -23,11 +28,20 @@ class Token(BaseModel):
|
||||
token_type: str
|
||||
|
||||
|
||||
class UserSettings(BaseModel):
|
||||
email: EmailStr
|
||||
|
||||
|
||||
class UserSettingsUpdate(BaseModel):
|
||||
email: Optional[EmailStr] = None
|
||||
password: Optional[str] = None
|
||||
|
||||
|
||||
class BotCreate(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
strategy_config: dict
|
||||
llm_config: dict
|
||||
strategy_config: Optional[dict] = {}
|
||||
llm_config: Optional[dict] = {}
|
||||
|
||||
|
||||
class BotUpdate(BaseModel):
|
||||
@@ -40,7 +54,7 @@ class BotUpdate(BaseModel):
|
||||
|
||||
class BotResponse(BaseModel):
|
||||
id: str
|
||||
user_id: str
|
||||
user_id: Optional[str] # None for anonymous bots
|
||||
name: str
|
||||
description: Optional[str]
|
||||
strategy_config: dict
|
||||
@@ -55,11 +69,19 @@ class BotResponse(BaseModel):
|
||||
|
||||
class BacktestCreate(BaseModel):
|
||||
token: str
|
||||
token_name: Optional[str] = None
|
||||
chain: str
|
||||
timeframe: str
|
||||
start_date: str
|
||||
end_date: str
|
||||
|
||||
@field_validator("chain")
|
||||
@classmethod
|
||||
def chain_must_be_bsc(cls, v: str) -> str:
|
||||
if v != "bsc":
|
||||
raise ValueError("Phase 1 only supports BSC (bnb chain)")
|
||||
return v
|
||||
|
||||
|
||||
class BacktestResponse(BaseModel):
|
||||
id: str
|
||||
@@ -69,6 +91,7 @@ class BacktestResponse(BaseModel):
|
||||
status: str
|
||||
config: dict
|
||||
result: Optional[dict]
|
||||
progress: Optional[int] = None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
@@ -77,8 +100,14 @@ class BacktestResponse(BaseModel):
|
||||
class SimulationCreate(BaseModel):
|
||||
token: str
|
||||
chain: str
|
||||
duration_seconds: int = 3600
|
||||
auto_execute: bool = False
|
||||
kline_interval: str = "1m"
|
||||
|
||||
@field_validator("chain")
|
||||
@classmethod
|
||||
def chain_must_be_bsc(cls, v: str) -> str:
|
||||
if v != "bsc":
|
||||
raise ValueError("Phase 1 only supports BSC (bnb chain)")
|
||||
return v
|
||||
|
||||
|
||||
class SimulationResponse(BaseModel):
|
||||
@@ -88,6 +117,12 @@ class SimulationResponse(BaseModel):
|
||||
status: str
|
||||
config: dict
|
||||
signals: Optional[List[dict]]
|
||||
klines: Optional[List[dict]] = None # Price data for chart
|
||||
trade_log: Optional[List[dict]] = None # Trade activity log
|
||||
portfolio: Optional[dict] = None # Portfolio data
|
||||
current_candle_index: Optional[int] = None # Progress: current candle
|
||||
total_candles: Optional[int] = None # Progress: total candles
|
||||
candles_processed: Optional[int] = None # Progress: candles processed
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
@@ -109,6 +144,21 @@ class BotConversationResponse(BaseModel):
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class BotChatRequest(BaseModel):
|
||||
message: str
|
||||
strategy_config: Optional[bool] = False
|
||||
|
||||
|
||||
class BotChatResponse(BaseModel):
|
||||
response: str
|
||||
thinking: Optional[str] = None
|
||||
strategy_config: Optional[dict] = None
|
||||
success: bool = False
|
||||
strategy_needs_confirmation: Optional[bool] = False
|
||||
strategy_data: Optional[dict] = None
|
||||
token_search_results: Optional[List[dict]] = None
|
||||
|
||||
|
||||
class SignalResponse(BaseModel):
|
||||
id: str
|
||||
bot_id: str
|
||||
@@ -123,3 +173,126 @@ class SignalResponse(BaseModel):
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class AveTokenSearchResponse(BaseModel):
|
||||
tokens: List[dict]
|
||||
upsell_message: Optional[str] = None
|
||||
|
||||
|
||||
class AveBatchPricesRequest(BaseModel):
|
||||
token_ids: List[str]
|
||||
|
||||
|
||||
class AveBatchPricesResponse(BaseModel):
|
||||
prices: Dict[str, dict]
|
||||
upsell_message: Optional[str] = None
|
||||
|
||||
|
||||
class AveTokenDetailsResponse(BaseModel):
|
||||
token: Optional[dict] = None
|
||||
upsell_message: Optional[str] = None
|
||||
|
||||
|
||||
class AveKlinesRequest(BaseModel):
|
||||
token_id: str
|
||||
interval: str = "1h"
|
||||
limit: int = 100
|
||||
start_time: Optional[int] = None
|
||||
end_time: Optional[int] = None
|
||||
|
||||
|
||||
class AveKlinesResponse(BaseModel):
|
||||
klines: List[dict]
|
||||
upsell_message: Optional[str] = None
|
||||
|
||||
|
||||
class AveTrendingTokensResponse(BaseModel):
|
||||
tokens: List[dict]
|
||||
upsell_message: Optional[str] = None
|
||||
|
||||
|
||||
class AveTokenRiskResponse(BaseModel):
|
||||
risk: Optional[dict] = None
|
||||
upsell_message: Optional[str] = None
|
||||
|
||||
|
||||
class AveChainQuoteRequest(BaseModel):
|
||||
chain: str
|
||||
from_token: str
|
||||
to_token: str
|
||||
amount: str
|
||||
slippage: float = 0.5
|
||||
|
||||
|
||||
class AveChainQuoteResponse(BaseModel):
|
||||
quote: Optional[dict] = None
|
||||
upsell_message: Optional[str] = None
|
||||
|
||||
|
||||
class AveChainSwapRequest(BaseModel):
|
||||
chain: str
|
||||
from_token: str
|
||||
to_token: str
|
||||
amount: str
|
||||
slippage: float = 0.5
|
||||
wallet_address: Optional[str] = None
|
||||
|
||||
|
||||
class AveChainSwapResponse(BaseModel):
|
||||
swap: Optional[dict] = None
|
||||
upsell_message: Optional[str] = None
|
||||
|
||||
|
||||
class ConversationResponse(BaseModel):
|
||||
id: str
|
||||
user_id: Optional[str]
|
||||
anonymous_token: Optional[str]
|
||||
bot_id: Optional[str]
|
||||
title: str
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class MessageResponse(BaseModel):
|
||||
id: str
|
||||
conversation_id: Optional[str]
|
||||
role: str
|
||||
content: str
|
||||
created_at: datetime
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class ConversationWithMessagesResponse(BaseModel):
|
||||
id: str
|
||||
user_id: Optional[str]
|
||||
anonymous_token: Optional[str]
|
||||
bot_id: Optional[str]
|
||||
title: str
|
||||
created_at: datetime
|
||||
updated_at: datetime
|
||||
messages: List[MessageResponse] = []
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class SetBotRequest(BaseModel):
|
||||
bot_id: str
|
||||
|
||||
|
||||
class ChatRequest(BaseModel):
|
||||
message: str
|
||||
|
||||
|
||||
class ChatResponse(BaseModel):
|
||||
response: str
|
||||
thinking: Optional[str] = None
|
||||
strategy_config: Optional[dict] = None
|
||||
success: bool = False
|
||||
warning: Optional[str] = None
|
||||
|
||||
@@ -1,13 +1,49 @@
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from .api import auth, bots, backtest, simulate, config
|
||||
from slowapi import Limiter
|
||||
from slowapi.util import get_remote_address
|
||||
from .api import auth, bots, backtest, simulate, config, ave, conversations
|
||||
from .core.limiter import limiter
|
||||
from .core.database import engine, Base
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Initialize database on startup."""
|
||||
# Import all models to ensure they're registered
|
||||
from .db.models import (
|
||||
User,
|
||||
Bot,
|
||||
BotConversation,
|
||||
Backtest,
|
||||
Simulation,
|
||||
Signal,
|
||||
Conversation,
|
||||
Message,
|
||||
AnonymousUser,
|
||||
)
|
||||
|
||||
# Create tables if they don't exist
|
||||
Base.metadata.create_all(bind=engine)
|
||||
logger.info("Database initialized successfully")
|
||||
|
||||
yield
|
||||
# Cleanup on shutdown if needed
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
title="Randebu Trading Bot API",
|
||||
description="AI-powered trading bot platform API",
|
||||
version="0.1.0",
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
app.state.limiter = limiter
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
@@ -18,9 +54,11 @@ app.add_middleware(
|
||||
|
||||
app.include_router(auth.router, prefix="/api/auth", tags=["auth"])
|
||||
app.include_router(bots.router, prefix="/api/bots", tags=["bots"])
|
||||
app.include_router(conversations.router, tags=["conversations"])
|
||||
app.include_router(backtest.router, prefix="/api", tags=["backtest"])
|
||||
app.include_router(simulate.router, prefix="/api", tags=["simulate"])
|
||||
app.include_router(config.router, prefix="/api/config", tags=["config"])
|
||||
app.include_router(ave.router, prefix="/api/ave", tags=["ave"])
|
||||
|
||||
|
||||
@app.get("/")
|
||||
|
||||
@@ -1,4 +1,29 @@
|
||||
from .crew import CrewAgent
|
||||
from .llm_connector import LLMConnector
|
||||
"""AI Agent module for conversational trading."""
|
||||
|
||||
__all__ = ["CrewAgent", "LLMConnector"]
|
||||
from .agent import ConversationalAgent, get_conversational_agent
|
||||
from .client import MiniMaxClient
|
||||
from .tools import get_tool_registry, TOOL_REGISTRY
|
||||
from .help import (
|
||||
format_tools_list,
|
||||
format_general_help,
|
||||
format_tool_help,
|
||||
format_skill_acknowledgment,
|
||||
)
|
||||
from .crew import TradingCrew, get_trading_crew
|
||||
from .llm_connector import MiniMaxLLM, MiniMaxConnector
|
||||
|
||||
__all__ = [
|
||||
"ConversationalAgent",
|
||||
"get_conversational_agent",
|
||||
"MiniMaxClient",
|
||||
"get_tool_registry",
|
||||
"TOOL_REGISTRY",
|
||||
"format_tools_list",
|
||||
"format_general_help",
|
||||
"format_tool_help",
|
||||
"format_skill_acknowledgment",
|
||||
"TradingCrew",
|
||||
"get_trading_crew",
|
||||
"MiniMaxLLM",
|
||||
"MiniMaxConnector",
|
||||
]
|
||||
|
||||
2926
src/backend/app/services/ai_agent/agent.py
Normal file
2926
src/backend/app/services/ai_agent/agent.py
Normal file
File diff suppressed because it is too large
Load Diff
451
src/backend/app/services/ai_agent/client.py
Normal file
451
src/backend/app/services/ai_agent/client.py
Normal file
@@ -0,0 +1,451 @@
|
||||
"""MiniMax API client for the conversational agent."""
|
||||
|
||||
import requests
|
||||
from typing import Dict, Any, Optional, List
|
||||
|
||||
|
||||
SYSTEM_PROMPT = """You are a helpful AI trading assistant named Randebu. You help users manage their trading bots.
|
||||
|
||||
IMPORTANT CHAIN LIMITATION:
|
||||
- We ONLY support BSC (Binance Smart Chain) blockchain
|
||||
- If user asks about any other chain (Solana, ETH, Base, etc.), respond with: "Currently we only support BSC (Binance Smart Chain). All trading strategies and token searches are performed on BSC."
|
||||
- Never search or recommend tokens on other chains
|
||||
- The search_tokens tool defaults to BSC, never change this
|
||||
|
||||
Your response must be valid JSON with exactly this structure:
|
||||
{
|
||||
"thinking": "Your internal reasoning and analysis (what you're thinking about)",
|
||||
"response": "Your actual response to the user (be concise and helpful)",
|
||||
"strategy_update": null or {
|
||||
"conditions": [{"type": "price_drop" | "price_rise" | "volume_spike" | "price_level", "token": "TOKEN_SYMBOL", "token_address": null, "threshold": number, ...}],
|
||||
"actions": [{"type": "buy" | "sell" | "hold", "amount_percent": number, ...}],
|
||||
"risk_management": {"stop_loss_percent": number, "take_profit_percent": number}
|
||||
}
|
||||
}
|
||||
|
||||
Guidelines:
|
||||
- "thinking" should be detailed reasoning about the user's request
|
||||
- "response" should be conversational and clear
|
||||
- "strategy_update" should be populated ONLY when the user provides specific trading parameters (percentages, tokens, conditions, etc.)
|
||||
- IMPORTANT: When a token is mentioned, set "token_address": null and ask user to confirm the token address before saving. Your response should say something like: "I need to confirm the token address. Could you provide the contract address for [TOKEN]?"
|
||||
- If no strategy parameters are provided, set "strategy_update" to null
|
||||
- Be friendly, concise, and helpful in your response
|
||||
|
||||
Example 1 (no strategy update):
|
||||
User: "What can this bot do?"
|
||||
{
|
||||
"thinking": "The user is asking about the bot's capabilities. I should explain the main features.",
|
||||
"response": "Randebu is your AI trading assistant! It can monitor cryptocurrency prices and execute trades based on your configured strategies. Tell me your trading parameters and I'll set them up for you.",
|
||||
"strategy_update": null
|
||||
}
|
||||
|
||||
Example 2 (token needs confirmation):
|
||||
User: "I want to buy PEPE when it drops 10%"
|
||||
{
|
||||
"thinking": "User wants to buy PEPE. I need the token contract address to proceed. I should ask for confirmation.",
|
||||
"response": "I'd be happy to set up a buy order for PEPE! However, I need to confirm the token contract address. Could you provide the BSC contract address for PEPE? (It usually starts with 0x...)",
|
||||
"strategy_update": {
|
||||
"conditions": [{"type": "price_drop", "token": "PEPE", "token_address": null, "threshold": 10}],
|
||||
"actions": [{"type": "buy", "amount_percent": 100}],
|
||||
"risk_management": null
|
||||
}
|
||||
}
|
||||
|
||||
Example 3 (with token address provided by user):
|
||||
User: "Buy 0x6982508145454Ce125dDE157d8d64a26D53f60a2 when it drops 10%"
|
||||
{
|
||||
"thinking": "User provided a contract address, I can use it directly.",
|
||||
"response": "Perfect! I've configured your strategy to buy the token when it drops 10%.",
|
||||
"strategy_update": {
|
||||
"conditions": [{"type": "price_drop", "token": "TOKEN", "token_address": "0x6982508145454Ce125dDE157d8d64a26D53f60a2", "threshold": 10}],
|
||||
"actions": [{"type": "buy", "amount_percent": 100}],
|
||||
"risk_management": null
|
||||
}
|
||||
}"""
|
||||
|
||||
|
||||
TOOLS = [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "search_tokens",
|
||||
"description": "Search for tokens by keyword on BSC blockchain. Use this when user asks to search for a specific token or find tokens by name/symbol.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"keyword": {
|
||||
"type": "string",
|
||||
"description": "Token symbol or name to search for (e.g., 'PEPE', 'BTC')",
|
||||
},
|
||||
"limit": {
|
||||
"type": "integer",
|
||||
"description": "Number of tokens to return (default: 10)",
|
||||
"default": 10,
|
||||
},
|
||||
},
|
||||
"required": ["keyword"],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_token",
|
||||
"description": "Get detailed information about a specific token including price, market cap, and pairs. Use when user asks for token details or wants to find a specific token by contract address.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"address": {
|
||||
"type": "string",
|
||||
"description": "Token contract address (e.g., '0x6982508145454Ce125dDE157d8d64a26D53f60a2')",
|
||||
},
|
||||
"chain": {
|
||||
"type": "string",
|
||||
"description": "Blockchain chain (default: bsc)",
|
||||
"default": "bsc",
|
||||
},
|
||||
},
|
||||
"required": ["address"],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_price",
|
||||
"description": "Get current price(s) for tokens. Use when user asks for token price or wants to compare prices of multiple tokens.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"token_ids": {
|
||||
"type": "string",
|
||||
"description": "Comma-separated list of token IDs with chain suffix (e.g., 'PEPE-bsc,TRUMP-bsc')",
|
||||
}
|
||||
},
|
||||
"required": ["token_ids"],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_risk",
|
||||
"description": "Get risk analysis for a token contract. Use when user asks about token risk, honeypot analysis, or safety assessment before trading.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"address": {
|
||||
"type": "string",
|
||||
"description": "Token contract address (e.g., '0x6982508145454Ce125dDE157d8d64a26D53f60a2')",
|
||||
},
|
||||
"chain": {
|
||||
"type": "string",
|
||||
"description": "Blockchain chain (default: bsc)",
|
||||
"default": "bsc",
|
||||
},
|
||||
},
|
||||
"required": ["address"],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_trending",
|
||||
"description": "Get trending tokens on a blockchain. Use when user asks what's trending, top tokens, or popular tokens right now.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"chain": {
|
||||
"type": "string",
|
||||
"description": "Blockchain chain (default: bsc)",
|
||||
"default": "bsc",
|
||||
},
|
||||
"limit": {
|
||||
"type": "integer",
|
||||
"description": "Number of trending tokens to return (default: 10, max: 50)",
|
||||
"default": 10,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "run_backtest",
|
||||
"description": "Run a backtest to evaluate how the current trading strategy would have performed historically. Returns key metrics like ROI, win rate, max drawdown, etc. Use this when user asks to backtest, test strategy, or check historical performance.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"token_address": {
|
||||
"type": "string",
|
||||
"description": "The BSC contract address of the token to backtest (required)",
|
||||
},
|
||||
"timeframe": {
|
||||
"type": "string",
|
||||
"description": "Timeframe for klines: '1d' (1 day), '4h' (4 hours), '1h' (1 hour), '15m' (15 minutes)",
|
||||
"default": "1d",
|
||||
},
|
||||
"start_date": {
|
||||
"type": "string",
|
||||
"description": "Start date for backtest in YYYY-MM-DD format (e.g., '2024-01-01')",
|
||||
},
|
||||
"end_date": {
|
||||
"type": "string",
|
||||
"description": "End date for backtest in YYYY-MM-DD format (e.g., '2024-12-01')",
|
||||
},
|
||||
},
|
||||
"required": ["token_address"],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "manage_simulation",
|
||||
"description": "Manage trading simulations: start, stop, or check status. Simulations run on real-time klines and show live portfolio updates. Use when user asks to run simulation, check simulation status, or stop simulation.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"action": {
|
||||
"type": "string",
|
||||
"enum": ["start", "stop", "status", "results"],
|
||||
"description": "Action to perform: 'start' (begin new simulation), 'stop' (stop running simulation), 'status' (check if simulation is running), 'results' (get results from current or latest simulation)",
|
||||
},
|
||||
"token_address": {
|
||||
"type": "string",
|
||||
"description": "Token contract address for simulation (required for 'start' action)",
|
||||
},
|
||||
"kline_interval": {
|
||||
"type": "string",
|
||||
"description": "Kline interval: '1m', '5m', '15m', '1h' (default: '1m')",
|
||||
"default": "1m",
|
||||
},
|
||||
},
|
||||
"required": ["action"],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "create_bot",
|
||||
"description": "Create a new trading bot. Use this when user wants to create a new trading bot. Returns the bot ID and confirmation.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"description": "Name for the new bot (required)",
|
||||
},
|
||||
"strategy": {
|
||||
"type": "string",
|
||||
"description": "Trading strategy description in plain English (optional, can be set later)",
|
||||
},
|
||||
},
|
||||
"required": ["name"],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "list_bots",
|
||||
"description": "List all trading bots owned by the user. Use this when user wants to see their bots or asks which bots they have.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "set_bot",
|
||||
"description": "Set (associate) a bot with the current conversation. Use this when user wants to switch which bot they're working with.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"bot_id": {
|
||||
"type": "string",
|
||||
"description": "ID of the bot to set for this conversation (required)",
|
||||
},
|
||||
},
|
||||
"required": ["bot_id"],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_bot_info",
|
||||
"description": "Get details of a specific bot including name, strategy, and status. Use this when user wants to see details of a bot.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"bot_id": {
|
||||
"type": "string",
|
||||
"description": "ID of the bot to get info for (optional, defaults to current bot)",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "update_strategy",
|
||||
"description": "Update (save) the trading strategy for a bot. This SAVES the strategy to the database. ALWAYS call this tool when user wants to configure or update a trading strategy. The strategy will be persisted and used for backtests.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"strategy": {
|
||||
"type": "string",
|
||||
"description": "Description of the strategy (e.g., 'Dip buying strategy', 'Mean reversion')",
|
||||
},
|
||||
"conditions": {
|
||||
"type": "array",
|
||||
"description": "Array of condition objects. Each condition has: type (price_drop, price_rise, volume_spike, price_level), token, token_address, threshold",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"type": {
|
||||
"type": "string",
|
||||
"enum": ["price_drop", "price_rise", "volume_spike", "price_level"],
|
||||
"description": "Type of condition"
|
||||
},
|
||||
"token": {"type": "string", "description": "Token symbol (e.g., 'SHIB', 'PEPE')"},
|
||||
"token_address": {"type": "string", "description": "Token contract address on BSC (e.g., '0x...') - REQUIRED"},
|
||||
"threshold": {"type": "number", "description": "Threshold value (e.g., 5 for 5% drop/rise)"},
|
||||
},
|
||||
"required": ["type", "token_address", "threshold"]
|
||||
}
|
||||
},
|
||||
"actions": {
|
||||
"type": "array",
|
||||
"description": "Array of action objects. Each action has: type (buy, sell, hold), amount_percent",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"type": {
|
||||
"type": "string",
|
||||
"enum": ["buy", "sell", "hold"],
|
||||
"description": "Type of action"
|
||||
},
|
||||
"amount_percent": {"type": "number", "description": "Percentage of funds to use (e.g., 20 for 20%)"},
|
||||
},
|
||||
"required": ["type", "amount_percent"]
|
||||
}
|
||||
},
|
||||
"stop_loss": {"type": "number", "description": "Stop loss percentage (e.g., 15 for 15% loss)"},
|
||||
"take_profit": {"type": "number", "description": "Take profit percentage (e.g., 20 for 20% gain)"},
|
||||
},
|
||||
"required": ["conditions", "actions"]
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
SYSTEM_PROMPT_WITH_TOOLS = (
|
||||
SYSTEM_PROMPT
|
||||
+ """
|
||||
|
||||
CRITICAL INSTRUCTIONS:
|
||||
1. When user asks to run a backtest or simulation, first check if a bot exists using list_bots.
|
||||
2. If no bot exists, ASK THE USER what they want to name their bot (e.g., "What would you like to name your trading bot?").
|
||||
3. When user provides a bot name (after being asked), call create_bot with that name.
|
||||
4. If a bot exists but has NO STRATEGY SET, the user MUST set up a strategy before running backtests. Ask the user for their trading strategy (e.g., "What token should I monitor?", "What price drop percentage should trigger a buy?", "What percentage of funds should be used?"). Help them configure the strategy.
|
||||
5. IMPORTANT: When configuring a strategy, you MUST call the update_strategy tool with COMPLETE details:
|
||||
- conditions: Array of {type: "price_drop"|"price_rise"|"volume_spike", token: "SYMBOL", token_address: "0x...", threshold: number}
|
||||
- actions: Array of {type: "buy"|"sell", amount_percent: number}
|
||||
- stop_loss: number (e.g., 5 for 5%)
|
||||
- take_profit: number (e.g., 15 for 15%)
|
||||
- ALWAYS include the token_address from search results when configuring strategy!
|
||||
6. IMPORTANT: After executing ANY tool, you MUST incorporate the ACTUAL tool result into your response. Do NOT ignore what the tool returned. If the tool returned an error, you MUST tell the user about the error - do NOT pretend the operation succeeded.
|
||||
7. NEVER tell users about internal tool names like "create_bot", "list_bots", etc. Use natural language instead.
|
||||
8. NEVER say "Let me..." or "I'll..." - IMMEDIATELY call the appropriate tool. If user asks for trending tokens, call get_trending NOW. If user asks for token info, call get_token NOW. Do NOT ask for permission or say you will do something - just do it.
|
||||
9. When asking for information from the user, be specific and actionable (e.g., "What token do you want to backtest?", "What would you like to name your bot?").
|
||||
10. If user asks you to look up/search/find tokens, IMMEDIATELY call search_tokens or get_trending tool. Do not ask follow-up questions first.
|
||||
11. IMPORTANT: When user says they want to use a token from search results (e.g., "that main PEPE" or "the first one"), ALWAYS extract the token_address from the search results and include it in update_strategy. Do NOT ask for the address again!
|
||||
|
||||
You have access to tools:
|
||||
- search_tokens(keyword, limit): Search for tokens by keyword/symbol.
|
||||
- get_token(address, chain): Get detailed token info.
|
||||
- get_price(token_ids): Get current token prices.
|
||||
- get_risk(address, chain): Get risk/honeypot analysis.
|
||||
- get_trending(chain, limit): Get trending tokens.
|
||||
- run_backtest(token_address, timeframe, start_date, end_date): Run backtest. REQUIRES a bot to be set first.
|
||||
- manage_simulation(action, token_address, kline_interval): Manage simulations. REQUIRES a bot.
|
||||
- create_bot(name, strategy): Create a new trading bot.
|
||||
- list_bots(): List user's bots.
|
||||
- set_bot(bot_id): Switch bots in conversation.
|
||||
- get_bot_info(bot_id): Get bot details including strategy conditions and actions.
|
||||
- update_strategy(conditions, actions, stop_loss, take_profit): Save trading strategy to bot. MUST include token_address in conditions!
|
||||
|
||||
Take action immediately. Do not ask for confirmation. Do not describe what you will do - just do it.
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
class MiniMaxClient:
|
||||
"""Client for MiniMax extended thinking API."""
|
||||
|
||||
def __init__(self, api_key: str, model: str = "MiniMax-M2.7"):
|
||||
self.api_key = api_key
|
||||
self.model = model
|
||||
self.endpoint = "https://api.minimax.io/v1/text/chatcompletion_v2"
|
||||
|
||||
def chat(
|
||||
self,
|
||||
messages: List[Dict[str, str]],
|
||||
system_prompt: str,
|
||||
tools: Optional[List[Dict[str, Any]]] = None,
|
||||
temperature: float = 0.7,
|
||||
max_tokens: int = 3000,
|
||||
thinking_budget: int = 1500,
|
||||
) -> Dict[str, Any]:
|
||||
"""Send a chat request to MiniMax API."""
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
all_messages = [{"role": "system", "content": system_prompt}] + messages
|
||||
|
||||
payload = {
|
||||
"model": self.model,
|
||||
"messages": all_messages,
|
||||
"temperature": temperature,
|
||||
"max_tokens": max_tokens,
|
||||
"thinking": {"type": "human", "budget_tokens": thinking_budget},
|
||||
}
|
||||
|
||||
if tools:
|
||||
payload["tools"] = tools
|
||||
|
||||
resp = requests.post(self.endpoint, headers=headers, json=payload)
|
||||
|
||||
# Check for HTTP errors
|
||||
if resp.status_code != 200:
|
||||
error_text = resp.text
|
||||
print(f"API Error {resp.status_code}: {error_text[:500]}")
|
||||
return {"error": f"API returned {resp.status_code}: {error_text[:200]}"}
|
||||
|
||||
return resp.json() or {}
|
||||
|
||||
def check_connection(self) -> bool:
|
||||
"""Check if API is reachable."""
|
||||
try:
|
||||
resp = requests.post(
|
||||
self.endpoint,
|
||||
headers={"Authorization": f"Bearer {self.api_key}"},
|
||||
json={
|
||||
"model": self.model,
|
||||
"messages": [{"role": "user", "content": "ping"}],
|
||||
},
|
||||
timeout=10,
|
||||
)
|
||||
return resp.status_code == 200
|
||||
except Exception:
|
||||
return False
|
||||
@@ -1,15 +1,241 @@
|
||||
from typing import List, Optional
|
||||
from typing import List, Optional, Dict, Any
|
||||
from crewai import Agent, Task, Crew, LLM
|
||||
from .llm_connector import MiniMaxConnector
|
||||
from ...core.config import get_settings
|
||||
|
||||
|
||||
class CrewAgent:
|
||||
def __init__(self, role: str, goal: str, backstory: str):
|
||||
self.role = role
|
||||
self.goal = goal
|
||||
self.backstory = backstory
|
||||
class StrategyValidator:
|
||||
SUPPORTED_CONDITIONS = ["price_drop", "price_rise", "volume_spike", "price_level"]
|
||||
SUPPORTED_ACTIONS = ["buy", "sell", "notify"]
|
||||
|
||||
def execute_task(self, task: str) -> str:
|
||||
raise NotImplementedError("CrewAI agent not yet implemented")
|
||||
def validate(self, strategy_config: dict) -> tuple[bool, list[str]]:
|
||||
errors = []
|
||||
|
||||
if "conditions" not in strategy_config:
|
||||
errors.append("Missing 'conditions' in strategy config")
|
||||
return False, errors
|
||||
|
||||
if not isinstance(strategy_config["conditions"], list):
|
||||
errors.append("'conditions' must be a list")
|
||||
return False, errors
|
||||
|
||||
if len(strategy_config["conditions"]) == 0:
|
||||
errors.append("At least one condition is required")
|
||||
return False, errors
|
||||
|
||||
for i, condition in enumerate(strategy_config["conditions"]):
|
||||
if "type" not in condition:
|
||||
errors.append(f"Condition {i}: missing 'type'")
|
||||
continue
|
||||
|
||||
cond_type = condition.get("type")
|
||||
if cond_type not in self.SUPPORTED_CONDITIONS:
|
||||
errors.append(f"Condition {i}: unsupported type '{cond_type}'")
|
||||
continue
|
||||
|
||||
if cond_type in ["price_drop", "price_rise", "volume_spike"]:
|
||||
if "token" not in condition:
|
||||
errors.append(f"Condition {i}: missing 'token'")
|
||||
if "threshold" not in condition:
|
||||
errors.append(f"Condition {i}: missing 'threshold'")
|
||||
elif not isinstance(condition["threshold"], (int, float)):
|
||||
errors.append(f"Condition {i}: 'threshold' must be a number")
|
||||
elif condition["threshold"] <= 0:
|
||||
errors.append(f"Condition {i}: 'threshold' must be positive")
|
||||
|
||||
elif cond_type == "price_level":
|
||||
if "token" not in condition:
|
||||
errors.append(f"Condition {i}: missing 'token'")
|
||||
if "price" not in condition:
|
||||
errors.append(f"Condition {i}: missing 'price'")
|
||||
if "direction" not in condition:
|
||||
errors.append(f"Condition {i}: missing 'direction'")
|
||||
elif condition["direction"] not in ["above", "below"]:
|
||||
errors.append(
|
||||
f"Condition {i}: direction must be 'above' or 'below'"
|
||||
)
|
||||
|
||||
if "actions" in strategy_config:
|
||||
if not isinstance(strategy_config["actions"], list):
|
||||
errors.append("'actions' must be a list")
|
||||
else:
|
||||
for i, action in enumerate(strategy_config["actions"]):
|
||||
if "type" not in action:
|
||||
errors.append(f"Action {i}: missing 'type'")
|
||||
elif action["type"] not in self.SUPPORTED_ACTIONS:
|
||||
errors.append(
|
||||
f"Action {i}: unsupported type '{action['type']}'"
|
||||
)
|
||||
|
||||
return len(errors) == 0, errors
|
||||
|
||||
|
||||
def get_trading_crew():
|
||||
raise NotImplementedError("Trading crew not yet implemented")
|
||||
class StrategyExplainer:
|
||||
def explain(self, strategy_config: dict) -> str:
|
||||
explanations = []
|
||||
|
||||
if "conditions" in strategy_config:
|
||||
cond_list = strategy_config["conditions"]
|
||||
if cond_list:
|
||||
explanations.append("This strategy will trigger when:")
|
||||
for cond in cond_list:
|
||||
cond_type = cond.get("type")
|
||||
token = cond.get("token", "the token")
|
||||
|
||||
if cond_type == "price_drop":
|
||||
pct = cond.get("threshold", 0)
|
||||
explanations.append(f" - {token} price drops by {pct}%")
|
||||
elif cond_type == "price_rise":
|
||||
pct = cond.get("threshold", 0)
|
||||
explanations.append(f" - {token} price rises by {pct}%")
|
||||
elif cond_type == "volume_spike":
|
||||
pct = cond.get("threshold", 0)
|
||||
explanations.append(
|
||||
f" - {token} trading volume increases by {pct}%"
|
||||
)
|
||||
elif cond_type == "price_level":
|
||||
price = cond.get("price", 0)
|
||||
direction = cond.get("direction", "unknown")
|
||||
explanations.append(
|
||||
f" - {token} price crosses {direction} ${price}"
|
||||
)
|
||||
|
||||
if "actions" in strategy_config:
|
||||
actions = strategy_config.get("actions", [])
|
||||
if actions:
|
||||
explanations.append("\nWhen triggered, the strategy will:")
|
||||
for action in actions:
|
||||
action_type = action.get("type")
|
||||
if action_type == "buy":
|
||||
explanations.append(" - Buy the token")
|
||||
elif action_type == "sell":
|
||||
explanations.append(" - Sell the token")
|
||||
elif action_type == "notify":
|
||||
explanations.append(" - Send a notification")
|
||||
|
||||
if not explanations:
|
||||
explanations.append("Strategy configuration is empty or invalid.")
|
||||
|
||||
return "\n".join(explanations)
|
||||
|
||||
|
||||
def create_trading_designer_agent(
|
||||
api_key: str, model: str = "MiniMax-M2.7"
|
||||
) -> Agent:
|
||||
connector = MiniMaxConnector(api_key=api_key, model=model)
|
||||
|
||||
system_prompt = """You are a Trading Strategy Designer AI. Your role is to parse user requests
|
||||
for trading strategies into structured JSON configuration.
|
||||
|
||||
Supported conditions (MVP):
|
||||
- price_drop: Triggers when a token's price drops by a specified percentage
|
||||
- price_rise: Triggers when a token's price rises by a specified percentage
|
||||
- volume_spike: Triggers when trading volume increases by a specified percentage
|
||||
- price_level: Triggers when price crosses above or below a specified level
|
||||
|
||||
Always ask clarifying questions if the user's request is ambiguous.
|
||||
Output strategy_config in valid JSON format only when you have all required information.
|
||||
"""
|
||||
|
||||
return Agent(
|
||||
role="Trading Strategy Designer",
|
||||
goal="Convert natural language trading requests into precise strategy configurations",
|
||||
backstory=system_prompt,
|
||||
llm=LLM(model=model, api_key=api_key, api_base="https://api.minimax.io/v1"),
|
||||
verbose=True,
|
||||
)
|
||||
|
||||
|
||||
def create_strategy_validator_agent(
|
||||
api_key: str, model: str = "MiniMax-M2.7"
|
||||
) -> Agent:
|
||||
return Agent(
|
||||
role="Strategy Validator",
|
||||
goal="Validate trading strategy configurations for feasibility and identify potential issues",
|
||||
backstory="""You are a meticulous strategy validator with expertise in trading systems.
|
||||
You check that all required parameters are present, values are reasonable, and the
|
||||
strategy makes logical sense. You never approve strategies with missing or invalid data.""",
|
||||
llm=LLM(model=model, api_key=api_key, api_base="https://api.minimax.io/v1"),
|
||||
verbose=True,
|
||||
)
|
||||
|
||||
|
||||
def create_strategy_explainer_agent(
|
||||
api_key: str, model: str = "MiniMax-M2.7"
|
||||
) -> Agent:
|
||||
return Agent(
|
||||
role="Strategy Explainer",
|
||||
goal="Generate clear, user-friendly explanations of trading strategies",
|
||||
backstory="""You are a patient trading strategy explainer. You translate complex
|
||||
strategy configurations into easy-to-understand language. You help users understand
|
||||
exactly what their strategies will do when triggered.""",
|
||||
llm=LLM(model=model, api_key=api_key, api_base="https://api.minimax.io/v1"),
|
||||
verbose=True,
|
||||
)
|
||||
|
||||
|
||||
class TradingCrew:
|
||||
def __init__(self, api_key: str, model: str = "MiniMax-M2.7"):
|
||||
self.api_key = api_key
|
||||
self.model = model
|
||||
self.validator = StrategyValidator()
|
||||
self.explainer = StrategyExplainer()
|
||||
self.connector = MiniMaxConnector(api_key=api_key, model=model)
|
||||
|
||||
def parse_strategy(
|
||||
self, user_message: str, conversation_history: list[dict] = None
|
||||
) -> dict:
|
||||
strategy_config = self.connector.parse_strategy(
|
||||
user_message, conversation_history
|
||||
)
|
||||
|
||||
if "error" in strategy_config:
|
||||
return strategy_config
|
||||
|
||||
is_valid, errors = self.validator.validate(strategy_config)
|
||||
if not is_valid:
|
||||
return {
|
||||
"error": "Strategy validation failed",
|
||||
"validation_errors": errors,
|
||||
"partial_config": strategy_config,
|
||||
}
|
||||
|
||||
return strategy_config
|
||||
|
||||
def explain_strategy(self, strategy_config: dict) -> str:
|
||||
return self.explainer.explain(strategy_config)
|
||||
|
||||
def chat(self, user_message: str, conversation_history: list[dict] = None) -> dict:
|
||||
strategy_config = self.parse_strategy(user_message, conversation_history)
|
||||
|
||||
if "error" in strategy_config:
|
||||
explanation = f"I had trouble understanding your strategy: {strategy_config.get('error', 'Unknown error')}"
|
||||
if "validation_errors" in strategy_config:
|
||||
explanation += "\n\nValidation issues:"
|
||||
for err in strategy_config["validation_errors"]:
|
||||
explanation += f"\n - {err}"
|
||||
return {
|
||||
"response": explanation,
|
||||
"strategy_config": strategy_config.get("partial_config"),
|
||||
"success": False,
|
||||
}
|
||||
|
||||
explanation = self.explain_strategy(strategy_config)
|
||||
return {
|
||||
"response": f"I've configured your strategy:\n\n{explanation}",
|
||||
"strategy_config": strategy_config,
|
||||
"success": True,
|
||||
}
|
||||
|
||||
|
||||
def get_trading_crew(
|
||||
api_key: Optional[str] = None, model: Optional[str] = None
|
||||
) -> TradingCrew:
|
||||
if api_key is None:
|
||||
settings = get_settings()
|
||||
api_key = settings.MINIMAX_API_KEY
|
||||
if model is None:
|
||||
settings = get_settings()
|
||||
model = settings.MINIMAX_MODEL
|
||||
|
||||
return TradingCrew(api_key=api_key, model=model)
|
||||
|
||||
83
src/backend/app/services/ai_agent/help.py
Normal file
83
src/backend/app/services/ai_agent/help.py
Normal file
@@ -0,0 +1,83 @@
|
||||
"""Help formatters for slash commands and tool documentation."""
|
||||
|
||||
from typing import Optional
|
||||
from .tools import get_tool_registry, SKILL_EMOJIS
|
||||
|
||||
|
||||
def format_tools_list() -> str:
|
||||
"""Format the tool registry as a help message."""
|
||||
message = "📋 Available Tools\n\n"
|
||||
|
||||
for category in ["randebu", "ave"]:
|
||||
tools = get_tool_registry().get(category, [])
|
||||
if category == "randebu":
|
||||
message += "🤖 Randebu Built-in:\n"
|
||||
else:
|
||||
message += "☁️ AVE Cloud Skills:\n"
|
||||
|
||||
for tool in tools:
|
||||
message += f" • {tool['command']} - {tool['description']}\n"
|
||||
message += "\n"
|
||||
|
||||
message = (
|
||||
message.rstrip() + "\n\nType /<tool-name> for detailed help on a specific tool."
|
||||
)
|
||||
return message
|
||||
|
||||
|
||||
def format_skill_acknowledgment(tool_name: str, description: str) -> str:
|
||||
"""Format a brief acknowledgment when a skill is activated."""
|
||||
emoji = SKILL_EMOJIS.get(tool_name.lower(), "✨")
|
||||
return f"{emoji} **{tool_name}** loaded. Ready for *{description}*, ask me away!"
|
||||
|
||||
|
||||
def format_tool_help(tool_name: str) -> str:
|
||||
"""Format detailed help for a specific tool."""
|
||||
tool_name = tool_name.lstrip("/")
|
||||
|
||||
for category in ["randebu", "ave"]:
|
||||
for tool in get_tool_registry().get(category, []):
|
||||
if tool["name"].lower() == tool_name.lower():
|
||||
cat_label = (
|
||||
"Randebu Built-in" if category == "randebu" else "AVE Cloud Skill"
|
||||
)
|
||||
details = tool["details"]
|
||||
message = (
|
||||
f"🔍 {tool['command']} - {details['description']} ({cat_label})\n\n"
|
||||
)
|
||||
message += f"**Description:** {details['description']}\n"
|
||||
message += f"**Commands:**\n {details['usage']}\n\n"
|
||||
message += f"**Example:**\n```\n{details['example']}\n```"
|
||||
return message
|
||||
|
||||
return f"Tool '{tool_name}' not found. Type / to see all available tools."
|
||||
|
||||
|
||||
def format_general_help() -> str:
|
||||
"""Format general help about Randebu."""
|
||||
return """🤖 **Randebu - AI Trading Assistant**
|
||||
|
||||
Randebu is your AI trading assistant that helps you manage your trading bots on BSC (Binance Smart Chain).
|
||||
|
||||
**Getting Started:**
|
||||
1. Create a bot on the dashboard
|
||||
2. Describe your trading strategy in plain English
|
||||
3. Run backtests to validate your strategy
|
||||
4. Start simulations to see live trading
|
||||
|
||||
**Example Strategies:**
|
||||
- "Buy PEPE when it drops 5%"
|
||||
- "Sell if price rises 10% within 1 hour"
|
||||
- "Buy when volume spikes by 200%"
|
||||
|
||||
**Slash Commands:**
|
||||
- `/` - Show all available tools
|
||||
- `/help` - Show this help message
|
||||
- `/<tool-name>` - Get help on a specific tool
|
||||
|
||||
**Natural Language:**
|
||||
You can also just describe what you want in natural language. For example:
|
||||
- "What's the price of PEPE?"
|
||||
- "Run a backtest on 0x... token"
|
||||
- "Start a simulation on TRUMP"
|
||||
"""
|
||||
@@ -1,13 +1,105 @@
|
||||
from typing import Optional
|
||||
from typing import Optional, List, Dict, Any
|
||||
import httpx
|
||||
|
||||
|
||||
class LLMConnector:
|
||||
def __init__(self, api_key: str, model: str = "MiniMax-Text-01"):
|
||||
class MiniMaxLLM:
|
||||
def __init__(self, api_key: str, model: str = "MiniMax-M2.7", **kwargs):
|
||||
self.api_key = api_key
|
||||
self.model = model
|
||||
self.base_url = "https://api.minimax.io/v1"
|
||||
|
||||
def _call(self, messages: List[Dict[str, str]], **kwargs) -> str:
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
payload = {
|
||||
"model": self.model,
|
||||
"messages": messages,
|
||||
"temperature": kwargs.get("temperature", 0.7),
|
||||
"max_tokens": kwargs.get("max_tokens", 2048),
|
||||
}
|
||||
with httpx.Client(timeout=60.0) as client:
|
||||
response = client.post(
|
||||
f"{self.base_url}/text/chatcompletion_v2",
|
||||
headers=headers,
|
||||
json=payload,
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()["choices"][0]["message"]["content"]
|
||||
|
||||
def call(self, messages: List[Dict[str, str]], **kwargs) -> str:
|
||||
return self._call(messages, **kwargs)
|
||||
|
||||
|
||||
class MiniMaxConnector:
|
||||
def __init__(self, api_key: str, model: str = "MiniMax-M2.7"):
|
||||
self.api_key = api_key
|
||||
self.model = model
|
||||
|
||||
def chat(self, messages: list[dict], **kwargs):
|
||||
raise NotImplementedError("LLM integration not yet implemented")
|
||||
def chat(self, messages: list[dict], **kwargs) -> str:
|
||||
formatted_messages = []
|
||||
for msg in messages:
|
||||
if isinstance(msg, dict):
|
||||
formatted_messages.append(
|
||||
{
|
||||
"role": msg.get("role", "user"),
|
||||
"content": msg.get("content", str(msg)),
|
||||
}
|
||||
)
|
||||
else:
|
||||
formatted_messages.append({"role": "user", "content": str(msg)})
|
||||
|
||||
def parse_strategy(self, user_message: str) -> dict:
|
||||
raise NotImplementedError("Strategy parsing not yet implemented")
|
||||
llm = MiniMaxLLM(api_key=self.api_key, model=self.model)
|
||||
return llm.call(formatted_messages, **kwargs)
|
||||
|
||||
def parse_strategy(
|
||||
self, user_message: str, conversation_history: list[dict] = None
|
||||
) -> dict:
|
||||
system_prompt = """You are a trading strategy designer. Parse the user's natural language request into a JSON strategy_config object.
|
||||
|
||||
Supported conditions (MVP):
|
||||
- price_drop: Token price drops by X% (requires: token, threshold)
|
||||
- price_rise: Token price rises by X% (requires: token, threshold)
|
||||
- volume_spike: Trading volume increases X% (requires: token, threshold)
|
||||
- price_level: Price crosses above/below X (requires: token, price, direction)
|
||||
|
||||
Output ONLY valid JSON with this schema:
|
||||
{
|
||||
"conditions": [
|
||||
{
|
||||
"type": "price_drop|price_rise|volume_spike|price_level",
|
||||
"token": "TOKEN_SYMBOL",
|
||||
"chain": "bsc",
|
||||
"threshold": number, // for price_drop, price_rise, volume_spike
|
||||
"price": number, // for price_level
|
||||
"direction": "above|below", // for price_level
|
||||
"timeframe": "1h"
|
||||
}
|
||||
],
|
||||
"actions": [
|
||||
{
|
||||
"type": "buy|sell|notify"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
If the user wants a condition not in the supported list, ask for clarification.
|
||||
"""
|
||||
|
||||
messages = [{"role": "system", "content": system_prompt}]
|
||||
if conversation_history:
|
||||
for msg in conversation_history:
|
||||
messages.append(
|
||||
{"role": msg.get("role", "user"), "content": msg.get("content", "")}
|
||||
)
|
||||
messages.append({"role": "user", "content": user_message})
|
||||
|
||||
response = self.chat(messages)
|
||||
try:
|
||||
import json
|
||||
|
||||
result = json.loads(response)
|
||||
return result
|
||||
except json.JSONDecodeError:
|
||||
return {"error": "Failed to parse strategy", "raw_response": response}
|
||||
|
||||
164
src/backend/app/services/ai_agent/mock_client.py
Normal file
164
src/backend/app/services/ai_agent/mock_client.py
Normal file
@@ -0,0 +1,164 @@
|
||||
"""Mock client for testing the ConversationalAgent without hitting real APIs."""
|
||||
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
|
||||
class MockMiniMaxClient:
|
||||
"""Mock client that returns predefined responses for testing.
|
||||
|
||||
Usage:
|
||||
mock = MockMiniMaxClient()
|
||||
mock.add_response({\"choices\": [...]}) # Add responses in order
|
||||
mock.add_response({\"choices\": [...]}) # Second call gets this
|
||||
|
||||
agent = ConversationalAgent(client=mock)
|
||||
result = agent.chat(\"hello\")
|
||||
"""
|
||||
|
||||
def __init__(self, responses: List[Dict[str, Any]] = None):
|
||||
self.responses = responses or []
|
||||
self.call_count = 0
|
||||
self.calls: List[Dict[str, Any]] = [] # Record all calls for assertions
|
||||
|
||||
def add_response(self, response: Dict[str, Any]):
|
||||
"""Add a response to be returned on the next call."""
|
||||
self.responses.append(response)
|
||||
|
||||
def chat(
|
||||
self,
|
||||
messages: List[Dict[str, str]],
|
||||
system_prompt: str,
|
||||
tools: Optional[List[Dict[str, Any]]] = None,
|
||||
temperature: float = 0.7,
|
||||
max_tokens: int = 2000,
|
||||
thinking_budget: int = 1500,
|
||||
) -> Dict[str, Any]:
|
||||
"""Return the next predefined response."""
|
||||
# Record the call for debugging/tests
|
||||
self.calls.append({
|
||||
"messages": messages,
|
||||
"system_prompt": system_prompt[:100] if system_prompt else None,
|
||||
"tool_calls_count": len(tools) if tools else 0,
|
||||
})
|
||||
|
||||
if self.call_count < len(self.responses):
|
||||
response = self.responses[self.call_count]
|
||||
self.call_count += 1
|
||||
return response
|
||||
|
||||
# Default response if no more predefined responses
|
||||
return {
|
||||
"choices": [{
|
||||
"message": {
|
||||
"content": "Mock response - no more responses configured",
|
||||
"role": "assistant",
|
||||
}
|
||||
}]
|
||||
}
|
||||
|
||||
def reset(self):
|
||||
"""Reset call count and calls list."""
|
||||
self.call_count = 0
|
||||
self.calls = []
|
||||
|
||||
def verify_call(self, call_index: int, expected_messages: int = None, expected_tool_count: int = None):
|
||||
"""Verify a specific call was made correctly."""
|
||||
if call_index >= len(self.calls):
|
||||
raise AssertionError(f"Call {call_index} was not made. Total calls: {len(self.calls)}")
|
||||
|
||||
call = self.calls[call_index]
|
||||
if expected_messages is not None:
|
||||
actual = len(call["messages"])
|
||||
if actual != expected_messages:
|
||||
raise AssertionError(
|
||||
f"Call {call_index}: expected {expected_messages} messages, got {actual}"
|
||||
)
|
||||
if expected_tool_count is not None:
|
||||
actual = call["tool_calls_count"]
|
||||
if actual != expected_tool_count:
|
||||
raise AssertionError(
|
||||
f"Call {call_index}: expected {expected_tool_count} tools, got {actual}"
|
||||
)
|
||||
|
||||
|
||||
class MockMiniMaxClientWithToolCall(MockMiniMaxClient):
|
||||
"""Mock client that generates tool call responses based on message content."""
|
||||
|
||||
def __init__(self, tool_handlers: Dict[str, Dict[str, Any]] = None):
|
||||
"""tool_handlers: dict mapping tool names to their responses."""
|
||||
super().__init__()
|
||||
self.tool_handlers = tool_handlers or {}
|
||||
|
||||
def chat(
|
||||
self,
|
||||
messages: List[Dict[str, str]],
|
||||
system_prompt: str,
|
||||
tools: Optional[List[Dict[str, Any]]] = None,
|
||||
temperature: float = 0.7,
|
||||
max_tokens: int = 2000,
|
||||
thinking_budget: int = 1500,
|
||||
) -> Dict[str, Any]:
|
||||
"""Check if last message contains a tool call request and return appropriate response."""
|
||||
self.calls.append({
|
||||
"messages": messages,
|
||||
"system_prompt": system_prompt[:100] if system_prompt else None,
|
||||
"tool_calls_count": len(tools) if tools else 0,
|
||||
})
|
||||
|
||||
# Get the last message
|
||||
if not messages:
|
||||
return {"choices": [{"message": {"content": "No messages", "role": "assistant"}}]}
|
||||
|
||||
last_msg = messages[-1]
|
||||
|
||||
# If it's a tool result, look for the tool that was called
|
||||
if last_msg.get("role") == "user" and "[TOOL RESULT]" in last_msg.get("content", ""):
|
||||
# Extract tool name from the content
|
||||
content = last_msg["content"]
|
||||
# Format: [TOOL RESULT] tool_name: result
|
||||
for tool_name in self.tool_handlers:
|
||||
if f"[TOOL RESULT] {tool_name}:" in content:
|
||||
return self.tool_handlers[tool_name]
|
||||
|
||||
# Check if we should generate a tool call
|
||||
last_user_msg = ""
|
||||
for msg in reversed(messages):
|
||||
if msg.get("role") == "user" and "[TOOL RESULT]" not in msg.get("content", ""):
|
||||
last_user_msg = msg.get("content", "")
|
||||
break
|
||||
|
||||
# Check each tool's trigger
|
||||
for tool_name, handler in self.tool_handlers.items():
|
||||
trigger = handler.get("trigger", "")
|
||||
if trigger and trigger.lower() in last_user_msg.lower():
|
||||
return {
|
||||
"choices": [{
|
||||
"message": {
|
||||
"content": handler.get("content", ""),
|
||||
"role": "assistant",
|
||||
"tool_calls": [{
|
||||
"id": f"call_{tool_name}",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": tool_name,
|
||||
"arguments": handler.get("arguments", "{}")
|
||||
}
|
||||
}]
|
||||
}
|
||||
}]
|
||||
}
|
||||
|
||||
# Default: return first available response or empty
|
||||
if self.responses:
|
||||
response = self.responses[0]
|
||||
self.call_count += 1
|
||||
return response
|
||||
|
||||
return {
|
||||
"choices": [{
|
||||
"message": {
|
||||
"content": "How can I help you with your trading bot?",
|
||||
"role": "assistant",
|
||||
}
|
||||
}]
|
||||
}
|
||||
172
src/backend/app/services/ai_agent/tools.py
Normal file
172
src/backend/app/services/ai_agent/tools.py
Normal file
@@ -0,0 +1,172 @@
|
||||
"""Tool registry and definitions for the conversational agent."""
|
||||
|
||||
from typing import Dict, Any, List
|
||||
|
||||
TOOL_REGISTRY: Dict[str, Any] = {
|
||||
"randebu": [
|
||||
{
|
||||
"name": "backtest",
|
||||
"description": "Run strategy backtest",
|
||||
"category": "Randebu Built-in",
|
||||
"command": "/backtest",
|
||||
"details": {
|
||||
"description": "Run a backtest to evaluate how the current trading strategy would have performed historically.",
|
||||
"usage": "/backtest [token_address] [--timeframe 1d|4h|1h|15m] [--start YYYY-MM-DD] [--end YYYY-MM-DD]",
|
||||
"example": "Run a backtest on PEPE for the last 30 days",
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "simulate",
|
||||
"description": "Start/stop simulation",
|
||||
"category": "Randebu Built-in",
|
||||
"command": "/simulate",
|
||||
"details": {
|
||||
"description": "Start or stop trading simulations that run on real-time klines.",
|
||||
"usage": "/simulate start|stop|status|results [token_address]",
|
||||
"example": "Start a simulation on PEPE",
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "strategy",
|
||||
"description": "View/update strategy",
|
||||
"category": "Randebu Built-in",
|
||||
"command": "/strategy",
|
||||
"details": {
|
||||
"description": "View your current trading strategy or update it with new parameters.",
|
||||
"usage": "Describe your strategy in plain English, e.g., 'Buy PEPE when price drops 5%'",
|
||||
"example": "Buy PEPE when it drops 10% within 1 hour",
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "create_bot",
|
||||
"description": "Create a new trading bot",
|
||||
"category": "Randebu Built-in",
|
||||
"command": None,
|
||||
"details": {
|
||||
"description": "Create a new trading bot linked to the current conversation.",
|
||||
"usage": "create_bot <name> [--strategy <strategy_desc>]",
|
||||
"example": "create_bot MyBot --strategy Buy PEPE when it drops 5%",
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "list_bots",
|
||||
"description": "List your trading bots",
|
||||
"category": "Randebu Built-in",
|
||||
"command": None,
|
||||
"details": {
|
||||
"description": "List all trading bots you own.",
|
||||
"usage": "list_bots",
|
||||
"example": "list_bots",
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "set_bot",
|
||||
"description": "Set bot for this conversation",
|
||||
"category": "Randebu Built-in",
|
||||
"command": None,
|
||||
"details": {
|
||||
"description": "Associate a bot with the current conversation.",
|
||||
"usage": "set_bot <bot_id>",
|
||||
"example": "set_bot abc-123-def",
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "get_bot_info",
|
||||
"description": "Get current bot details",
|
||||
"category": "Randebu Built-in",
|
||||
"command": None,
|
||||
"details": {
|
||||
"description": "Get details of the current bot for display in the right pane.",
|
||||
"usage": "get_bot_info [bot_id]",
|
||||
"example": "get_bot_info abc-123-def",
|
||||
},
|
||||
},
|
||||
],
|
||||
"ave": [
|
||||
{
|
||||
"name": "search",
|
||||
"description": "Token search",
|
||||
"category": "AVE Cloud Skills",
|
||||
"command": "/search",
|
||||
"details": {
|
||||
"description": "Find tokens by keyword, symbol, or contract address on BSC.",
|
||||
"usage": "search <keyword> [--chain bsc] [--limit 20]",
|
||||
"example": "search PEPE\nsearch 0x1234... --chain bsc",
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "trending",
|
||||
"description": "Popular tokens",
|
||||
"category": "AVE Cloud Skills",
|
||||
"command": "/trending",
|
||||
"details": {
|
||||
"description": "Get list of trending/popular tokens on BSC.",
|
||||
"usage": "trending [--chain bsc] [--limit 20]",
|
||||
"example": "trending --chain bsc\ntrending --limit 10",
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "risk",
|
||||
"description": "Honeypot detection",
|
||||
"category": "AVE Cloud Skills",
|
||||
"command": "/risk",
|
||||
"details": {
|
||||
"description": "Get risk analysis for a token contract including honeypot assessment.",
|
||||
"usage": "risk <token_address> [--chain bsc]",
|
||||
"example": "risk 0x6982508145454Ce125dDE157d8d64a26D53f60a2",
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "token",
|
||||
"description": "Token details",
|
||||
"category": "AVE Cloud Skills",
|
||||
"command": "/token",
|
||||
"details": {
|
||||
"description": "Get detailed information about a specific token including price, market cap, and pairs.",
|
||||
"usage": "token <address> [--chain bsc]",
|
||||
"example": "token 0x6982508145454Ce125dDE157d8d64a26D53f60a2",
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "price",
|
||||
"description": "Batch prices",
|
||||
"category": "AVE Cloud Skills",
|
||||
"command": "/price",
|
||||
"details": {
|
||||
"description": "Get current price(s) for multiple tokens.",
|
||||
"usage": "price <token_id>,<token_id>,... (e.g., PEPE-bsc,TRUMP-bsc)",
|
||||
"example": "price PEPE-bsc,TRUMP-bsc",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
SKILL_EMOJIS: Dict[str, str] = {
|
||||
"backtest": "📊",
|
||||
"simulate": "🎮",
|
||||
"strategy": "📝",
|
||||
"search": "🔍",
|
||||
"trending": "📈",
|
||||
"risk": "📉",
|
||||
"token": "🪙",
|
||||
"price": "💰",
|
||||
}
|
||||
|
||||
|
||||
def get_tool_registry() -> Dict[str, Any]:
|
||||
"""Return the tool registry for slash command help."""
|
||||
return TOOL_REGISTRY
|
||||
|
||||
|
||||
def get_tools_by_category(category: str) -> List[Dict[str, Any]]:
|
||||
"""Get tools filtered by category."""
|
||||
return TOOL_REGISTRY.get(category, [])
|
||||
|
||||
|
||||
def get_tool_by_name(tool_name: str) -> Dict[str, Any]:
|
||||
"""Get a tool by its name."""
|
||||
for category in ["randebu", "ave"]:
|
||||
for tool in TOOL_REGISTRY.get(category, []):
|
||||
if tool["name"].lower() == tool_name.lower():
|
||||
return tool
|
||||
return None
|
||||
3
src/backend/app/services/ave/__init__.py
Normal file
3
src/backend/app/services/ave/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .client import AveCloudClient, check_tier_access
|
||||
|
||||
__all__ = ["AveCloudClient", "check_tier_access"]
|
||||
243
src/backend/app/services/ave/client.py
Normal file
243
src/backend/app/services/ave/client.py
Normal file
@@ -0,0 +1,243 @@
|
||||
import httpx
|
||||
from typing import List, Dict, Any, Optional
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class AveCloudClient:
|
||||
DATA_API_URL = "https://prod.ave-api.com"
|
||||
TRADING_API_URL = "https://bot-api.ave.ai"
|
||||
|
||||
def __init__(self, api_key: str, plan: str = "free"):
|
||||
self.api_key = api_key
|
||||
self.plan = plan
|
||||
|
||||
def _data_headers(self) -> Dict[str, str]:
|
||||
return {"X-API-KEY": self.api_key}
|
||||
|
||||
def _trading_headers(self) -> Dict[str, str]:
|
||||
return {"X-API-KEY": self.api_key, "Content-Type": "application/json"}
|
||||
|
||||
async def get_tokens(
|
||||
self,
|
||||
query: Optional[str] = None,
|
||||
chain: Optional[str] = None,
|
||||
limit: int = 20,
|
||||
) -> List[Dict[str, Any]]:
|
||||
# Use trending endpoint which supports chain filter
|
||||
url = f"{self.DATA_API_URL}/v2/tokens/trending"
|
||||
params = {"limit": min(limit, 100)} # API returns max 100
|
||||
if chain:
|
||||
params["chain"] = chain
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(
|
||||
url, headers=self._data_headers(), params=params, timeout=30.0
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
if data.get("status") == 1: # 1 = SUCCESS
|
||||
tokens = data.get("data", {}).get("tokens", [])
|
||||
# Filter by query if provided
|
||||
if query:
|
||||
query_lower = query.lower()
|
||||
tokens = [
|
||||
t for t in tokens
|
||||
if query_lower in t.get("symbol", "").lower()
|
||||
or query_lower in t.get("name", "").lower()
|
||||
]
|
||||
return tokens[:limit]
|
||||
return []
|
||||
raise Exception(f"Failed to fetch tokens: {data}")
|
||||
|
||||
async def get_batch_prices(self, token_ids: List[str]) -> Dict[str, Dict[str, Any]]:
|
||||
url = f"{self.DATA_API_URL}/v2/tokens/price"
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(
|
||||
url,
|
||||
headers=self._data_headers(),
|
||||
json={"token_ids": token_ids},
|
||||
timeout=30.0,
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
if data.get("status") == 200:
|
||||
return data.get("data", {})
|
||||
return {}
|
||||
|
||||
async def get_token_details(self, token_id: str) -> Optional[Dict[str, Any]]:
|
||||
url = f"{self.DATA_API_URL}/v2/tokens/{token_id}"
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(url, headers=self._data_headers(), timeout=30.0)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
if data.get("status") == 200:
|
||||
return data.get("data")
|
||||
return None
|
||||
|
||||
async def get_klines(
|
||||
self,
|
||||
token_id: str,
|
||||
interval: str = "1h",
|
||||
limit: int = 100,
|
||||
start_time: Optional[int] = None,
|
||||
end_time: Optional[int] = None,
|
||||
) -> List[Dict[str, Any]]:
|
||||
# Token ID must be in format "{contract_address}-bsc" for the AVE API
|
||||
if not token_id.endswith("-bsc") and token_id.startswith("0x"):
|
||||
token_id = f"{token_id}-bsc"
|
||||
|
||||
url = f"{self.DATA_API_URL}/v2/klines/token/{token_id}"
|
||||
params = {"interval": interval, "limit": limit}
|
||||
if start_time:
|
||||
params["start_time"] = start_time
|
||||
if end_time:
|
||||
params["end_time"] = end_time
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(
|
||||
url, headers=self._data_headers(), params=params, timeout=30.0
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
# AVE API returns status: 1 for success, not 200
|
||||
if data.get("status") == 1:
|
||||
return data.get("data", {}).get("points", [])
|
||||
raise Exception(f"Failed to fetch klines: {data}")
|
||||
|
||||
async def get_token_price(self, token_id: str) -> Optional[Dict[str, Any]]:
|
||||
url = f"{self.DATA_API_URL}/v2/tokens/price"
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(
|
||||
url,
|
||||
headers=self._data_headers(),
|
||||
json={"token_ids": [token_id]},
|
||||
timeout=30.0,
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
if data.get("status") == 1:
|
||||
prices = data.get("data", {})
|
||||
return prices.get(token_id)
|
||||
return None
|
||||
|
||||
async def get_trending_tokens(
|
||||
self, chain: Optional[str] = None, limit: int = 20
|
||||
) -> List[Dict[str, Any]]:
|
||||
url = f"{self.DATA_API_URL}/v2/tokens/trending"
|
||||
params = {"limit": limit}
|
||||
if chain:
|
||||
params["chain"] = chain
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(
|
||||
url, headers=self._data_headers(), params=params, timeout=30.0
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
if data.get("status") == 200:
|
||||
return data.get("data", [])
|
||||
raise Exception(f"Failed to fetch trending tokens: {data}")
|
||||
|
||||
async def get_token_risk(self, contract_id: str) -> Optional[Dict[str, Any]]:
|
||||
url = f"{self.DATA_API_URL}/v2/contracts/{contract_id}"
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(url, headers=self._data_headers(), timeout=30.0)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
if data.get("status") == 200:
|
||||
return data.get("data")
|
||||
return None
|
||||
|
||||
async def get_chain_quote(
|
||||
self,
|
||||
chain: str,
|
||||
from_token: str,
|
||||
to_token: str,
|
||||
amount: str,
|
||||
slippage: float = 0.5,
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
url = f"{self.TRADING_API_URL}/v1/chain/quote"
|
||||
payload = {
|
||||
"chain": chain,
|
||||
"from_token": from_token,
|
||||
"to_token": to_token,
|
||||
"amount": amount,
|
||||
"slippage": slippage,
|
||||
}
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(
|
||||
url, headers=self._trading_headers(), json=payload, timeout=30.0
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
if data.get("status") == 200:
|
||||
return data.get("data")
|
||||
return None
|
||||
|
||||
async def get_chain_swap(
|
||||
self,
|
||||
chain: str,
|
||||
from_token: str,
|
||||
to_token: str,
|
||||
amount: str,
|
||||
slippage: float = 0.5,
|
||||
wallet_address: Optional[str] = None,
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
url = f"{self.TRADING_API_URL}/v1/chain/swap"
|
||||
payload = {
|
||||
"chain": chain,
|
||||
"from_token": from_token,
|
||||
"to_token": to_token,
|
||||
"amount": amount,
|
||||
"slippage": slippage,
|
||||
}
|
||||
if wallet_address:
|
||||
payload["wallet_address"] = wallet_address
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.post(
|
||||
url, headers=self._trading_headers(), json=payload, timeout=60.0
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
if data.get("status") == 200:
|
||||
return data.get("data")
|
||||
return None
|
||||
|
||||
|
||||
def check_tier_access(user_tier: str, feature: str) -> tuple[bool, Optional[str]]:
|
||||
tier_access = {
|
||||
"free": {
|
||||
"data_rest": True,
|
||||
"websocket": False,
|
||||
"chain_wallet": True,
|
||||
"proxy_wallet": False,
|
||||
},
|
||||
"normal": {
|
||||
"data_rest": True,
|
||||
"websocket": False,
|
||||
"chain_wallet": True,
|
||||
"proxy_wallet": True,
|
||||
},
|
||||
"pro": {
|
||||
"data_rest": True,
|
||||
"websocket": True,
|
||||
"chain_wallet": True,
|
||||
"proxy_wallet": True,
|
||||
},
|
||||
}
|
||||
|
||||
if user_tier not in tier_access:
|
||||
user_tier = "free"
|
||||
|
||||
access = tier_access[user_tier]
|
||||
if access.get(feature):
|
||||
return True, None
|
||||
|
||||
upsell_messages = {
|
||||
"websocket": "Upgrade to Pro plan to access WebSocket streaming data. Visit your account settings.",
|
||||
"proxy_wallet": "Upgrade to Normal or Pro plan to access Proxy Wallet functionality. Visit your account settings.",
|
||||
}
|
||||
|
||||
return False, upsell_messages.get(
|
||||
feature, "Upgrade your plan to access this feature."
|
||||
)
|
||||
@@ -1,15 +1,568 @@
|
||||
from typing import Optional, Dict, Any
|
||||
import uuid
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
from typing import Dict, Any, List, Optional
|
||||
from ..ave.client import AveCloudClient
|
||||
|
||||
|
||||
class BacktestEngine:
|
||||
def __init__(self, config: Dict[str, Any]):
|
||||
self.config = config
|
||||
self.run_id = str(uuid.uuid4())
|
||||
self.status = "pending"
|
||||
self.results: Optional[Dict[str, Any]] = None
|
||||
self.signals: List[Dict[str, Any]] = []
|
||||
self.ave_client = AveCloudClient(
|
||||
api_key=config.get("ave_api_key", ""),
|
||||
plan=config.get("ave_api_plan", "free"),
|
||||
)
|
||||
self.bot_id = config.get("bot_id")
|
||||
self.strategy_config = config.get("strategy_config", {})
|
||||
self.conditions = self.strategy_config.get("conditions", [])
|
||||
self.actions = self.strategy_config.get("actions", [])
|
||||
self.risk_management = self.strategy_config.get("risk_management", {})
|
||||
self.stop_loss_percent = self.risk_management.get("stop_loss_percent")
|
||||
self.take_profit_percent = self.risk_management.get("take_profit_percent")
|
||||
self.initial_balance = config.get("initial_balance", 10000.0)
|
||||
self.current_balance = self.initial_balance
|
||||
self.position = 0.0
|
||||
self.position_token = ""
|
||||
self.entry_price: Optional[float] = None
|
||||
self.cost_basis = 0.0 # Track total amount spent on current position for average price calc
|
||||
self.entry_time: Optional[int] = None
|
||||
self.trades: List[Dict[str, Any]] = []
|
||||
self.running = False
|
||||
self.progress = 0
|
||||
self.total_klines = 0
|
||||
self.last_kline_price: Optional[float] = None # Track last price for open position valuation
|
||||
|
||||
async def run(self) -> Dict[str, Any]:
|
||||
raise NotImplementedError("Backtest engine not yet implemented")
|
||||
self.running = True
|
||||
self.status = "running"
|
||||
started_at = datetime.utcnow()
|
||||
|
||||
try:
|
||||
chain = self.config.get("chain", "bsc")
|
||||
timeframe = self.config.get("timeframe", "1h")
|
||||
start_date = self.config.get("start_date", "")
|
||||
end_date = self.config.get("end_date", "")
|
||||
|
||||
# Get token address from strategy config (saved when user confirmed token)
|
||||
token_address = None
|
||||
token_symbol = None
|
||||
|
||||
# Try to get from conditions first
|
||||
if self.conditions:
|
||||
token_address = self.conditions[0].get("token_address")
|
||||
token_symbol = self.conditions[0].get("token")
|
||||
# Fallback to actions
|
||||
if not token_address and self.actions:
|
||||
token_address = self.actions[0].get("token_address")
|
||||
token_symbol = self.actions[0].get("token") or token_symbol
|
||||
|
||||
if not token_address:
|
||||
raise ValueError("Token address not found in strategy. Please update your strategy with a valid token.")
|
||||
|
||||
token_id = token_address
|
||||
|
||||
start_ts = None
|
||||
end_ts = None
|
||||
if start_date:
|
||||
start_ts = int(
|
||||
datetime.fromisoformat(
|
||||
start_date.replace("Z", "+00:00")
|
||||
).timestamp()
|
||||
* 1000
|
||||
)
|
||||
if end_date:
|
||||
end_ts = int(
|
||||
datetime.fromisoformat(end_date.replace("Z", "+00:00")).timestamp()
|
||||
* 1000
|
||||
)
|
||||
|
||||
klines = await self.ave_client.get_klines(
|
||||
token_id=token_id,
|
||||
interval=timeframe,
|
||||
limit=1000,
|
||||
start_time=start_ts,
|
||||
end_time=end_ts,
|
||||
)
|
||||
|
||||
if not klines:
|
||||
self.status = "failed"
|
||||
self.results = {"error": "No kline data available"}
|
||||
return self.results
|
||||
|
||||
# Debug: log first and last few klines to verify price data
|
||||
print(f"DEBUG BacktestEngine: Got {len(klines)} klines for {token_id}")
|
||||
if klines:
|
||||
print(f"DEBUG BacktestEngine: First kline: {klines[0]}")
|
||||
print(f"DEBUG BacktestEngine: Last kline: {klines[-1]}")
|
||||
|
||||
# Validate Kline data - check for obviously wrong prices
|
||||
first_close = float(klines[0].get('close', 0))
|
||||
last_close = float(klines[-1].get('close', 0))
|
||||
|
||||
print(f"DEBUG BacktestEngine: first_close={first_close}, last_close={last_close}")
|
||||
|
||||
# If price changes by more than 1000x, data is likely wrong
|
||||
if first_close > 0 and last_close > 0:
|
||||
price_ratio = max(first_close, last_close) / min(first_close, last_close)
|
||||
print(f"DEBUG BacktestEngine: price_ratio={price_ratio:.0f}x")
|
||||
|
||||
if price_ratio > 1000:
|
||||
self.status = "failed"
|
||||
self.results = {
|
||||
"error": f"Kline data appears incorrect. Price changed by {price_ratio:.0f}x during the period. "
|
||||
f"First price: ${first_close:.8f}, Last price: ${last_close:.8f}. "
|
||||
f"This may be due to incorrect token data from the API. Please try a different token or timeframe."
|
||||
}
|
||||
return self.results
|
||||
|
||||
await self._process_klines(klines)
|
||||
self._calculate_metrics()
|
||||
self.status = "completed"
|
||||
|
||||
except Exception as e:
|
||||
self.status = "failed"
|
||||
self.results = {"error": str(e)}
|
||||
|
||||
ended_at = datetime.utcnow()
|
||||
self.results = self.results or {}
|
||||
self.results["started_at"] = started_at
|
||||
self.results["ended_at"] = ended_at
|
||||
self.results["duration_seconds"] = (ended_at - started_at).total_seconds()
|
||||
|
||||
return self.results
|
||||
|
||||
async def run_with_klines(self, klines: List[Dict[str, Any]]):
|
||||
"""Test helper method that runs backtest with provided klines (bypasses API call)."""
|
||||
self.running = True
|
||||
self.status = "running"
|
||||
started_at = datetime.utcnow()
|
||||
|
||||
try:
|
||||
if not klines:
|
||||
self.status = "failed"
|
||||
self.results = {"error": "No kline data available"}
|
||||
return self.results
|
||||
|
||||
await self._process_klines(klines)
|
||||
self._calculate_metrics()
|
||||
self.status = "completed"
|
||||
|
||||
except Exception as e:
|
||||
self.status = "failed"
|
||||
self.results = {"error": str(e)}
|
||||
|
||||
ended_at = datetime.utcnow()
|
||||
self.results = self.results or {}
|
||||
self.results["started_at"] = started_at
|
||||
self.results["ended_at"] = ended_at
|
||||
self.results["duration_seconds"] = (ended_at - started_at).total_seconds()
|
||||
|
||||
return self.results
|
||||
|
||||
async def _process_klines(self, klines: List[Dict[str, Any]]):
|
||||
self.total_klines = len(klines)
|
||||
|
||||
# Debug: log strategy config
|
||||
print(f"DEBUG _process_klines: {len(klines)} klines to process")
|
||||
print(f"DEBUG _process_klines: conditions = {self.conditions}")
|
||||
print(f"DEBUG _process_klines: actions = {self.actions}")
|
||||
print(f"DEBUG _process_klines: stop_loss_percent = {self.stop_loss_percent}")
|
||||
print(f"DEBUG _process_klines: take_profit_percent = {self.take_profit_percent}")
|
||||
|
||||
# Count price drops for debugging
|
||||
dip_opportunities = 0
|
||||
|
||||
for i, kline in enumerate(klines):
|
||||
if not self.running:
|
||||
break
|
||||
|
||||
self.progress = int((i / self.total_klines) * 100) if self.total_klines > 0 else 0
|
||||
|
||||
price = float(kline.get("close", 0))
|
||||
if price <= 0:
|
||||
continue
|
||||
|
||||
self.last_kline_price = price # Track last price for mark to market
|
||||
|
||||
timestamp = kline.get("timestamp", 0)
|
||||
|
||||
if self.position > 0 and self.entry_price is not None:
|
||||
exit_info = self._check_risk_management(price, timestamp)
|
||||
if exit_info:
|
||||
print(f"DEBUG: Kline {i} - Risk exit triggered: {exit_info['reason']} at price {price}")
|
||||
await self._execute_risk_exit(price, timestamp, exit_info)
|
||||
continue
|
||||
|
||||
# Check each condition
|
||||
for condition in self.conditions:
|
||||
cond_result = self._check_condition(condition, klines, i, price)
|
||||
if cond_result:
|
||||
dip_opportunities += 1
|
||||
if self.position == 0:
|
||||
print(f"DEBUG: Kline {i} - BUY condition triggered: {condition['type']} at price {price}")
|
||||
await self._execute_actions(price, timestamp, condition)
|
||||
break
|
||||
|
||||
print(f"DEBUG _process_klines: Total dip opportunities: {dip_opportunities}")
|
||||
|
||||
@property
|
||||
def average_entry_price(self) -> Optional[float]:
|
||||
"""Calculate weighted average entry price based on cost basis."""
|
||||
if self.position <= 0 or self.cost_basis <= 0:
|
||||
return None
|
||||
return self.cost_basis / self.position
|
||||
|
||||
def _check_risk_management(
|
||||
self, current_price: float, timestamp: int
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
if self.position <= 0 or self.average_entry_price is None:
|
||||
return None
|
||||
|
||||
if self.stop_loss_percent is not None:
|
||||
stop_loss_price = self.average_entry_price * (1 - self.stop_loss_percent / 100)
|
||||
if current_price <= stop_loss_price:
|
||||
return {"reason": "stop_loss", "price": stop_loss_price}
|
||||
|
||||
if self.take_profit_percent is not None:
|
||||
take_profit_price = self.average_entry_price * (1 + self.take_profit_percent / 100)
|
||||
# Use small epsilon to handle floating point precision
|
||||
if current_price >= take_profit_price - 0.001:
|
||||
return {"reason": "take_profit", "price": take_profit_price}
|
||||
|
||||
return None
|
||||
|
||||
async def _execute_risk_exit(
|
||||
self, price: float, timestamp: int, exit_info: Dict[str, Any]
|
||||
):
|
||||
if self.position <= 0:
|
||||
return
|
||||
|
||||
reason = exit_info["reason"]
|
||||
sell_amount = self.position * price
|
||||
self.current_balance += sell_amount
|
||||
self.trades.append(
|
||||
{
|
||||
"type": "sell",
|
||||
"token": self.position_token,
|
||||
"price": price,
|
||||
"amount": sell_amount,
|
||||
"quantity": self.position,
|
||||
"timestamp": timestamp,
|
||||
"exit_reason": reason,
|
||||
}
|
||||
)
|
||||
self.signals.append(
|
||||
{
|
||||
"id": str(uuid.uuid4()),
|
||||
"bot_id": self.bot_id,
|
||||
"run_id": self.run_id,
|
||||
"signal_type": "sell",
|
||||
"token": self.position_token,
|
||||
"price": price,
|
||||
"confidence": 1.0,
|
||||
"reasoning": f"Risk management triggered {reason}",
|
||||
"executed": False,
|
||||
"created_at": datetime.utcnow(),
|
||||
}
|
||||
)
|
||||
self.position = 0
|
||||
self.entry_price = None
|
||||
self.cost_basis = 0.0
|
||||
self.entry_time = None
|
||||
|
||||
def _check_condition(
|
||||
self,
|
||||
condition: Dict[str, Any],
|
||||
klines: List[Dict[str, Any]],
|
||||
current_idx: int,
|
||||
current_price: float,
|
||||
) -> bool:
|
||||
cond_type = condition.get("type", "")
|
||||
threshold = condition.get("threshold", 0)
|
||||
timeframe = condition.get("timeframe", "1h")
|
||||
price_level = condition.get("price")
|
||||
direction = condition.get("direction", "above")
|
||||
|
||||
if cond_type == "price_drop":
|
||||
if current_idx == 0:
|
||||
return False
|
||||
prev_price = float(klines[current_idx - 1].get("close", 0))
|
||||
if prev_price <= 0:
|
||||
return False
|
||||
drop_pct = ((prev_price - current_price) / prev_price) * 100
|
||||
# Debug first few to see what's happening
|
||||
if current_idx < 5 or drop_pct >= threshold:
|
||||
print(f"DEBUG _check_condition: idx={current_idx}, prev={prev_price}, curr={current_price}, drop={drop_pct:.4f}%, threshold={threshold}, trigger={drop_pct >= threshold}")
|
||||
return drop_pct >= threshold
|
||||
|
||||
elif cond_type == "price_rise":
|
||||
if current_idx == 0:
|
||||
return False
|
||||
prev_price = float(klines[current_idx - 1].get("close", 0))
|
||||
if prev_price <= 0:
|
||||
return False
|
||||
rise_pct = ((current_price - prev_price) / prev_price) * 100
|
||||
return rise_pct >= threshold
|
||||
|
||||
elif cond_type == "volume_spike":
|
||||
if current_idx == 0:
|
||||
return False
|
||||
prev_volume = float(klines[current_idx - 1].get("volume", 0))
|
||||
current_volume = float(kline.get("volume", 0))
|
||||
if prev_volume <= 0:
|
||||
return False
|
||||
volume_increase = ((current_volume - prev_volume) / prev_volume) * 100
|
||||
return volume_increase >= threshold
|
||||
|
||||
elif cond_type == "price_level":
|
||||
if price_level is None:
|
||||
return False
|
||||
if direction == "above":
|
||||
return current_price > price_level
|
||||
else:
|
||||
return current_price < price_level
|
||||
|
||||
return False
|
||||
|
||||
async def _execute_actions(
|
||||
self, price: float, timestamp: int, matched_condition: Dict[str, Any]
|
||||
):
|
||||
token = matched_condition.get("token", self.config.get("token", ""))
|
||||
|
||||
for action in self.actions:
|
||||
action_type = action.get("type", "")
|
||||
amount_percent = action.get("amount_percent", 10)
|
||||
amount = self.current_balance * (amount_percent / 100)
|
||||
|
||||
if action_type == "buy" and self.current_balance >= amount:
|
||||
# Buy if we have funds available (supports DCA - buy more on each dip)
|
||||
# position > 0 just means we already have some tokens, we can still buy more
|
||||
quantity = amount / price
|
||||
self.position += quantity
|
||||
self.current_balance -= amount
|
||||
self.cost_basis += amount # Track total cost for average price
|
||||
self.position_token = token
|
||||
self.entry_price = price # Keep last entry price for reference
|
||||
self.entry_time = timestamp
|
||||
print(f"DEBUG _execute_actions: BUY - amount=${amount:.2f}, price={price}, quantity={quantity}, position={self.position}")
|
||||
self.trades.append(
|
||||
{
|
||||
"type": "buy",
|
||||
"token": token,
|
||||
"price": price,
|
||||
"amount": amount,
|
||||
"quantity": quantity,
|
||||
"timestamp": timestamp,
|
||||
}
|
||||
)
|
||||
self.signals.append(
|
||||
{
|
||||
"id": str(uuid.uuid4()),
|
||||
"bot_id": self.bot_id,
|
||||
"run_id": self.run_id,
|
||||
"signal_type": "buy",
|
||||
"token": token,
|
||||
"price": price,
|
||||
"confidence": 0.8,
|
||||
"reasoning": f"Condition {matched_condition.get('type')} triggered buy",
|
||||
"executed": False,
|
||||
"created_at": datetime.utcnow(),
|
||||
}
|
||||
)
|
||||
|
||||
elif action_type == "sell" and self.position > 0:
|
||||
# Sell amount_percent of current position (default 100% if not specified)
|
||||
sell_percent = action.get("amount_percent", 100) / 100.0
|
||||
sell_quantity = self.position * sell_percent
|
||||
sell_amount = sell_quantity * price
|
||||
self.current_balance += sell_amount
|
||||
|
||||
# Proportionally reduce cost_basis
|
||||
sold_cost_basis = self.cost_basis * sell_percent
|
||||
self.cost_basis -= sold_cost_basis
|
||||
|
||||
print(f"DEBUG _execute_actions: SELL - position={self.position}, sell_percent={sell_percent*100}%, quantity={sell_quantity}, price={price}, sell_amount=${sell_amount:.2f}")
|
||||
self.trades.append(
|
||||
{
|
||||
"type": "sell",
|
||||
"token": self.position_token,
|
||||
"price": price,
|
||||
"amount": sell_amount,
|
||||
"quantity": sell_quantity,
|
||||
"timestamp": timestamp,
|
||||
"exit_reason": "manual",
|
||||
}
|
||||
)
|
||||
|
||||
# Update remaining position
|
||||
self.position -= sell_quantity
|
||||
if self.position <= 0.00000001: # Account for floating point
|
||||
self.position = 0
|
||||
self.entry_price = None
|
||||
self.cost_basis = 0.0
|
||||
self.entry_time = None
|
||||
self.signals.append(
|
||||
{
|
||||
"id": str(uuid.uuid4()),
|
||||
"bot_id": self.bot_id,
|
||||
"run_id": self.run_id,
|
||||
"signal_type": "sell",
|
||||
"token": self.position_token,
|
||||
"price": price,
|
||||
"confidence": 0.8,
|
||||
"reasoning": f"Condition {matched_condition.get('type')} triggered sell",
|
||||
"executed": False,
|
||||
"created_at": datetime.utcnow(),
|
||||
}
|
||||
)
|
||||
|
||||
def _calculate_metrics(self):
|
||||
# Debug: log all trades for analysis
|
||||
print(f"DEBUG _calculate_metrics: {len(self.trades)} total trades")
|
||||
buy_trades = [t for t in self.trades if t["type"] == "buy"]
|
||||
sell_trades = [t for t in self.trades if t["type"] == "sell"]
|
||||
print(f" Buy trades: {len(buy_trades)}")
|
||||
print(f" Sell trades: {len(sell_trades)}")
|
||||
|
||||
if buy_trades:
|
||||
print(f" First buy: amount=${buy_trades[0]['amount']:.2f}, price={buy_trades[0]['price']}, quantity={buy_trades[0]['quantity']}")
|
||||
print(f" Last buy: amount=${buy_trades[-1]['amount']:.2f}, price={buy_trades[-1]['price']}, quantity={buy_trades[-1]['quantity']}")
|
||||
|
||||
if sell_trades:
|
||||
print(f" First sell: amount=${sell_trades[0]['amount']:.2f}, price={sell_trades[0]['price']}, exit_reason={sell_trades[0].get('exit_reason')}")
|
||||
print(f" Last sell: amount=${sell_trades[-1]['amount']:.2f}, price={sell_trades[-1]['price']}, exit_reason={sell_trades[-1].get('exit_reason')}")
|
||||
|
||||
# For open positions, use the last kline price to mark to market
|
||||
# If no last kline price, fall back to entry price
|
||||
position_price = self.last_kline_price
|
||||
if position_price is None and self.trades and self.position > 0:
|
||||
position_price = self.trades[-1]["price"] # Fall back to entry price
|
||||
|
||||
# Debug logging
|
||||
print(f"DEBUG _calculate_metrics:")
|
||||
print(f" initial_balance: {self.initial_balance}")
|
||||
print(f" current_balance: {self.current_balance}")
|
||||
print(f" position: {self.position}")
|
||||
print(f" position_price: {position_price}")
|
||||
print(f" last_kline_price: {self.last_kline_price}")
|
||||
|
||||
# Calculate final balance: use marked-to-market value if position open, otherwise current balance
|
||||
if self.position > 0 and position_price:
|
||||
final_balance = self.current_balance + self.position * position_price
|
||||
else:
|
||||
final_balance = self.current_balance
|
||||
|
||||
print(f" final_balance calculated: {final_balance}")
|
||||
|
||||
total_return = (
|
||||
(final_balance - self.initial_balance) / self.initial_balance
|
||||
) * 100
|
||||
|
||||
print(f" total_return calculated: {total_return}%")
|
||||
|
||||
buy_trades = [t for t in self.trades if t["type"] == "buy"]
|
||||
sell_trades = [t for t in self.trades if t["type"] == "sell"]
|
||||
total_trades = len(buy_trades) + len(sell_trades)
|
||||
|
||||
winning_trades = 0
|
||||
for i, trade in enumerate(sell_trades):
|
||||
if i < len(buy_trades):
|
||||
buy_price = buy_trades[i]["price"]
|
||||
sell_price = trade["price"]
|
||||
if sell_price > buy_price:
|
||||
winning_trades += 1
|
||||
|
||||
win_rate = (winning_trades / len(sell_trades) * 100) if sell_trades else 0
|
||||
|
||||
portfolio_values = []
|
||||
running_balance = self.initial_balance
|
||||
running_position = 0.0
|
||||
current_token = ""
|
||||
last_price = 0.0
|
||||
|
||||
for trade in self.trades:
|
||||
if trade["type"] == "buy":
|
||||
running_position += trade["quantity"] # Add to existing position (DCA)
|
||||
running_balance -= trade["amount"] # Subtract amount spent
|
||||
current_token = trade["token"]
|
||||
last_price = trade["price"]
|
||||
else: # sell
|
||||
running_balance += trade["amount"] # Add amount received
|
||||
running_position = 0 # Close entire position
|
||||
last_price = trade["price"]
|
||||
|
||||
portfolio_value = running_balance + (running_position * last_price)
|
||||
portfolio_values.append(portfolio_value)
|
||||
|
||||
# If there's an open position, add final marked-to-market value
|
||||
if self.position > 0 and self.last_kline_price:
|
||||
final_portfolio_value = self.current_balance + (self.position * self.last_kline_price)
|
||||
portfolio_values.append(final_portfolio_value)
|
||||
|
||||
max_value = self.initial_balance
|
||||
max_drawdown = 0.0
|
||||
for value in portfolio_values:
|
||||
if value > max_value:
|
||||
max_value = value
|
||||
drawdown = ((max_value - value) / max_value) * 100
|
||||
if drawdown > max_drawdown:
|
||||
max_drawdown = drawdown
|
||||
|
||||
sharpe_ratio = 0.0
|
||||
if len(portfolio_values) > 1:
|
||||
returns = []
|
||||
for i in range(1, len(portfolio_values)):
|
||||
ret = (
|
||||
portfolio_values[i] - portfolio_values[i - 1]
|
||||
) / portfolio_values[i - 1]
|
||||
returns.append(ret)
|
||||
if returns:
|
||||
avg_return = sum(returns) / len(returns)
|
||||
variance = sum((r - avg_return) ** 2 for r in returns) / len(returns)
|
||||
std_dev = variance**0.5
|
||||
if std_dev > 0:
|
||||
sharpe_ratio = avg_return / std_dev
|
||||
|
||||
buy_signals = len(buy_trades)
|
||||
sell_signals = len(sell_trades)
|
||||
|
||||
self.results = {
|
||||
"total_return": round(total_return, 2),
|
||||
"win_rate": round(win_rate, 2),
|
||||
"total_trades": total_trades,
|
||||
"buy_signals": buy_signals,
|
||||
"sell_signals": sell_signals,
|
||||
"max_drawdown": round(max_drawdown, 2),
|
||||
"sharpe_ratio": round(sharpe_ratio, 2),
|
||||
"final_balance": round(final_balance, 2),
|
||||
"signals": self.signals,
|
||||
"trades": self.trades, # Include trades in results for storage
|
||||
}
|
||||
|
||||
async def stop(self):
|
||||
raise NotImplementedError("Backtest stop not yet implemented")
|
||||
self.running = False
|
||||
self.progress = 0
|
||||
self.total_klines = 0
|
||||
self.status = "stopped"
|
||||
self._calculate_metrics()
|
||||
|
||||
def get_results(self) -> Dict[str, Any]:
|
||||
raise NotImplementedError("Backtest results not yet implemented")
|
||||
return {
|
||||
"id": self.run_id,
|
||||
"status": self.status,
|
||||
"results": self.results,
|
||||
"signals": self.signals,
|
||||
"progress": self.progress,
|
||||
"total_klines": self.total_klines,
|
||||
}
|
||||
|
||||
def get_status(self) -> Dict[str, Any]:
|
||||
return {
|
||||
"status": self.status,
|
||||
"progress": self.progress,
|
||||
"total_klines": self.total_klines,
|
||||
}
|
||||
|
||||
95
src/backend/app/services/rate_limiter.py
Normal file
95
src/backend/app/services/rate_limiter.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import os
|
||||
from datetime import datetime, timedelta
|
||||
from sqlalchemy import func
|
||||
from fastapi import HTTPException
|
||||
from ..db.models import Message, AnonymousUser
|
||||
|
||||
MAX_CHATS_PER_5HOURS = int(os.getenv("MAX_CHATS_PER_5HOURS", "500"))
|
||||
MAX_ANONYMOUS_CHATS = 50
|
||||
MAX_ANONYMOUS_BOTS = 1
|
||||
MAX_ANONYMOUS_BACKTESTS = 1
|
||||
|
||||
|
||||
class RateLimiter:
|
||||
@staticmethod
|
||||
def check_system_limit(db):
|
||||
cutoff = datetime.utcnow() - timedelta(hours=5)
|
||||
count = (
|
||||
db.query(func.count(Message.id))
|
||||
.filter(Message.created_at >= cutoff)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
if count >= MAX_CHATS_PER_5HOURS:
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail="Rate limited from the agent service. Please come back later.",
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def check_anonymous_limit(db, anonymous_token: str):
|
||||
anon = (
|
||||
db.query(AnonymousUser).filter(AnonymousUser.id == anonymous_token).first()
|
||||
)
|
||||
|
||||
if anon and anon.chat_count >= MAX_ANONYMOUS_CHATS:
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail="You've reached the limit. Please create an account to continue.",
|
||||
)
|
||||
|
||||
return anon
|
||||
|
||||
@staticmethod
|
||||
def check_anonymous_bot_limit(db, anonymous_token: str):
|
||||
anon = (
|
||||
db.query(AnonymousUser).filter(AnonymousUser.id == anonymous_token).first()
|
||||
)
|
||||
|
||||
if anon and anon.bot_created:
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail="You've reached the limit. Please create an account to continue.",
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def check_anonymous_backtest_limit(db, anonymous_token: str):
|
||||
anon = (
|
||||
db.query(AnonymousUser).filter(AnonymousUser.id == anonymous_token).first()
|
||||
)
|
||||
|
||||
if anon and anon.backtest_count >= MAX_ANONYMOUS_BACKTESTS:
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail="You've reached the limit. Please create an account to continue.",
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def increment_chat_count(db, anonymous_token: str):
|
||||
anon = (
|
||||
db.query(AnonymousUser).filter(AnonymousUser.id == anonymous_token).first()
|
||||
)
|
||||
|
||||
if anon:
|
||||
anon.chat_count += 1
|
||||
db.commit()
|
||||
|
||||
@staticmethod
|
||||
def set_bot_created(db, anonymous_token: str):
|
||||
anon = (
|
||||
db.query(AnonymousUser).filter(AnonymousUser.id == anonymous_token).first()
|
||||
)
|
||||
|
||||
if anon:
|
||||
anon.bot_created = True
|
||||
db.commit()
|
||||
|
||||
@staticmethod
|
||||
def increment_backtest_count(db, anonymous_token: str):
|
||||
anon = (
|
||||
db.query(AnonymousUser).filter(AnonymousUser.id == anonymous_token).first()
|
||||
)
|
||||
|
||||
if anon:
|
||||
anon.backtest_count += 1
|
||||
db.commit()
|
||||
@@ -1,15 +1,437 @@
|
||||
from typing import Optional, Dict, Any, List
|
||||
import uuid
|
||||
import asyncio
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from typing import Dict, Any, List, Optional
|
||||
|
||||
from ..ave.client import AveCloudClient
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SimulateEngine:
|
||||
def __init__(self, config: Dict[str, Any]):
|
||||
self.config = config
|
||||
self.run_id = str(uuid.uuid4())
|
||||
self.status = "pending"
|
||||
self.results: Optional[Dict[str, Any]] = None
|
||||
self.signals: List[Dict[str, Any]] = []
|
||||
self.ave_client = AveCloudClient(
|
||||
api_key=config.get("ave_api_key", ""),
|
||||
plan=config.get("ave_api_plan", "free"),
|
||||
)
|
||||
self.bot_id = config.get("bot_id")
|
||||
self.strategy_config = config.get("strategy_config", {})
|
||||
self.conditions = self.strategy_config.get("conditions", [])
|
||||
self.actions = self.strategy_config.get("actions", [])
|
||||
self.risk_management = self.strategy_config.get("risk_management", {})
|
||||
self.stop_loss_percent = self.risk_management.get("stop_loss_percent")
|
||||
self.take_profit_percent = self.risk_management.get("take_profit_percent")
|
||||
|
||||
async def run(self) -> List[Dict[str, Any]]:
|
||||
raise NotImplementedError("Simulation engine not yet implemented")
|
||||
# Kline-based settings
|
||||
self.kline_interval = config.get("kline_interval", "1m")
|
||||
self.max_candles = config.get("max_candles", 100) # Limit candles to simulate real-time
|
||||
|
||||
async def stop(self):
|
||||
raise NotImplementedError("Simulation stop not yet implemented")
|
||||
# Delay between candles (in seconds) to simulate real-time
|
||||
# e.g., 1m interval -> 30s delay between candles
|
||||
# Use config value if provided, otherwise calculate
|
||||
if "candle_delay" in config and config["candle_delay"] is not None:
|
||||
self.candle_delay = config["candle_delay"]
|
||||
else:
|
||||
self.candle_delay = self._get_interval_seconds(self.kline_interval) / 2
|
||||
|
||||
self.auto_execute = config.get("auto_execute", False)
|
||||
self.token = config.get("token", "")
|
||||
self.chain = config.get("chain", "bsc")
|
||||
self.running = False
|
||||
self.started_at: Optional[datetime] = None
|
||||
|
||||
# Price tracking (for conditions)
|
||||
self.last_close: Optional[float] = None
|
||||
self.last_volume: Optional[float] = None
|
||||
|
||||
# Position tracking (for risk management)
|
||||
self.position: float = 0.0
|
||||
self.position_token: str = ""
|
||||
self.entry_price: Optional[float] = None
|
||||
self.entry_time: Optional[int] = None
|
||||
|
||||
# Portfolio
|
||||
self.current_balance: float = config.get("initial_balance", 10000.0)
|
||||
self.trades: List[Dict[str, Any]] = []
|
||||
|
||||
# Error tracking
|
||||
self.errors: List[str] = []
|
||||
|
||||
# Kline data
|
||||
self.klines: List[Dict[str, Any]] = []
|
||||
self.last_processed_time: Optional[int] = None
|
||||
|
||||
# Trade log - tracks what happened at each candle
|
||||
self.trade_log: List[Dict[str, Any]] = []
|
||||
|
||||
# Current candle being processed (for frontend to show progress)
|
||||
self.current_candle_index = 0
|
||||
self.total_candles = 0
|
||||
|
||||
def _get_interval_seconds(self, interval: str) -> int:
|
||||
"""Convert kline interval to seconds."""
|
||||
mapping = {
|
||||
"1m": 60,
|
||||
"5m": 300,
|
||||
"15m": 900,
|
||||
"30m": 1800,
|
||||
"1h": 3600,
|
||||
"4h": 14400,
|
||||
"1d": 86400,
|
||||
}
|
||||
return mapping.get(interval, 60)
|
||||
|
||||
async def run(self) -> Dict[str, Any]:
|
||||
self.running = True
|
||||
self.status = "running"
|
||||
self.started_at = datetime.utcnow()
|
||||
|
||||
token_id = (
|
||||
f"{self.token}-{self.chain}"
|
||||
if self.token and not self.token.endswith(f"-{self.chain}")
|
||||
else self.token
|
||||
)
|
||||
|
||||
if not token_id or token_id == f"-{self.chain}":
|
||||
self.status = "failed"
|
||||
self.results = {"error": "Token ID is required"}
|
||||
return self.results
|
||||
|
||||
try:
|
||||
# Step 1: Fetch klines (only once for simulation)
|
||||
self.klines = await self._fetch_klines(token_id)
|
||||
|
||||
if not self.klines:
|
||||
self.status = "failed"
|
||||
self.results = {"error": "No kline data available"}
|
||||
return self.results
|
||||
|
||||
logger.info(f"Fetched {len(self.klines)} klines for {token_id}")
|
||||
|
||||
# Step 2: Process candles (with limit)
|
||||
candles_processed = 0
|
||||
self.total_candles = min(len(self.klines), self.max_candles)
|
||||
self.current_candle_index = 0
|
||||
|
||||
for i, candle in enumerate(self.klines):
|
||||
if not self.running:
|
||||
break
|
||||
if candles_processed >= self.max_candles:
|
||||
logger.info(f"Reached max candles limit ({self.max_candles})")
|
||||
break
|
||||
|
||||
self.current_candle_index = candles_processed
|
||||
candle_time = int(candle.get("time", 0))
|
||||
|
||||
# Get OHLCV data from candle
|
||||
close_price = float(candle.get("close", 0))
|
||||
volume = float(candle.get("volume", 0))
|
||||
|
||||
if close_price > 0:
|
||||
# Process candle
|
||||
await self._process_candle(close_price, volume, candle_time)
|
||||
|
||||
# Update last close for next iteration
|
||||
self.last_close = close_price
|
||||
self.last_volume = volume
|
||||
|
||||
# Track last processed time
|
||||
self.last_processed_time = candle_time
|
||||
|
||||
candles_processed += 1
|
||||
|
||||
# Delay to simulate real-time (only for visible candles, not initial batch)
|
||||
if candles_processed > 1 and self.candle_delay > 0:
|
||||
await asyncio.sleep(self.candle_delay)
|
||||
|
||||
self.status = "completed"
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Simulation error: {e}")
|
||||
self.status = "failed"
|
||||
self.results = {"error": str(e)}
|
||||
self.errors.append(str(e))
|
||||
|
||||
self.results = self.results or {}
|
||||
self.results["total_signals"] = len(self.signals)
|
||||
self.results["total_trades"] = len(self.trades)
|
||||
self.results["total_errors"] = len(self.errors)
|
||||
self.results["errors"] = self.errors
|
||||
self.results["signals"] = self.signals
|
||||
self.results["candles_processed"] = candles_processed
|
||||
self.results["current_candle_index"] = self.current_candle_index
|
||||
self.results["total_candles"] = self.total_candles
|
||||
self.results["klines"] = self.klines # Include klines for chart display
|
||||
self.results["trade_log"] = self.trade_log # Include trade log for dashboard
|
||||
self.results["portfolio"] = {
|
||||
"initial_balance": self.config.get("initial_balance", 10000),
|
||||
"current_balance": self.current_balance,
|
||||
"position": self.position,
|
||||
"position_token": self.position_token,
|
||||
"entry_price": self.entry_price,
|
||||
"current_price": self.last_close,
|
||||
}
|
||||
self.results["started_at"] = self.started_at
|
||||
self.results["ended_at"] = datetime.utcnow()
|
||||
|
||||
return self.results
|
||||
|
||||
async def _fetch_klines(
|
||||
self,
|
||||
token_id: str,
|
||||
limit: int = 500
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""Fetch klines from AVE API."""
|
||||
try:
|
||||
klines = await self.ave_client.get_klines(
|
||||
token_id,
|
||||
interval=self.kline_interval,
|
||||
limit=limit
|
||||
)
|
||||
|
||||
# Sort by time ascending (oldest first)
|
||||
klines = sorted(klines, key=lambda x: x.get("time", 0))
|
||||
return klines
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to fetch klines for {token_id}: {e}")
|
||||
self.errors.append(f"Kline fetch failed: {str(e)}")
|
||||
return []
|
||||
|
||||
async def _process_candle(
|
||||
self,
|
||||
close_price: float,
|
||||
volume: float,
|
||||
timestamp: int
|
||||
):
|
||||
"""Process a single candle - check conditions and risk management."""
|
||||
|
||||
action = "hold" # Default action
|
||||
reason = ""
|
||||
|
||||
# Check risk management first (for open positions)
|
||||
if self.position > 0 and self.entry_price is not None:
|
||||
exit_info = self._check_risk_management(close_price, timestamp)
|
||||
if exit_info:
|
||||
await self._execute_risk_exit(close_price, timestamp, exit_info)
|
||||
action = "sell"
|
||||
reason = exit_info["reason"]
|
||||
# Log the action
|
||||
self.trade_log.append({
|
||||
"time": timestamp,
|
||||
"price": close_price,
|
||||
"action": action,
|
||||
"reason": reason,
|
||||
"position": self.position,
|
||||
"entry_price": self.entry_price,
|
||||
})
|
||||
return
|
||||
|
||||
# Check conditions (only if no open position)
|
||||
if self.position == 0:
|
||||
for condition in self.conditions:
|
||||
if self._check_condition(condition, close_price, volume):
|
||||
await self._execute_actions(close_price, timestamp, condition)
|
||||
action = "buy"
|
||||
reason = f"{condition.get('type')} {condition.get('threshold')}%".format(
|
||||
type=condition.get('type'),
|
||||
threshold=condition.get('threshold')
|
||||
)
|
||||
# Log the action
|
||||
self.trade_log.append({
|
||||
"time": timestamp,
|
||||
"price": close_price,
|
||||
"action": action,
|
||||
"reason": reason,
|
||||
"position": self.position,
|
||||
"entry_price": self.entry_price,
|
||||
})
|
||||
break
|
||||
|
||||
# Log hold action (no signal)
|
||||
if action == "hold":
|
||||
# Only log every 10th candle to reduce data
|
||||
if len(self.trade_log) == 0 or (len(self.klines) - len(self.trade_log) > 10):
|
||||
self.trade_log.append({
|
||||
"time": timestamp,
|
||||
"price": close_price,
|
||||
"action": "hold",
|
||||
"reason": "no_signal",
|
||||
"position": self.position,
|
||||
"entry_price": self.entry_price,
|
||||
})
|
||||
|
||||
def _check_risk_management(
|
||||
self, current_price: float, timestamp: int
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
"""Check if stop loss or take profit is triggered."""
|
||||
if self.position <= 0 or self.entry_price is None:
|
||||
return None
|
||||
|
||||
if self.stop_loss_percent is not None:
|
||||
stop_loss_price = self.entry_price * (1 - self.stop_loss_percent / 100)
|
||||
if current_price <= stop_loss_price:
|
||||
return {"reason": "stop_loss", "price": stop_loss_price}
|
||||
|
||||
if self.take_profit_percent is not None:
|
||||
take_profit_price = self.entry_price * (1 + self.take_profit_percent / 100)
|
||||
if current_price >= take_profit_price:
|
||||
return {"reason": "take_profit", "price": take_profit_price}
|
||||
|
||||
return None
|
||||
|
||||
async def _execute_risk_exit(
|
||||
self, price: float, timestamp: int, exit_info: Dict[str, Any]
|
||||
):
|
||||
"""Execute stop loss or take profit."""
|
||||
if self.position <= 0:
|
||||
return
|
||||
|
||||
reason = exit_info["reason"]
|
||||
quantity = self.position
|
||||
sale_proceeds = quantity * price
|
||||
|
||||
# Add sale proceeds to cash balance
|
||||
self.current_balance += sale_proceeds
|
||||
|
||||
self.trades.append(
|
||||
{
|
||||
"type": "sell",
|
||||
"token": self.position_token,
|
||||
"price": price,
|
||||
"quantity": quantity,
|
||||
"amount": sale_proceeds,
|
||||
"timestamp": timestamp,
|
||||
"exit_reason": reason,
|
||||
}
|
||||
)
|
||||
self.signals.append(
|
||||
{
|
||||
"id": str(uuid.uuid4()),
|
||||
"bot_id": self.bot_id,
|
||||
"run_id": self.run_id,
|
||||
"signal_type": "sell",
|
||||
"token": self.position_token,
|
||||
"price": price,
|
||||
"confidence": 1.0,
|
||||
"reasoning": f"Risk management triggered {reason}",
|
||||
"executed": self.auto_execute,
|
||||
"created_at": datetime.utcnow(),
|
||||
}
|
||||
)
|
||||
self.position = 0
|
||||
self.entry_price = None
|
||||
self.entry_time = None
|
||||
|
||||
def _check_condition(
|
||||
self,
|
||||
condition: Dict[str, Any],
|
||||
current_price: float,
|
||||
current_volume: float,
|
||||
) -> bool:
|
||||
"""Check if a condition is met based on price movement."""
|
||||
cond_type = condition.get("type", "")
|
||||
threshold = condition.get("threshold", 0)
|
||||
|
||||
if cond_type == "price_drop":
|
||||
# Price dropped by threshold % from last close
|
||||
if self.last_close is None or self.last_close <= 0:
|
||||
return False
|
||||
drop_pct = ((self.last_close - current_price) / self.last_close) * 100
|
||||
return drop_pct >= threshold
|
||||
|
||||
elif cond_type == "price_rise":
|
||||
# Price rose by threshold % from last close
|
||||
if self.last_close is None or self.last_close <= 0:
|
||||
return False
|
||||
rise_pct = ((current_price - self.last_close) / self.last_close) * 100
|
||||
return rise_pct >= threshold
|
||||
|
||||
elif cond_type == "volume_spike":
|
||||
# Volume increased significantly
|
||||
if self.last_volume is None or self.last_volume <= 0:
|
||||
return False
|
||||
volume_increase = ((current_volume - self.last_volume) / self.last_volume) * 100
|
||||
return volume_increase >= threshold
|
||||
|
||||
elif cond_type == "price_level":
|
||||
price_level = condition.get("price")
|
||||
direction = condition.get("direction", "above")
|
||||
if price_level is None:
|
||||
return False
|
||||
if direction == "above":
|
||||
return current_price > price_level
|
||||
else:
|
||||
return current_price < price_level
|
||||
|
||||
return False
|
||||
|
||||
async def _execute_actions(
|
||||
self, price: float, timestamp: int, matched_condition: Dict[str, Any]
|
||||
):
|
||||
"""Execute buy/sell actions based on matched condition."""
|
||||
token = matched_condition.get("token", self.token)
|
||||
reasoning = f"Condition {matched_condition.get('type')} triggered"
|
||||
|
||||
for action in self.actions:
|
||||
action_type = action.get("type", "")
|
||||
if action_type == "buy":
|
||||
amount_percent = action.get("amount_percent", 10)
|
||||
amount = self.current_balance * (amount_percent / 100)
|
||||
quantity = amount / price
|
||||
|
||||
self.position += quantity
|
||||
self.position_token = token
|
||||
self.entry_price = price
|
||||
self.entry_time = timestamp
|
||||
self.current_balance -= amount
|
||||
|
||||
self.trades.append(
|
||||
{
|
||||
"type": "buy",
|
||||
"token": token,
|
||||
"price": price,
|
||||
"amount": amount,
|
||||
"quantity": quantity,
|
||||
"timestamp": timestamp,
|
||||
}
|
||||
)
|
||||
|
||||
signal = {
|
||||
"id": str(uuid.uuid4()),
|
||||
"bot_id": self.bot_id,
|
||||
"run_id": self.run_id,
|
||||
"signal_type": action_type,
|
||||
"token": token,
|
||||
"price": price,
|
||||
"confidence": 0.8,
|
||||
"reasoning": reasoning,
|
||||
"executed": self.auto_execute,
|
||||
"created_at": datetime.utcnow(),
|
||||
}
|
||||
|
||||
self.signals.append(signal)
|
||||
|
||||
def stop(self):
|
||||
"""Stop the simulation."""
|
||||
self.running = False
|
||||
self.status = "stopped"
|
||||
|
||||
def get_results(self) -> Dict[str, Any]:
|
||||
"""Get simulation results."""
|
||||
return {
|
||||
"id": self.run_id,
|
||||
"status": self.status,
|
||||
"results": self.results,
|
||||
"signals": self.signals,
|
||||
}
|
||||
|
||||
def get_signals(self) -> List[Dict[str, Any]]:
|
||||
raise NotImplementedError("Simulation signals not yet implemented")
|
||||
"""Get current signals."""
|
||||
return self.signals
|
||||
|
||||
@@ -6,7 +6,9 @@ pydantic-settings>=2.1.0
|
||||
email-validator>=2.0.0
|
||||
python-jose[cryptography]>=3.3.0
|
||||
passlib[bcrypt]>=1.7.4
|
||||
bcrypt>=4.0,<5.0 # Required for passlib compatibility
|
||||
crewai>=0.1.0
|
||||
anthropic>=0.18.0
|
||||
httpx>=0.26.0
|
||||
python-multipart>=0.0.6
|
||||
slowapi>=0.1.9
|
||||
|
||||
@@ -8,4 +8,5 @@ if __name__ == "__main__":
|
||||
host=settings.HOST,
|
||||
port=settings.PORT,
|
||||
reload=settings.DEBUG,
|
||||
timeout_keep_alive=300,
|
||||
)
|
||||
|
||||
609
src/backend/tests/test_agent.py
Normal file
609
src/backend/tests/test_agent.py
Normal file
@@ -0,0 +1,609 @@
|
||||
"""Tests for ConversationalAgent using mock client."""
|
||||
|
||||
import pytest
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add parent directory to path
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from app.services.ai_agent.agent import ConversationalAgent
|
||||
from app.services.ai_agent.mock_client import MockMiniMaxClient, MockMiniMaxClientWithToolCall
|
||||
|
||||
|
||||
class TestConversationalAgent:
|
||||
"""Test ConversationalAgent with mocked MiniMax API."""
|
||||
|
||||
def test_greeting_response(self):
|
||||
"""Test that agent responds to greeting."""
|
||||
mock = MockMiniMaxClient()
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"message": {
|
||||
"content": "Hello! How can I help you with your trading today?",
|
||||
"role": "assistant",
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
agent = ConversationalAgent(client=mock)
|
||||
result = agent.chat("Hello")
|
||||
|
||||
assert "Hello" in result.get("response", "")
|
||||
assert mock.call_count == 1
|
||||
|
||||
def test_api_error_returns_error_message(self):
|
||||
"""Test that when API returns an error (like 529 overloaded), we return an error message."""
|
||||
mock = MockMiniMaxClient()
|
||||
|
||||
# API returns an error on all 3 retry attempts
|
||||
for _ in range(3):
|
||||
mock.add_response({
|
||||
"error": "API returned 529: Server overloaded"
|
||||
})
|
||||
|
||||
agent = ConversationalAgent(client=mock)
|
||||
result = agent.chat("Hello")
|
||||
|
||||
# Should return error message, not empty string
|
||||
response = result.get("response", "")
|
||||
print(f"Response: {response}")
|
||||
print(f"Call count: {mock.call_count}")
|
||||
|
||||
assert response != "", "Response should not be empty when API errors"
|
||||
assert "trouble" in response.lower() or "error" in response.lower() or "sorry" in response.lower(), \
|
||||
f"Response should mention error: {response}"
|
||||
assert result.get("success") == False
|
||||
|
||||
def test_api_empty_choices_returns_error_message(self):
|
||||
"""Test that when API returns choices=None (like 520 error), we return an error message."""
|
||||
mock = MockMiniMaxClient()
|
||||
|
||||
# API returns empty choices on all 3 retry attempts
|
||||
for _ in range(3):
|
||||
mock.add_response({
|
||||
"id": "test-id",
|
||||
"choices": None, # Empty choices = API error
|
||||
"model": "MiniMax-M2.7",
|
||||
"base_resp": {"status_code": 1000, "status_msg": "unknown error, 520"}
|
||||
})
|
||||
|
||||
agent = ConversationalAgent(client=mock)
|
||||
result = agent.chat("Hello")
|
||||
|
||||
# Should return error message, not empty string
|
||||
response = result.get("response", "")
|
||||
print(f"Response: {response}")
|
||||
print(f"Call count: {mock.call_count}")
|
||||
|
||||
assert response != "", "Response should not be empty when API returns empty choices"
|
||||
assert "trouble" in response.lower() or "error" in response.lower() or "sorry" in response.lower(), \
|
||||
f"Response should mention error: {response}"
|
||||
assert result.get("success") == False
|
||||
|
||||
def test_tool_call_list_bots(self):
|
||||
"""Test that agent calls list_bots tool when asked about bots."""
|
||||
mock = MockMiniMaxClient()
|
||||
|
||||
# First call: model decides to call list_bots
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"finish_reason": "tool_calls",
|
||||
"message": {
|
||||
"content": "",
|
||||
"role": "assistant",
|
||||
"tool_calls": [{
|
||||
"id": "call_1",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "list_bots",
|
||||
"arguments": "{}"
|
||||
}
|
||||
}]
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
# Second call: after tool result, model gives final response
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"message": {
|
||||
"content": "You don't have any bots yet. Would you like to create one?",
|
||||
"role": "assistant",
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
# Pass anonymous_token so _execute_list_bots doesn't return error
|
||||
agent = ConversationalAgent(client=mock, anonymous_token="test-token")
|
||||
result = agent.chat("What bots do I have?")
|
||||
|
||||
assert mock.call_count == 2
|
||||
# The response should be from the second call
|
||||
assert "bot" in result.get("response", "").lower()
|
||||
|
||||
def test_tool_result_with_empty_content_returns_tool_result(self):
|
||||
"""Test that if second API call returns empty content but has tool_calls, we fallback to tool result."""
|
||||
mock = MockMiniMaxClient()
|
||||
|
||||
# First call: model calls list_bots
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"finish_reason": "tool_calls",
|
||||
"message": {
|
||||
"content": "",
|
||||
"role": "assistant",
|
||||
"tool_calls": [{
|
||||
"id": "call_1",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "list_bots",
|
||||
"arguments": "{}"
|
||||
}
|
||||
}]
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
# Second call: model calls ANOTHER tool (not providing text)
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"finish_reason": "tool_calls",
|
||||
"message": {
|
||||
"content": "\n", # Whitespace only
|
||||
"role": "assistant",
|
||||
"tool_calls": [{
|
||||
"id": "call_2",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_bot_info",
|
||||
"arguments": "{}"
|
||||
}
|
||||
}]
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
agent = ConversationalAgent(client=mock)
|
||||
result = agent.chat("What bots do I have?")
|
||||
|
||||
# Should fallback to the tool result text, not empty string
|
||||
assert result.get("response", "") != ""
|
||||
|
||||
def test_content_with_tool_calls_returns_content(self):
|
||||
"""Test that if second API call returns BOTH content AND tool_calls, we return the content.
|
||||
|
||||
This tests the exact scenario from production:
|
||||
- Tool result: 'Backtest failed: Token address not found...'
|
||||
- Model response: 'Got it! Running the backtest now.' (with tool_calls)
|
||||
- Expected: Since tool result is an ERROR, we should return the error, NOT model content
|
||||
"""
|
||||
mock = MockMiniMaxClient()
|
||||
|
||||
# First call: model calls run_backtest
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"finish_reason": "tool_calls",
|
||||
"message": {
|
||||
"content": "",
|
||||
"role": "assistant",
|
||||
"tool_calls": [{
|
||||
"id": "call_1",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "run_backtest",
|
||||
"arguments": '{"token_address": "0x..."}'
|
||||
}
|
||||
}]
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
# Second call: model returns misleading positive content AND has tool_calls
|
||||
# But tool result was an ERROR, so we should return the error
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"finish_reason": "tool_calls",
|
||||
"message": {
|
||||
"content": "Got it! Running the backtest now.", # Misleading!
|
||||
"role": "assistant",
|
||||
"tool_calls": [{
|
||||
"id": "call_2",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_bot_info",
|
||||
"arguments": "{}"
|
||||
}
|
||||
}]
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
agent = ConversationalAgent(
|
||||
client=mock,
|
||||
conversation_id="test-conv",
|
||||
anonymous_token="test-token"
|
||||
)
|
||||
result = agent.chat("Run backtest on SHIB")
|
||||
|
||||
# Since tool result is an error, we should return the ERROR, not model content
|
||||
response = result.get("response", "")
|
||||
print(f"Response: '{response}'")
|
||||
|
||||
# The key assertion: tool result was an error, so we should return the error
|
||||
assert "not found" in response.lower() or "error" in response.lower() or "couldn't" in response.lower(), \
|
||||
f"Expected error from tool result, got: {response}"
|
||||
assert "Got it" not in response, f"Should NOT return misleading positive content"
|
||||
|
||||
def test_content_without_tool_calls_returns_content(self):
|
||||
"""Test that if second API call returns content WITHOUT tool_calls, we return the content.
|
||||
Note: This test uses list_bots which returns a friendly message, not an error."""
|
||||
mock = MockMiniMaxClient()
|
||||
|
||||
# First call: model calls list_bots
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"finish_reason": "tool_calls",
|
||||
"message": {
|
||||
"content": "",
|
||||
"role": "assistant",
|
||||
"tool_calls": [{
|
||||
"id": "call_1",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "list_bots",
|
||||
"arguments": '{}'
|
||||
}
|
||||
}]
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
# Second call: model returns ONLY content, no tool_calls
|
||||
# Tool result was "You don't have any bots yet" (not an error keyword), use model content
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"finish_reason": "stop",
|
||||
"message": {
|
||||
"content": "You don't have any bots. Would you like to create one?", # Content only
|
||||
"role": "assistant",
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
agent = ConversationalAgent(
|
||||
client=mock,
|
||||
conversation_id="test-conv",
|
||||
anonymous_token="test-token"
|
||||
)
|
||||
result = agent.chat("What bots do I have?")
|
||||
|
||||
response = result.get("response", "")
|
||||
print(f"Response: '{response}'")
|
||||
assert "You don't have any bots" in response
|
||||
|
||||
def test_second_api_call_error_returns_tool_result(self):
|
||||
"""Test that if second API call (in _send_tool_result_to_model) returns error, we return tool result."""
|
||||
mock = MockMiniMaxClient()
|
||||
|
||||
# First call: model calls run_backtest
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"finish_reason": "tool_calls",
|
||||
"message": {
|
||||
"content": "",
|
||||
"role": "assistant",
|
||||
"tool_calls": [{
|
||||
"id": "call_1",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "run_backtest",
|
||||
"arguments": '{}'
|
||||
}
|
||||
}]
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
# Second call: API returns error on all 3 retry attempts
|
||||
for _ in range(3):
|
||||
mock.add_response({
|
||||
"error": "API returned 529: Server overloaded"
|
||||
})
|
||||
|
||||
agent = ConversationalAgent(
|
||||
client=mock,
|
||||
conversation_id="test-conv",
|
||||
anonymous_token="test-token"
|
||||
)
|
||||
result = agent.chat("Run backtest")
|
||||
|
||||
# Should fallback to tool result (backtest not found message), not empty string
|
||||
response = result.get("response", "")
|
||||
print(f"Response: '{response}'")
|
||||
print(f"Call count: {mock.call_count}")
|
||||
|
||||
assert response != "", "Response should not be empty when second API errors"
|
||||
# The tool result should be "I couldn't find the bot..."
|
||||
assert "bot" in response.lower() or "couldn't find" in response.lower(), \
|
||||
f"Response should contain tool result: {response}"
|
||||
|
||||
def test_chained_tool_calls_with_empty_content(self):
|
||||
"""Test that if model returns empty content but has tool_calls, we execute the next tool.
|
||||
|
||||
This is the exact scenario from production:
|
||||
- User asks about bots
|
||||
- Model calls list_bots tool
|
||||
- Tool returns bot list
|
||||
- Model responds with EMPTY content but has get_bot_info tool call
|
||||
- We should execute get_bot_info and continue, NOT return empty string
|
||||
"""
|
||||
mock = MockMiniMaxClient()
|
||||
|
||||
# First call: model decides to call list_bots
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"finish_reason": "tool_calls",
|
||||
"message": {
|
||||
"content": "",
|
||||
"role": "assistant",
|
||||
"tool_calls": [{
|
||||
"id": "call_1",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "list_bots",
|
||||
"arguments": "{}"
|
||||
}
|
||||
}]
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
# Second call: model returns EMPTY content but wants to call get_bot_info
|
||||
# This is the BUG scenario - model just wants to do more tool calls
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"finish_reason": "tool_calls",
|
||||
"message": {
|
||||
"content": "", # Empty content!
|
||||
"role": "assistant",
|
||||
"tool_calls": [{
|
||||
"id": "call_2",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_bot_info",
|
||||
"arguments": '{"bot_id": "test-bot-123"}'
|
||||
}
|
||||
}]
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
# Third call: after get_bot_info, model finally returns content
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"message": {
|
||||
"content": "I found your bot 'Test Bot' with 2 strategy conditions configured.",
|
||||
"role": "assistant",
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
agent = ConversationalAgent(client=mock, anonymous_token="test-token")
|
||||
result = agent.chat("Tell me about my bots")
|
||||
|
||||
response = result.get("response", "")
|
||||
print(f"Response: '{response}'")
|
||||
print(f"Call count: {mock.call_count}")
|
||||
|
||||
# Should NOT return empty string - should continue with tool calls
|
||||
assert response != "", "Response should not be empty when model has more tool calls"
|
||||
# get_bot_info returns error since bot doesn't exist in DB, but we continued the chain
|
||||
# (which is the key behavior we're testing - it didn't return empty string)
|
||||
assert "❌" in response or "bot" in response.lower(), f"Response should mention bot or error: {response}"
|
||||
|
||||
def test_json_response_extraction(self):
|
||||
"""Test that JSON-formatted responses are properly extracted."""
|
||||
mock = MockMiniMaxClient()
|
||||
|
||||
# Model returns JSON in code block with response field
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"message": {
|
||||
"content": "```json\n{\n \"thinking\": \"The user wants a bot. Creating one now.\",\n \"response\": \"✅ Bot created successfully!\"\n}\n```",
|
||||
"role": "assistant",
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
agent = ConversationalAgent(client=mock)
|
||||
result = agent.chat("create a bot")
|
||||
|
||||
response = result.get("response", "")
|
||||
print(f"Response: '{response}'")
|
||||
|
||||
# Should extract the response field, not show JSON
|
||||
assert "✅ Bot created successfully!" in response, f"Should contain bot message: {response}"
|
||||
assert "thinking" not in response.lower() or "json" not in response.lower(), f"Should not contain raw JSON: {response}"
|
||||
assert "```json" not in response, f"Should not contain code block markers: {response}"
|
||||
|
||||
def test_retry_succeeds_on_second_attempt(self):
|
||||
"""Test that if first API call fails but second succeeds, we use the successful response."""
|
||||
mock = MockMiniMaxClient()
|
||||
|
||||
# First attempt fails with error
|
||||
mock.add_response({"error": "API returned 529: Server overloaded"})
|
||||
# Second attempt succeeds
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"message": {
|
||||
"content": "Hello! How can I help you?",
|
||||
"role": "assistant",
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
agent = ConversationalAgent(client=mock)
|
||||
result = agent.chat("Hello")
|
||||
|
||||
response = result.get("response", "")
|
||||
print(f"Response: '{response}'")
|
||||
print(f"Call count: {mock.call_count}")
|
||||
|
||||
# Should use the successful response from 2nd attempt
|
||||
assert "Hello" in response
|
||||
assert "trouble" not in response.lower()
|
||||
assert mock.call_count == 2 # Two calls: 1 error + 1 success
|
||||
|
||||
def test_model_json_response_is_parsed(self):
|
||||
"""Test that if model returns JSON-formatted response in _send_tool_result_to_model, we extract only the response field."""
|
||||
mock = MockMiniMaxClient()
|
||||
|
||||
# User message triggers tool call
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"finish_reason": "tool_calls",
|
||||
"message": {
|
||||
"content": "",
|
||||
"role": "assistant",
|
||||
"tool_calls": [{
|
||||
"id": "call_1",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "create_bot",
|
||||
"arguments": '{"name": "TestBot"}'
|
||||
}
|
||||
}]
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
# Model's second response is JSON (this is the bug scenario)
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"message": {
|
||||
# Model returns JSON instead of plain text
|
||||
"content": '{"thinking": "Bot created", "response": "Your bot TestBot is ready! What strategy?", "strategy_update": null}',
|
||||
"role": "assistant",
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
agent = ConversationalAgent(
|
||||
client=mock,
|
||||
conversation_id="test-conv",
|
||||
anonymous_token="test-token"
|
||||
)
|
||||
|
||||
result = agent.chat("Create a bot named TestBot")
|
||||
|
||||
# The response should NOT contain JSON
|
||||
response_text = result.get("response", "")
|
||||
print(f"Response: {response_text}")
|
||||
|
||||
# Should NOT contain JSON artifacts
|
||||
assert "{\"thinking\":" not in response_text
|
||||
assert "\"strategy_update\":" not in response_text
|
||||
assert "```json" not in response_text
|
||||
|
||||
# Should contain the actual response text
|
||||
assert "bot TestBot is ready" in response_text or "strategy" in response_text.lower()
|
||||
|
||||
def test_create_bot_flow(self):
|
||||
"""Test the full flow of creating a bot."""
|
||||
mock = MockMiniMaxClient()
|
||||
|
||||
# First call: model asks for bot name
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"message": {
|
||||
"content": "I'd be happy to help you create a trading bot! What would you like to name it?",
|
||||
"role": "assistant",
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
# Second call: user provides name, model calls create_bot
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"finish_reason": "tool_calls",
|
||||
"message": {
|
||||
"content": "",
|
||||
"role": "assistant",
|
||||
"tool_calls": [{
|
||||
"id": "call_create",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "create_bot",
|
||||
"arguments": '{"name": "TestBot"}'
|
||||
}
|
||||
}]
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
# Third call: after create_bot result, model gives PLAIN TEXT response (not JSON)
|
||||
mock.add_response({
|
||||
"choices": [{
|
||||
"message": {
|
||||
"content": "✅ Your bot 'TestBot' has been created! Now let's set up your trading strategy.",
|
||||
"role": "assistant",
|
||||
}
|
||||
}]
|
||||
})
|
||||
|
||||
agent = ConversationalAgent(
|
||||
client=mock,
|
||||
conversation_id="test-conv-123",
|
||||
anonymous_token="test-token"
|
||||
)
|
||||
|
||||
# First message - greeting
|
||||
result1 = agent.chat("I want to run a backtest")
|
||||
assert mock.call_count == 1
|
||||
|
||||
# Second message - bot name
|
||||
result2 = agent.chat("MyBot")
|
||||
assert mock.call_count == 3 # Two calls due to tool execution
|
||||
|
||||
assert "bot" in result2.get("response", "").lower()
|
||||
|
||||
|
||||
class TestMockMiniMaxClient:
|
||||
"""Test the mock client itself."""
|
||||
|
||||
def test_add_and_retrieve_responses(self):
|
||||
"""Test that responses are returned in order."""
|
||||
mock = MockMiniMaxClient()
|
||||
mock.add_response({"result": "first"})
|
||||
mock.add_response({"result": "second"})
|
||||
|
||||
assert mock.chat({}, "") == {"result": "first"}
|
||||
assert mock.chat({}, "") == {"result": "second"}
|
||||
|
||||
def test_calls_are_recorded(self):
|
||||
"""Test that all calls are recorded."""
|
||||
mock = MockMiniMaxClient()
|
||||
mock.add_response({"result": "ok"})
|
||||
|
||||
mock.chat(["msg1"], "system")
|
||||
mock.chat(["msg2"], "system")
|
||||
|
||||
assert len(mock.calls) == 2
|
||||
assert mock.calls[0]["messages"] == ["msg1"]
|
||||
assert mock.calls[1]["messages"] == ["msg2"]
|
||||
|
||||
def test_default_response_when_exhausted(self):
|
||||
"""Test default response when all predefined responses are used."""
|
||||
mock = MockMiniMaxClient()
|
||||
mock.add_response({"result": "only"})
|
||||
|
||||
result1 = mock.chat([], "")
|
||||
result2 = mock.chat([], "") # No more responses
|
||||
|
||||
assert result1 == {"result": "only"}
|
||||
assert "Mock response" in result2["choices"][0]["message"]["content"]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
505
src/backend/tests/test_backtest_engine.py
Normal file
505
src/backend/tests/test_backtest_engine.py
Normal file
@@ -0,0 +1,505 @@
|
||||
import pytest
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the backend directory to path
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'app'))
|
||||
|
||||
from app.services.backtest.engine import BacktestEngine
|
||||
|
||||
|
||||
def create_klines(start_price, num_klines, interval=1.0, volatility=0.01):
|
||||
"""Helper to create kline data with predictable price movements.
|
||||
|
||||
Args:
|
||||
start_price: Starting price
|
||||
num_klines: Number of klines to create
|
||||
interval: Price change per kline (can be positive or negative)
|
||||
volatility: Random noise factor (0.01 = 1%)
|
||||
"""
|
||||
import random
|
||||
klines = []
|
||||
price = start_price
|
||||
base_time = 1704067200 # 2024-01-01 00:00:00 UTC
|
||||
|
||||
for i in range(num_klines):
|
||||
# Add some randomness
|
||||
noise = (random.random() - 0.5) * 2 * volatility * price
|
||||
price = max(0.00000001, price + interval + noise)
|
||||
|
||||
open_price = price - (random.random() * volatility * price)
|
||||
close_price = price
|
||||
high_price = max(open_price, close_price) + (random.random() * volatility * price)
|
||||
low_price = min(open_price, close_price) - (random.random() * volatility * price)
|
||||
|
||||
klines.append({
|
||||
"open": str(open_price),
|
||||
"high": str(high_price),
|
||||
"low": str(low_price),
|
||||
"close": str(close_price),
|
||||
"volume": str(1000 + random.random() * 100),
|
||||
"amount": str(1000 * price),
|
||||
"time": base_time + (i * 3600) # 1 hour intervals
|
||||
})
|
||||
|
||||
return klines
|
||||
|
||||
|
||||
class MockAveClient:
|
||||
"""Mock AVE client that returns test klines."""
|
||||
|
||||
def __init__(self, klines):
|
||||
self.klines = klines
|
||||
|
||||
async def get_klines(self, token_id, interval, limit, start_time=None, end_time=None):
|
||||
return self.klines
|
||||
|
||||
|
||||
class TestBacktestEngine:
|
||||
"""Test cases for BacktestEngine."""
|
||||
|
||||
@pytest.fixture
|
||||
def base_config(self):
|
||||
"""Base config for backtest."""
|
||||
return {
|
||||
"bot_id": "test-bot-123",
|
||||
"token": "0xtest",
|
||||
"chain": "bsc",
|
||||
"timeframe": "1h",
|
||||
"start_date": "2024-01-01",
|
||||
"end_date": "2024-01-02",
|
||||
"ave_api_key": "test-key",
|
||||
"ave_api_plan": "free",
|
||||
"initial_balance": 10000.0,
|
||||
}
|
||||
|
||||
@pytest.fixture
|
||||
def simple_strategy(self):
|
||||
"""Simple strategy: buy on 1% drop, no auto sell."""
|
||||
return {
|
||||
"conditions": [
|
||||
{"type": "price_drop", "token": "TEST", "token_address": "0xtest", "threshold": 1.0}
|
||||
],
|
||||
"actions": [
|
||||
{"type": "buy", "amount_percent": 10}
|
||||
],
|
||||
"risk_management": {}
|
||||
}
|
||||
|
||||
@pytest.fixture
|
||||
def partial_sell_strategy(self):
|
||||
"""Strategy with partial sells: buy on 1% drop, sell 50% on rise (via risk management take profit)."""
|
||||
return {
|
||||
"conditions": [
|
||||
{"type": "price_drop", "token": "TEST", "token_address": "0xtest", "threshold": 1.0}
|
||||
],
|
||||
"actions": [
|
||||
{"type": "buy", "amount_percent": 10}
|
||||
],
|
||||
"risk_management": {
|
||||
"take_profit_percent": 1.5 # 1.5% take profit to trigger on price rise
|
||||
}
|
||||
}
|
||||
|
||||
@pytest.fixture
|
||||
def stop_loss_strategy(self):
|
||||
"""Strategy with stop loss and take profit."""
|
||||
return {
|
||||
"conditions": [
|
||||
{"type": "price_drop", "token": "TEST", "token_address": "0xtest", "threshold": 1.0}
|
||||
],
|
||||
"actions": [
|
||||
{"type": "buy", "amount_percent": 10}
|
||||
],
|
||||
"risk_management": {
|
||||
"stop_loss_percent": 5, # 5% stop loss
|
||||
"take_profit_percent": 10 # 10% take profit
|
||||
}
|
||||
}
|
||||
|
||||
def test_single_buy_and_hold(self, base_config, simple_strategy):
|
||||
"""Test buying once and holding (no sell triggers)."""
|
||||
# Create klines that drop 0.5% each (below 1% threshold, no buy)
|
||||
# Then rise 0.5% each (still no sell since no position)
|
||||
klines = create_klines(100, 10, interval=0.5) # Rising trend
|
||||
|
||||
config = {**base_config, "strategy_config": simple_strategy}
|
||||
engine = BacktestEngine(config)
|
||||
engine.ave_client = MockAveClient(klines)
|
||||
|
||||
results = asyncio.run(engine.run())
|
||||
|
||||
print(f"Results: {results}")
|
||||
# Should have 0 trades since price never dropped 1%
|
||||
assert results.get("total_trades") == 0
|
||||
assert results.get("final_balance") == 10000.0
|
||||
|
||||
def test_multiple_dips_multiple_buys(self, base_config, simple_strategy):
|
||||
"""Test multiple dips triggering multiple buys (DCA)."""
|
||||
# Create price that drops 1.5% then rises 0.5%, repeats
|
||||
# This should trigger buy on each drop
|
||||
klines = []
|
||||
price = 100.0
|
||||
base_time = 1704067200
|
||||
|
||||
for i in range(20):
|
||||
if i % 3 == 0:
|
||||
# Drop by 1.5%
|
||||
price = price * 0.985 # 1.5% drop
|
||||
else:
|
||||
# Rise by 0.5%
|
||||
price = price * 1.005
|
||||
|
||||
klines.append({
|
||||
"open": str(price * 0.99),
|
||||
"high": str(price * 1.01),
|
||||
"low": str(price * 0.98),
|
||||
"close": str(price),
|
||||
"volume": "1000",
|
||||
"amount": str(1000 * price),
|
||||
"time": base_time + (i * 3600)
|
||||
})
|
||||
|
||||
config = {**base_config, "strategy_config": simple_strategy}
|
||||
engine = BacktestEngine(config)
|
||||
engine.ave_client = MockAveClient(klines)
|
||||
|
||||
results = asyncio.run(engine.run())
|
||||
|
||||
print(f"Results: {results}")
|
||||
print(f"Trades: {engine.trades}")
|
||||
|
||||
# Should have multiple buys (6-7 dips at 1.5% threshold out of ~7 drops)
|
||||
assert results.get("total_trades") >= 6
|
||||
buy_trades = [t for t in engine.trades if t["type"] == "buy"]
|
||||
assert len(buy_trades) >= 6
|
||||
|
||||
# Should end with position > 0 (still holding since no sell triggers)
|
||||
assert engine.position > 0
|
||||
|
||||
def test_partial_sells(self, base_config, partial_sell_strategy):
|
||||
"""Test partial sells - selling some portion via take profit."""
|
||||
# Create: drop 1.5% (buy), rise 1.5% (sell via take profit), drop 1.5% (buy), rise 1.5% (sell via take profit)
|
||||
klines = []
|
||||
price = 100.0
|
||||
base_time = 1704067200
|
||||
|
||||
for i in range(8):
|
||||
if i % 2 == 0:
|
||||
# Even: drop 1.5% (should trigger buy)
|
||||
price = price * 0.985
|
||||
else:
|
||||
# Odd: rise 1.5% (should trigger take profit sell)
|
||||
price = price * 1.015
|
||||
|
||||
klines.append({
|
||||
"open": str(price * 0.99),
|
||||
"high": str(price * 1.01),
|
||||
"low": str(price * 0.98),
|
||||
"close": str(price),
|
||||
"volume": "1000",
|
||||
"amount": str(1000 * price),
|
||||
"time": base_time + (i * 3600)
|
||||
})
|
||||
|
||||
config = {**base_config, "strategy_config": partial_sell_strategy}
|
||||
engine = BacktestEngine(config)
|
||||
engine.ave_client = MockAveClient(klines)
|
||||
|
||||
results = asyncio.run(engine.run())
|
||||
|
||||
print(f"Results: {results}")
|
||||
print(f"Trades: {engine.trades}")
|
||||
|
||||
# Multiple cycles happen due to price oscillation
|
||||
# At minimum we should have some trades
|
||||
assert results.get("total_trades") >= 2
|
||||
|
||||
buy_trades = [t for t in engine.trades if t["type"] == "buy"]
|
||||
sell_trades = [t for t in engine.trades if t["type"] == "sell"]
|
||||
|
||||
# Should have executed some trades
|
||||
assert len(buy_trades) >= 1
|
||||
assert len(sell_trades) >= 1
|
||||
|
||||
# Should have made profit from the sells
|
||||
assert results.get("total_return") > 0
|
||||
|
||||
def test_stop_loss_trigger(self, base_config, stop_loss_strategy):
|
||||
"""Test stop loss triggers correctly."""
|
||||
# Create: buy, then continue dropping to trigger stop loss
|
||||
klines = []
|
||||
base_time = 1704067200
|
||||
|
||||
# Kline 0: reference price (skipped due to idx=0 check)
|
||||
klines.append({
|
||||
"open": "100",
|
||||
"high": "101",
|
||||
"low": "99",
|
||||
"close": "100",
|
||||
"volume": "1000",
|
||||
"amount": "100000",
|
||||
"time": base_time
|
||||
})
|
||||
|
||||
# Kline 1: drop triggers buy at 98.5
|
||||
klines.append({
|
||||
"open": "100",
|
||||
"high": "101",
|
||||
"low": "98",
|
||||
"close": "98.5", # 1.5% drop from 100
|
||||
"volume": "1000",
|
||||
"amount": "98500",
|
||||
"time": base_time + 3600
|
||||
})
|
||||
|
||||
# Kline 2: price rises slightly, no condition trigger
|
||||
klines.append({
|
||||
"open": "98.5",
|
||||
"high": "100",
|
||||
"low": "98",
|
||||
"close": "99",
|
||||
"volume": "1000",
|
||||
"amount": "99000",
|
||||
"time": base_time + 7200
|
||||
})
|
||||
|
||||
# Kline 3: price drops below stop loss
|
||||
# Entry was 98.5, stop loss is 5%, so SL = 98.5 * 0.95 = 93.575
|
||||
# This close (92) is below SL
|
||||
klines.append({
|
||||
"open": "99",
|
||||
"high": "100",
|
||||
"low": "90", # Well below stop loss
|
||||
"close": "92", # Below stop loss (93.575)
|
||||
"volume": "1000",
|
||||
"amount": "92000",
|
||||
"time": base_time + 10800
|
||||
})
|
||||
|
||||
config = {**base_config, "strategy_config": stop_loss_strategy}
|
||||
engine = BacktestEngine(config)
|
||||
engine.ave_client = MockAveClient(klines)
|
||||
|
||||
results = asyncio.run(engine.run())
|
||||
|
||||
print(f"Results: {results}")
|
||||
print(f"Trades: {engine.trades}")
|
||||
print(f"DEBUG: stop_loss_percent = {engine.stop_loss_percent}")
|
||||
print(f"DEBUG: take_profit_percent = {engine.take_profit_percent}")
|
||||
|
||||
# Should have 2 trades: 1 buy, 1 sell (stop loss)
|
||||
assert results.get("total_trades") == 2
|
||||
|
||||
sell_trades = [t for t in engine.trades if t["type"] == "sell"]
|
||||
assert len(sell_trades) == 1
|
||||
assert sell_trades[0]["exit_reason"] == "stop_loss"
|
||||
|
||||
# Should have lost money (stop loss triggered)
|
||||
assert results.get("total_return") < 0
|
||||
assert results.get("final_balance") < 10000.0
|
||||
|
||||
sell_trades = [t for t in engine.trades if t["type"] == "sell"]
|
||||
assert len(sell_trades) == 1
|
||||
assert sell_trades[0]["exit_reason"] == "stop_loss"
|
||||
|
||||
# Should have lost money (stop loss triggered)
|
||||
assert results.get("total_return") < 0
|
||||
assert results.get("final_balance") < 10000.0
|
||||
|
||||
def test_take_profit_trigger(self, base_config, stop_loss_strategy):
|
||||
"""Test take profit triggers correctly."""
|
||||
# Create: buy, then price rises to trigger take profit
|
||||
klines = []
|
||||
base_time = 1704067200
|
||||
|
||||
# Kline 0: reference price (skipped due to idx=0)
|
||||
klines.append({
|
||||
"open": "100",
|
||||
"high": "101",
|
||||
"low": "99",
|
||||
"close": "100",
|
||||
"volume": "1000",
|
||||
"amount": "100000",
|
||||
"time": base_time
|
||||
})
|
||||
|
||||
# Kline 1: drop triggers buy at 98.5 (1.5% drop from 100)
|
||||
klines.append({
|
||||
"open": "100",
|
||||
"high": "101",
|
||||
"low": "98",
|
||||
"close": "98.5",
|
||||
"volume": "1000",
|
||||
"amount": "98500",
|
||||
"time": base_time + 3600
|
||||
})
|
||||
|
||||
# Kline 2: price stays roughly flat
|
||||
klines.append({
|
||||
"open": "98.5",
|
||||
"high": "99",
|
||||
"low": "98",
|
||||
"close": "98.8",
|
||||
"volume": "1000",
|
||||
"amount": "98800",
|
||||
"time": base_time + 7200
|
||||
})
|
||||
|
||||
# Kline 3: price rises above take profit
|
||||
# Entry was 98.5, take profit is 10%, so TP = 98.5 * 1.10 = 108.35
|
||||
klines.append({
|
||||
"open": "98.8",
|
||||
"high": "120", # Way above TP
|
||||
"low": "98",
|
||||
"close": "115", # Above 108.35
|
||||
"volume": "1000",
|
||||
"amount": "115000",
|
||||
"time": base_time + 10800
|
||||
})
|
||||
|
||||
config = {**base_config, "strategy_config": stop_loss_strategy}
|
||||
engine = BacktestEngine(config)
|
||||
engine.ave_client = MockAveClient(klines)
|
||||
|
||||
results = asyncio.run(engine.run())
|
||||
|
||||
print(f"Results: {results}")
|
||||
print(f"Trades: {engine.trades}")
|
||||
print(f"DEBUG: stop_loss_percent = {engine.stop_loss_percent}")
|
||||
print(f"DEBUG: take_profit_percent = {engine.take_profit_percent}")
|
||||
|
||||
# Should have 2 trades: 1 buy, 1 sell (take profit)
|
||||
assert results.get("total_trades") == 2
|
||||
|
||||
sell_trades = [t for t in engine.trades if t["type"] == "sell"]
|
||||
assert len(sell_trades) == 1
|
||||
assert sell_trades[0]["exit_reason"] == "take_profit"
|
||||
|
||||
# Should have made profit (take profit triggered)
|
||||
assert results.get("total_return") > 0
|
||||
assert results.get("final_balance") > 10000.0
|
||||
|
||||
def test_full_cycle_dip_buy_sell(self, base_config):
|
||||
"""Test a complete cycle: buy on dip, then sell via take profit."""
|
||||
# Strategy: buy 10% on 1% dip, take profit 2% to sell
|
||||
strategy = {
|
||||
"conditions": [
|
||||
{"type": "price_drop", "token": "TEST", "token_address": "0xtest", "threshold": 1.0}
|
||||
],
|
||||
"actions": [
|
||||
{"type": "buy", "amount_percent": 10}
|
||||
],
|
||||
"risk_management": {
|
||||
"take_profit_percent": 2.0 # Sell when price rises 2%
|
||||
}
|
||||
}
|
||||
|
||||
klines = []
|
||||
base_time = 1704067200
|
||||
|
||||
# Klines:
|
||||
# 0: price 100 (reference)
|
||||
# 1: price 98.5 - 1.5% drop, triggers buy at 98.5
|
||||
# 2: price 100.5 - 2% rise from 98.5 = 100.47, triggers take profit
|
||||
klines.append({
|
||||
"open": "100", "high": "101", "low": "99", "close": "100",
|
||||
"volume": "1000", "amount": "100000", "time": base_time
|
||||
})
|
||||
klines.append({
|
||||
"open": "100", "high": "101", "low": "97.5", "close": "98.5",
|
||||
"volume": "1000", "amount": "98500", "time": base_time + 3600
|
||||
})
|
||||
klines.append({
|
||||
"open": "98.5", "high": "101", "low": "98", "close": "100.5",
|
||||
"volume": "1000", "amount": "100500", "time": base_time + 7200
|
||||
})
|
||||
|
||||
config = {**base_config, "strategy_config": strategy}
|
||||
engine = BacktestEngine(config)
|
||||
engine.ave_client = MockAveClient(klines)
|
||||
|
||||
results = asyncio.run(engine.run())
|
||||
|
||||
print(f"Results: {results}")
|
||||
print(f"Trades: {engine.trades}")
|
||||
|
||||
# Should have 2 trades: 1 buy, 1 sell (take profit)
|
||||
assert results.get("total_trades") == 2
|
||||
|
||||
buy_trades = [t for t in engine.trades if t["type"] == "buy"]
|
||||
sell_trades = [t for t in engine.trades if t["type"] == "sell"]
|
||||
|
||||
assert len(buy_trades) == 1
|
||||
assert len(sell_trades) == 1
|
||||
assert sell_trades[0]["exit_reason"] == "take_profit"
|
||||
|
||||
# Should have made profit
|
||||
assert results.get("total_return") > 0
|
||||
|
||||
def test_multiple_buys_then_multiple_sells(self, base_config):
|
||||
"""Test multiple buys followed by multiple sells via take profit."""
|
||||
# Create strategy: buy 10% on 2% dip, take profit 2% to sell
|
||||
strategy = {
|
||||
"conditions": [
|
||||
{"type": "price_drop", "token": "TEST", "token_address": "0xtest", "threshold": 2.0}
|
||||
],
|
||||
"actions": [
|
||||
{"type": "buy", "amount_percent": 10}
|
||||
],
|
||||
"risk_management": {
|
||||
"take_profit_percent": 2.5 # Sell when price rises 2.5% (slightly above entry drop)
|
||||
}
|
||||
}
|
||||
|
||||
# Price pattern: drop 2.5% (buy), rise 2.5% (take profit sells), repeat 3 times
|
||||
klines = []
|
||||
base_time = 1704067200
|
||||
price = 100.0
|
||||
|
||||
for i in range(12):
|
||||
if i % 2 == 0:
|
||||
price = price * 0.975 # Drop 2.5% - triggers buy
|
||||
else:
|
||||
price = price * 1.026 # Rise 2.5% - triggers take profit sell
|
||||
|
||||
klines.append({
|
||||
"open": str(price * 0.99),
|
||||
"high": str(price * 1.01),
|
||||
"low": str(price * 0.98),
|
||||
"close": str(price),
|
||||
"volume": "1000",
|
||||
"amount": str(1000 * price),
|
||||
"time": base_time + (i * 3600)
|
||||
})
|
||||
|
||||
config = {**base_config, "strategy_config": strategy}
|
||||
engine = BacktestEngine(config)
|
||||
engine.ave_client = MockAveClient(klines)
|
||||
|
||||
results = asyncio.run(engine.run())
|
||||
|
||||
print(f"Results: {results}")
|
||||
print(f"Trades: {engine.trades}")
|
||||
|
||||
# Price oscillates creating multiple dip/sell cycles
|
||||
# Should have 10 trades: 5 buys, 5 sells (via take profit)
|
||||
assert results.get("total_trades") == 10
|
||||
|
||||
buy_trades = [t for t in engine.trades if t["type"] == "buy"]
|
||||
sell_trades = [t for t in engine.trades if t["type"] == "sell"]
|
||||
|
||||
assert len(buy_trades) == 5
|
||||
assert len(sell_trades) == 5
|
||||
|
||||
# Position should be 0 after all sells
|
||||
assert engine.position == 0
|
||||
|
||||
# Should have profitable trades
|
||||
assert results.get("total_return") > 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
386
src/backend/tests/test_simulate_engine.py
Normal file
386
src/backend/tests/test_simulate_engine.py
Normal file
@@ -0,0 +1,386 @@
|
||||
import pytest
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import sys
|
||||
sys.path.insert(0, 'src/backend')
|
||||
|
||||
from app.services.simulate.engine import SimulateEngine
|
||||
|
||||
|
||||
class MockAveClient:
|
||||
"""Mock AVE client for testing."""
|
||||
|
||||
def __init__(self, klines_data=None):
|
||||
self.klines_data = klines_data or []
|
||||
|
||||
async def get_klines(self, token_id, interval="1m", limit=100, start_time=None, end_time=None):
|
||||
return self.klines_data
|
||||
|
||||
|
||||
def create_engine(config_override=None, klines_data=None):
|
||||
"""Create a test engine with mock client."""
|
||||
config = {
|
||||
"bot_id": "test-bot",
|
||||
"token": "0x1234567890123456789012345678901234567890",
|
||||
"chain": "bsc",
|
||||
"kline_interval": "1m",
|
||||
"max_candles": 10, # Small number for fast tests
|
||||
"candle_delay": 0, # No delay in tests
|
||||
"auto_execute": False,
|
||||
"strategy_config": {
|
||||
"conditions": [
|
||||
{"type": "price_drop", "threshold": 5, "token": "TEST", "token_address": "0x1234"}
|
||||
],
|
||||
"actions": [
|
||||
{"type": "buy", "amount_percent": 10}
|
||||
],
|
||||
"risk_management": {
|
||||
"stop_loss_percent": 5,
|
||||
"take_profit_percent": 10
|
||||
}
|
||||
},
|
||||
"ave_api_key": "test",
|
||||
"ave_api_plan": "free",
|
||||
}
|
||||
if config_override:
|
||||
config.update(config_override)
|
||||
|
||||
engine = SimulateEngine(config)
|
||||
engine.ave_client = MockAveClient(klines_data)
|
||||
return engine
|
||||
|
||||
|
||||
class TestSimulateEngine:
|
||||
"""Unit tests for SimulateEngine."""
|
||||
|
||||
# ==================== Kline Fetching Tests ====================
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_fetches_klines_on_start(self):
|
||||
"""Engine should fetch klines when run is called."""
|
||||
klines = [
|
||||
{"time": 1000, "open": 100, "high": 105, "low": 98, "close": 102, "volume": 1000},
|
||||
{"time": 2000, "open": 102, "high": 107, "low": 100, "close": 104, "volume": 1100},
|
||||
]
|
||||
engine = create_engine(klines_data=klines)
|
||||
engine.running = True
|
||||
|
||||
results = await engine.run()
|
||||
|
||||
assert engine.status == "completed"
|
||||
assert results["candles_processed"] == 2
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handles_no_klines_data(self):
|
||||
"""Engine should handle empty klines gracefully."""
|
||||
engine = create_engine(klines_data=[])
|
||||
engine.running = True
|
||||
|
||||
results = await engine.run()
|
||||
|
||||
assert engine.status == "failed"
|
||||
assert "error" in results
|
||||
assert "No kline data" in results["error"]
|
||||
|
||||
# ==================== Price Drop Condition Tests ====================
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_price_drop_condition_triggers_buy(self):
|
||||
"""Price drop >= threshold should trigger BUY signal."""
|
||||
# Price drops from 100 to 90 (10% drop) - should trigger 5% threshold
|
||||
klines = [
|
||||
{"time": 1000, "open": 100, "high": 102, "low": 99, "close": 100, "volume": 1000},
|
||||
{"time": 2000, "open": 100, "high": 101, "low": 89, "close": 90, "volume": 1200}, # 10% drop
|
||||
]
|
||||
engine = create_engine(klines_data=klines)
|
||||
engine.running = True
|
||||
|
||||
results = await engine.run()
|
||||
|
||||
assert results["total_signals"] >= 1
|
||||
buy_signals = [s for s in engine.signals if s["signal_type"] == "buy"]
|
||||
assert len(buy_signals) >= 1
|
||||
assert buy_signals[0]["price"] == 90.0
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_price_drop_below_threshold_no_signal(self):
|
||||
"""Price drop < threshold should NOT trigger signal."""
|
||||
# Price drops from 100 to 98 (2% drop) - below 5% threshold
|
||||
klines = [
|
||||
{"time": 1000, "open": 100, "high": 101, "low": 99, "close": 100, "volume": 1000},
|
||||
{"time": 2000, "open": 100, "high": 101, "low": 97, "close": 98, "volume": 1000}, # 2% drop
|
||||
]
|
||||
engine = create_engine(klines_data=klines)
|
||||
engine.running = True
|
||||
|
||||
results = await engine.run()
|
||||
|
||||
assert results["total_signals"] == 0
|
||||
|
||||
# ==================== Risk Management Tests ====================
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stop_loss_triggers_after_buy(self):
|
||||
"""Stop loss should trigger SELL after price drops below threshold."""
|
||||
klines = [
|
||||
{"time": 1000, "open": 100, "high": 102, "low": 99, "close": 100, "volume": 1000},
|
||||
{"time": 2000, "open": 100, "high": 101, "low": 89, "close": 90, "volume": 1200}, # BUY triggered @ 90
|
||||
{"time": 3000, "open": 90, "high": 91, "low": 84, "close": 85, "volume": 1300}, # Stop loss @ 85.5 (90 * 0.95)
|
||||
]
|
||||
engine = create_engine(klines_data=klines)
|
||||
engine.running = True
|
||||
|
||||
results = await engine.run()
|
||||
|
||||
buy_signals = [s for s in engine.signals if s["signal_type"] == "buy"]
|
||||
sell_signals = [s for s in engine.signals if s["signal_type"] == "sell"]
|
||||
|
||||
assert len(buy_signals) >= 1, "Should have at least one BUY signal"
|
||||
assert len(sell_signals) >= 1, "Stop loss should trigger SELL"
|
||||
assert "stop_loss" in sell_signals[0]["reasoning"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_take_profit_triggers_after_buy(self):
|
||||
"""Take profit should trigger SELL after price rises above threshold."""
|
||||
klines = [
|
||||
{"time": 1000, "open": 100, "high": 102, "low": 99, "close": 100, "volume": 1000},
|
||||
{"time": 2000, "open": 100, "high": 101, "low": 89, "close": 90, "volume": 1200}, # BUY triggered @ 90
|
||||
{"time": 3000, "open": 90, "high": 101, "low": 89, "close": 100, "volume": 1300}, # TP @ 99 (90 * 1.10)
|
||||
]
|
||||
engine = create_engine(klines_data=klines)
|
||||
engine.running = True
|
||||
|
||||
results = await engine.run()
|
||||
|
||||
buy_signals = [s for s in engine.signals if s["signal_type"] == "buy"]
|
||||
sell_signals = [s for s in engine.signals if s["signal_type"] == "sell"]
|
||||
|
||||
assert len(buy_signals) >= 1, "Should have at least one BUY signal"
|
||||
assert len(sell_signals) >= 1, "Take profit should trigger SELL"
|
||||
assert "take_profit" in sell_signals[0]["reasoning"]
|
||||
|
||||
# ==================== Multiple Conditions Tests ====================
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_no_buy_if_already_in_position(self):
|
||||
"""Should not trigger another BUY if already holding position."""
|
||||
klines = [
|
||||
{"time": 1000, "open": 100, "high": 102, "low": 99, "close": 100, "volume": 1000},
|
||||
{"time": 2000, "open": 100, "high": 101, "low": 89, "close": 90, "volume": 1200}, # BUY triggered
|
||||
{"time": 3000, "open": 90, "high": 91, "low": 85, "close": 86, "volume": 1300}, # Another drop but already in position
|
||||
{"time": 4000, "open": 86, "high": 87, "low": 81, "close": 82, "volume": 1400}, # Another drop
|
||||
]
|
||||
engine = create_engine(klines_data=klines)
|
||||
engine.running = True
|
||||
|
||||
results = await engine.run()
|
||||
|
||||
buy_signals = [s for s in engine.signals if s["signal_type"] == "buy"]
|
||||
|
||||
# Should only have 1 buy, not multiple
|
||||
assert len(buy_signals) == 1, "Should only have one BUY signal"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_can_buy_again_after_sell(self):
|
||||
"""Should be able to BUY again after position is closed by risk management."""
|
||||
klines = [
|
||||
{"time": 1000, "open": 100, "high": 102, "low": 99, "close": 100, "volume": 1000},
|
||||
# First trade
|
||||
{"time": 2000, "open": 100, "high": 101, "low": 89, "close": 90, "volume": 1200}, # BUY @ 90
|
||||
{"time": 3000, "open": 90, "high": 91, "low": 84, "close": 85, "volume": 1300}, # STOP LOSS @ 85.5
|
||||
# Second trade
|
||||
{"time": 4000, "open": 85, "high": 86, "low": 79, "close": 80, "volume": 1400}, # BUY @ 80 (after position closed)
|
||||
{"time": 5000, "open": 80, "high": 89, "low": 79, "close": 88, "volume": 1500}, # TP @ 88
|
||||
]
|
||||
engine = create_engine(klines_data=klines)
|
||||
engine.running = True
|
||||
|
||||
results = await engine.run()
|
||||
|
||||
buy_signals = [s for s in engine.signals if s["signal_type"] == "buy"]
|
||||
sell_signals = [s for s in engine.signals if s["signal_type"] == "sell"]
|
||||
|
||||
assert len(buy_signals) == 2, "Should have two BUY signals"
|
||||
assert len(sell_signals) == 2, "Should have two SELL signals"
|
||||
|
||||
# ==================== Edge Cases ====================
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handles_zero_price(self):
|
||||
"""Should skip processing for candles with zero price but still count them."""
|
||||
klines = [
|
||||
{"time": 1000, "open": 100, "high": 102, "low": 99, "close": 100, "volume": 1000},
|
||||
{"time": 2000, "open": 0, "high": 0, "low": 0, "close": 0, "volume": 0}, # Skipped in processing
|
||||
{"time": 3000, "open": 100, "high": 101, "low": 89, "close": 90, "volume": 1200}, # This should work
|
||||
]
|
||||
engine = create_engine(klines_data=klines)
|
||||
engine.running = True
|
||||
|
||||
results = await engine.run()
|
||||
|
||||
# All 3 candles counted, but only 2 valid for condition checking
|
||||
assert results["candles_processed"] == 3
|
||||
# Only 1 signal (the valid candle that dropped 10%)
|
||||
assert results["total_signals"] == 1
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_max_candles_limit(self):
|
||||
"""Should respect max_candles limit."""
|
||||
klines = [
|
||||
{"time": i * 1000, "open": 100, "high": 101, "low": 99, "close": 100, "volume": 1000}
|
||||
for i in range(1, 201) # 200 candles
|
||||
]
|
||||
engine = create_engine(klines_data=klines, config_override={"max_candles": 50})
|
||||
engine.running = True
|
||||
|
||||
results = await engine.run()
|
||||
|
||||
assert results["candles_processed"] == 50
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_stop_interrupts_processing(self):
|
||||
"""Should stop processing when stop() is called."""
|
||||
klines = [
|
||||
{"time": i * 1000, "open": 100, "high": 101, "low": 99, "close": 100, "volume": 1000}
|
||||
for i in range(1, 101)
|
||||
]
|
||||
engine = create_engine(klines_data=klines)
|
||||
engine.running = True
|
||||
engine.run_id = "test"
|
||||
|
||||
# Stop after a few candles
|
||||
async def stop_after_delay():
|
||||
await asyncio.sleep(0.1)
|
||||
engine.stop()
|
||||
|
||||
await asyncio.gather(engine.run(), stop_after_delay())
|
||||
|
||||
assert engine.status == "stopped"
|
||||
# Should have processed some candles before stopping
|
||||
assert engine.last_processed_time is not None
|
||||
|
||||
# ==================== Price Movement Display Tests ====================
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_records_all_processed_prices(self):
|
||||
"""Should track last processed time for display purposes."""
|
||||
klines = [
|
||||
{"time": 1000, "open": 100, "high": 102, "low": 99, "close": 100, "volume": 1000},
|
||||
{"time": 2000, "open": 100, "high": 101, "low": 99, "close": 101, "volume": 1100},
|
||||
{"time": 3000, "open": 101, "high": 103, "low": 100, "close": 102, "volume": 1200},
|
||||
]
|
||||
engine = create_engine(klines_data=klines)
|
||||
engine.running = True
|
||||
|
||||
await engine.run()
|
||||
|
||||
# Should have tracked the last candle's time
|
||||
assert engine.last_processed_time == 3000
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_tracks_price_changes(self):
|
||||
"""Should track price changes for potential chart display."""
|
||||
klines = [
|
||||
{"time": 1000, "open": 100, "high": 102, "low": 99, "close": 100, "volume": 1000},
|
||||
{"time": 2000, "open": 100, "high": 105, "low": 99, "close": 104, "volume": 1100},
|
||||
]
|
||||
engine = create_engine(klines_data=klines)
|
||||
engine.running = True
|
||||
|
||||
await engine.run()
|
||||
|
||||
# Last close should be the last candle's close
|
||||
assert engine.last_close == 104.0
|
||||
|
||||
# ==================== Integration Tests ====================
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_full_simulation_workflow_generates_signals_and_trades(self):
|
||||
"""
|
||||
Full integration test: provides klines with clear price movements
|
||||
and verifies signals and trade_log are populated.
|
||||
|
||||
This test ensures the simulation is working by:
|
||||
1. Creating klines with obvious price movements (drops > 0.1%)
|
||||
2. Using a very low threshold (0.1%)
|
||||
3. Verifying signals are generated
|
||||
4. Verifying trade_log is populated
|
||||
5. Verifying we have buy/sell actions
|
||||
"""
|
||||
# Create klines with clear price drops and rises
|
||||
klines = [
|
||||
{"time": 1000, "open": 100, "high": 101, "low": 99, "close": 100, "volume": 1000}, # Flat
|
||||
{"time": 2000, "open": 100, "high": 101, "low": 99.9, "close": 99.95, "volume": 1000}, # 0.05% drop
|
||||
{"time": 3000, "open": 99.95, "high": 100, "low": 99.5, "close": 99.5, "volume": 1000}, # 0.45% drop
|
||||
{"time": 4000, "open": 99.5, "high": 100, "low": 99, "close": 99.2, "volume": 1000}, # 0.30% drop
|
||||
{"time": 5000, "open": 99.2, "high": 100, "low": 98, "close": 98.5, "volume": 1000}, # 0.71% drop
|
||||
{"time": 6000, "open": 98.5, "high": 99, "low": 98, "close": 98.8, "volume": 1000}, # 0.30% rise
|
||||
{"time": 7000, "open": 98.8, "high": 99, "low": 98, "close": 98.3, "volume": 1000}, # 0.51% drop
|
||||
{"time": 8000, "open": 98.3, "high": 99, "low": 97, "close": 97.5, "volume": 1000}, # 0.81% drop
|
||||
{"time": 9000, "open": 97.5, "high": 98, "low": 96, "close": 96.5, "volume": 1000}, # 1.03% drop
|
||||
]
|
||||
|
||||
# Use very low threshold to ensure signals are generated
|
||||
config_override = {
|
||||
"max_candles": 100,
|
||||
"strategy_config": {
|
||||
"conditions": [
|
||||
{"type": "price_drop", "threshold": 0.1, "token": "TEST", "token_address": "0x1234"}
|
||||
],
|
||||
"actions": [
|
||||
{"type": "buy", "amount_percent": 10}
|
||||
],
|
||||
"risk_management": {
|
||||
"stop_loss_percent": 5,
|
||||
"take_profit_percent": 5
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
engine = create_engine(config_override=config_override, klines_data=klines)
|
||||
engine.running = True
|
||||
engine.run_id = "integration-test"
|
||||
|
||||
results = await engine.run()
|
||||
|
||||
# Verify results
|
||||
print(f"\n=== Integration Test Results ===")
|
||||
print(f"Status: {engine.status}")
|
||||
print(f"Candles processed: {results.get('candles_processed')}")
|
||||
print(f"Signals count: {len(engine.signals)}")
|
||||
print(f"Trade log count: {len(engine.trade_log)}")
|
||||
|
||||
# ASSERTIONS - These should NEVER fail if simulation is working
|
||||
assert engine.status == "completed", "Simulation should complete successfully"
|
||||
assert results.get("candles_processed") == len(klines), f"Should process all {len(klines)} candles"
|
||||
|
||||
# Critical: signals should NOT be empty
|
||||
assert len(engine.signals) > 0, "SIGNALS SHOULD NOT BE EMPTY! Simulation is not generating signals."
|
||||
print(f"Signals: {[s['signal_type'] for s in engine.signals]}")
|
||||
|
||||
# Critical: trade_log should NOT be empty
|
||||
assert len(engine.trade_log) > 0, "TRADE_LOG SHOULD NOT BE EMPTY! No activity logged."
|
||||
print(f"Trade log: {[t['action'] for t in engine.trade_log]}")
|
||||
|
||||
# Should have at least one BUY signal
|
||||
buy_signals = [s for s in engine.signals if s['signal_type'] == 'buy']
|
||||
assert len(buy_signals) > 0, "Should have at least one BUY signal"
|
||||
print(f"Buy signals: {len(buy_signals)}")
|
||||
|
||||
# Verify trade_log has BUY action
|
||||
buy_trades = [t for t in engine.trade_log if t['action'] == 'buy']
|
||||
assert len(buy_trades) > 0, "Trade log should contain BUY actions"
|
||||
|
||||
# Verify results contain the data
|
||||
assert "signals" in results, "Results should contain signals"
|
||||
assert "trade_log" in results, "Results should contain trade_log"
|
||||
|
||||
print("\n=== Integration Test PASSED ===")
|
||||
print(f"Simulation working correctly!")
|
||||
print(f"Generated {len(engine.signals)} signals and {len(engine.trade_log)} trade log entries")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
2
src/frontend/.env.example
Normal file
2
src/frontend/.env.example
Normal file
@@ -0,0 +1,2 @@
|
||||
VITE_API_URL=http://localhost:8000/api
|
||||
VITE_WS_URL=ws://localhost:8000/ws
|
||||
23
src/frontend/.gitignore
vendored
Normal file
23
src/frontend/.gitignore
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
node_modules
|
||||
|
||||
# Output
|
||||
.output
|
||||
.vercel
|
||||
.netlify
|
||||
.wrangler
|
||||
/.svelte-kit
|
||||
/build
|
||||
|
||||
# OS
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# Env
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
!.env.test
|
||||
|
||||
# Vite
|
||||
vite.config.js.timestamp-*
|
||||
vite.config.ts.timestamp-*
|
||||
1
src/frontend/.npmrc
Normal file
1
src/frontend/.npmrc
Normal file
@@ -0,0 +1 @@
|
||||
engine-strict=true
|
||||
3
src/frontend/.vscode/extensions.json
vendored
Normal file
3
src/frontend/.vscode/extensions.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"recommendations": ["svelte.svelte-vscode"]
|
||||
}
|
||||
42
src/frontend/README.md
Normal file
42
src/frontend/README.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# sv
|
||||
|
||||
Everything you need to build a Svelte project, powered by [`sv`](https://github.com/sveltejs/cli).
|
||||
|
||||
## Creating a project
|
||||
|
||||
If you're seeing this, you've probably already done this step. Congrats!
|
||||
|
||||
```sh
|
||||
# create a new project
|
||||
npx sv create my-app
|
||||
```
|
||||
|
||||
To recreate this project with the same configuration:
|
||||
|
||||
```sh
|
||||
# recreate this project
|
||||
npx sv@0.15.0 create --template minimal --types ts --no-install .
|
||||
```
|
||||
|
||||
## Developing
|
||||
|
||||
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
|
||||
|
||||
```sh
|
||||
npm run dev
|
||||
|
||||
# or start the server and open the app in a new browser tab
|
||||
npm run dev -- --open
|
||||
```
|
||||
|
||||
## Building
|
||||
|
||||
To create a production version of your app:
|
||||
|
||||
```sh
|
||||
npm run build
|
||||
```
|
||||
|
||||
You can preview the production build with `npm run preview`.
|
||||
|
||||
> To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment.
|
||||
1404
src/frontend/package-lock.json
generated
Normal file
1404
src/frontend/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
26
src/frontend/package.json
Normal file
26
src/frontend/package.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "frontend",
|
||||
"private": true,
|
||||
"version": "0.0.1",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite dev",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview",
|
||||
"prepare": "svelte-kit sync || echo ''",
|
||||
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
||||
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@sveltejs/adapter-auto": "^7.0.1",
|
||||
"@sveltejs/kit": "^2.57.0",
|
||||
"@sveltejs/vite-plugin-svelte": "^7.0.0",
|
||||
"svelte": "^5.55.2",
|
||||
"svelte-check": "^4.4.6",
|
||||
"typescript": "^6.0.2",
|
||||
"vite": "^8.0.7"
|
||||
},
|
||||
"dependencies": {
|
||||
"chart.js": "^4.5.1"
|
||||
}
|
||||
}
|
||||
13
src/frontend/src/app.d.ts
vendored
Normal file
13
src/frontend/src/app.d.ts
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
// See https://svelte.dev/docs/kit/types#app.d.ts
|
||||
// for information about these interfaces
|
||||
declare global {
|
||||
namespace App {
|
||||
// interface Error {}
|
||||
// interface Locals {}
|
||||
// interface PageData {}
|
||||
// interface PageState {}
|
||||
// interface Platform {}
|
||||
}
|
||||
}
|
||||
|
||||
export {};
|
||||
12
src/frontend/src/app.html
Normal file
12
src/frontend/src/app.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta name="text-scale" content="scale" />
|
||||
%sveltekit.head%
|
||||
</head>
|
||||
<body data-sveltekit-preload-data="hover">
|
||||
<div style="display: contents">%sveltekit.body%</div>
|
||||
</body>
|
||||
</html>
|
||||
307
src/frontend/src/lib/api/client.ts
Normal file
307
src/frontend/src/lib/api/client.ts
Normal file
@@ -0,0 +1,307 @@
|
||||
import type {
|
||||
User,
|
||||
Bot,
|
||||
BotConversation,
|
||||
Backtest,
|
||||
Simulation,
|
||||
Signal,
|
||||
AuthResponse,
|
||||
BotChatRequest,
|
||||
BotChatResponse,
|
||||
StrategyConfig,
|
||||
Conversation,
|
||||
ConversationWithMessages,
|
||||
Message
|
||||
} from './types';
|
||||
|
||||
const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000/api';
|
||||
|
||||
function getAuthHeaders(): HeadersInit {
|
||||
const token = localStorage.getItem('token');
|
||||
return token ? { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' } : { 'Content-Type': 'application/json' };
|
||||
}
|
||||
|
||||
class ApiError extends Error {
|
||||
status: number;
|
||||
|
||||
constructor(message: string, status: number) {
|
||||
super(message);
|
||||
this.name = 'ApiError';
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
|
||||
async function handleResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ detail: 'An error occurred' }));
|
||||
let errorMessage = 'An error occurred';
|
||||
|
||||
if (typeof error.detail === 'string') {
|
||||
errorMessage = error.detail;
|
||||
} else if (Array.isArray(error.detail)) {
|
||||
// Handle FastAPI validation error format: [{type, loc, msg, input}]
|
||||
errorMessage = error.detail.map((e: any) => e.msg || JSON.stringify(e)).join(', ');
|
||||
} else if (error.message) {
|
||||
errorMessage = error.message;
|
||||
} else {
|
||||
errorMessage = `HTTP error ${response.status}`;
|
||||
}
|
||||
|
||||
throw new ApiError(errorMessage, response.status);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export const api = {
|
||||
auth: {
|
||||
async register(email: string, password: string): Promise<AuthResponse> {
|
||||
const response = await fetch(`${API_URL}/auth/register`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ email, password })
|
||||
});
|
||||
return handleResponse<AuthResponse>(response);
|
||||
},
|
||||
|
||||
async login(email: string, password: string): Promise<AuthResponse> {
|
||||
const response = await fetch(`${API_URL}/auth/login`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ username: email, password })
|
||||
});
|
||||
return handleResponse<AuthResponse>(response);
|
||||
},
|
||||
|
||||
async logout(): Promise<void> {
|
||||
await fetch(`${API_URL}/auth/logout`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
},
|
||||
|
||||
async me(): Promise<User> {
|
||||
const response = await fetch(`${API_URL}/auth/me`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse<User>(response);
|
||||
}
|
||||
},
|
||||
|
||||
bots: {
|
||||
async list(): Promise<Bot[]> {
|
||||
const response = await fetch(`${API_URL}/bots`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse<Bot[]>(response);
|
||||
},
|
||||
|
||||
async create(name: string, description?: string): Promise<Bot> {
|
||||
const response = await fetch(`${API_URL}/bots`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
body: JSON.stringify({ name, description })
|
||||
});
|
||||
return handleResponse<Bot>(response);
|
||||
},
|
||||
|
||||
async get(id: string): Promise<Bot> {
|
||||
const response = await fetch(`${API_URL}/bots/${id}`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse<Bot>(response);
|
||||
},
|
||||
|
||||
async update(id: string, data: Partial<Bot>): Promise<Bot> {
|
||||
const response = await fetch(`${API_URL}/bots/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: getAuthHeaders(),
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return handleResponse<Bot>(response);
|
||||
},
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/bots/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error ${response.status}`);
|
||||
}
|
||||
},
|
||||
|
||||
async chat(id: string, message: string, signal?: AbortSignal): Promise<BotChatResponse> {
|
||||
const response = await fetch(`${API_URL}/bots/${id}/chat`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
body: JSON.stringify({ message } as BotChatRequest),
|
||||
signal
|
||||
});
|
||||
return handleResponse<BotChatResponse>(response);
|
||||
},
|
||||
|
||||
async getHistory(id: string): Promise<BotConversation[]> {
|
||||
const response = await fetch(`${API_URL}/bots/${id}/history`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse<BotConversation[]>(response);
|
||||
}
|
||||
},
|
||||
|
||||
backtest: {
|
||||
async start(botId: string, config: { token: string; timeframe: string; start_date: string; end_date: string }): Promise<Backtest> {
|
||||
const response = await fetch(`${API_URL}/bots/${botId}/backtest`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
body: JSON.stringify({ ...config, chain: 'bsc' })
|
||||
});
|
||||
return handleResponse<Backtest>(response);
|
||||
},
|
||||
|
||||
async get(botId: string, runId: string): Promise<Backtest> {
|
||||
const response = await fetch(`${API_URL}/bots/${botId}/backtest/${runId}`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse<Backtest>(response);
|
||||
},
|
||||
|
||||
async list(botId: string): Promise<Backtest[]> {
|
||||
const response = await fetch(`${API_URL}/bots/${botId}/backtests`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse<Backtest[]>(response);
|
||||
},
|
||||
|
||||
async stop(botId: string, runId: string): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/bots/${botId}/backtest/${runId}/stop`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error ${response.status}`);
|
||||
}
|
||||
},
|
||||
|
||||
async getTrades(botId: string, runId: string, page: number = 1, perPage: number = 5): Promise<{
|
||||
trades: any[];
|
||||
total_trades: number;
|
||||
page: number;
|
||||
per_page: number;
|
||||
total_pages: number;
|
||||
has_next: boolean;
|
||||
has_prev: boolean;
|
||||
}> {
|
||||
const response = await fetch(`${API_URL}/bots/${botId}/backtest/${runId}/trades?page=${page}&per_page=${perPage}`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
},
|
||||
|
||||
simulate: {
|
||||
async start(botId: string, config: { token: string; chain?: string; kline_interval: string }): Promise<Simulation> {
|
||||
const response = await fetch(`${API_URL}/bots/${botId}/simulate`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
body: JSON.stringify(config)
|
||||
});
|
||||
return handleResponse<Simulation>(response);
|
||||
},
|
||||
|
||||
async get(botId: string, runId: string): Promise<Simulation> {
|
||||
const response = await fetch(`${API_URL}/bots/${botId}/simulate/${runId}`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse<Simulation>(response);
|
||||
},
|
||||
|
||||
async list(botId: string): Promise<Simulation[]> {
|
||||
const response = await fetch(`${API_URL}/bots/${botId}/simulations`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse<Simulation[]>(response);
|
||||
},
|
||||
|
||||
async stop(botId: string, runId: string): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/bots/${botId}/simulate/${runId}/stop`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error ${response.status}`);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
config: {
|
||||
async getChains(): Promise<string[]> {
|
||||
const response = await fetch(`${API_URL}/config/chains`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse<string[]>(response);
|
||||
},
|
||||
|
||||
async getTokens(): Promise<{ symbol: string; chain: string; name: string }[]> {
|
||||
const response = await fetch(`${API_URL}/config/tokens`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse<{ symbol: string; chain: string; name: string }[]>(response);
|
||||
}
|
||||
},
|
||||
|
||||
conversations: {
|
||||
async list(): Promise<Conversation[]> {
|
||||
const response = await fetch(`${API_URL}/conversations`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse<Conversation[]>(response);
|
||||
},
|
||||
|
||||
async create(): Promise<Conversation> {
|
||||
const response = await fetch(`${API_URL}/conversations`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse<Conversation>(response);
|
||||
},
|
||||
|
||||
async get(id: string): Promise<ConversationWithMessages> {
|
||||
const response = await fetch(`${API_URL}/conversations/${id}`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse<ConversationWithMessages>(response);
|
||||
},
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/conversations/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error ${response.status}`);
|
||||
}
|
||||
},
|
||||
|
||||
async chat(id: string, message: string, signal?: AbortSignal): Promise<ConversationWithMessages> {
|
||||
const response = await fetch(`${API_URL}/conversations/${id}/chat`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
body: JSON.stringify({ message }),
|
||||
signal
|
||||
});
|
||||
return handleResponse<ConversationWithMessages>(response);
|
||||
},
|
||||
|
||||
async setBot(id: string, botId: string): Promise<Conversation> {
|
||||
const response = await fetch(`${API_URL}/conversations/${id}/set-bot`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
body: JSON.stringify({ bot_id: botId })
|
||||
});
|
||||
return handleResponse<Conversation>(response);
|
||||
}
|
||||
}
|
||||
};
|
||||
2
src/frontend/src/lib/api/index.ts
Normal file
2
src/frontend/src/lib/api/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { api } from './client';
|
||||
export * from './types';
|
||||
209
src/frontend/src/lib/api/types.ts
Normal file
209
src/frontend/src/lib/api/types.ts
Normal file
@@ -0,0 +1,209 @@
|
||||
export interface User {
|
||||
id: string;
|
||||
email: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export interface Bot {
|
||||
id: string;
|
||||
user_id: string;
|
||||
name: string;
|
||||
description: string | null;
|
||||
strategy_config: StrategyConfig;
|
||||
llm_config: LLMConfig;
|
||||
status: 'draft' | 'active' | 'paused';
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export interface StrategyConfig {
|
||||
conditions: Condition[];
|
||||
actions: Action[];
|
||||
risk_management?: RiskManagement;
|
||||
}
|
||||
|
||||
export interface Condition {
|
||||
type: 'price_drop' | 'price_rise' | 'volume_spike' | 'price_level';
|
||||
token: string;
|
||||
token_address?: string;
|
||||
chain?: string;
|
||||
threshold?: number;
|
||||
price?: number;
|
||||
direction?: 'above' | 'below';
|
||||
timeframe?: string;
|
||||
}
|
||||
|
||||
export interface Action {
|
||||
type: 'buy' | 'sell' | 'hold';
|
||||
amount_percent?: number;
|
||||
token?: string;
|
||||
token_address?: string;
|
||||
}
|
||||
|
||||
export interface RiskManagement {
|
||||
stop_loss_percent?: number;
|
||||
take_profit_percent?: number;
|
||||
}
|
||||
|
||||
export interface LLMConfig {
|
||||
model: string;
|
||||
temperature: number;
|
||||
}
|
||||
|
||||
export interface BotConversation {
|
||||
id: string;
|
||||
bot_id: string;
|
||||
role: 'user' | 'assistant' | 'system';
|
||||
content: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface Backtest {
|
||||
id: string;
|
||||
bot_id: string;
|
||||
started_at: string;
|
||||
ended_at: string | null;
|
||||
status: 'running' | 'completed' | 'failed' | 'stopped';
|
||||
config: BacktestConfig;
|
||||
result: BacktestResult | null;
|
||||
progress?: number;
|
||||
}
|
||||
|
||||
export interface BacktestConfig {
|
||||
token: string;
|
||||
token_name?: string;
|
||||
chain: string;
|
||||
timeframe: string;
|
||||
start_date: string;
|
||||
end_date: string;
|
||||
}
|
||||
|
||||
export interface BacktestResult {
|
||||
total_return: number;
|
||||
win_rate: number;
|
||||
total_trades: number;
|
||||
buy_signals: number;
|
||||
sell_signals: number;
|
||||
max_drawdown: number;
|
||||
sharpe_ratio: number;
|
||||
}
|
||||
|
||||
export interface PaginatedTrades {
|
||||
trades: Trade[];
|
||||
total_trades: number;
|
||||
page: number;
|
||||
per_page: number;
|
||||
total_pages: number;
|
||||
has_next: boolean;
|
||||
has_prev: boolean;
|
||||
}
|
||||
|
||||
export interface Trade {
|
||||
type: 'buy' | 'sell';
|
||||
token: string;
|
||||
price: number;
|
||||
amount: number;
|
||||
quantity: number;
|
||||
timestamp: number;
|
||||
exit_reason?: 'stop_loss' | 'take_profit' | string;
|
||||
}
|
||||
|
||||
export interface Simulation {
|
||||
id: string;
|
||||
bot_id: string;
|
||||
started_at: string;
|
||||
status: 'running' | 'stopped' | 'completed';
|
||||
config: SimulationConfig;
|
||||
signals: Signal[] | null;
|
||||
klines?: { time: number; close: number }[];
|
||||
trade_log?: TradeLogEntry[];
|
||||
portfolio?: Portfolio;
|
||||
current_candle_index?: number;
|
||||
total_candles?: number;
|
||||
candles_processed?: number;
|
||||
}
|
||||
|
||||
export interface SimulationConfig {
|
||||
token: string;
|
||||
chain?: string;
|
||||
kline_interval?: string;
|
||||
}
|
||||
|
||||
export interface TradeLogEntry {
|
||||
time: number;
|
||||
price: number;
|
||||
action: 'buy' | 'sell' | 'hold';
|
||||
reason: string;
|
||||
position: number;
|
||||
entry_price: number | null;
|
||||
}
|
||||
|
||||
export interface Portfolio {
|
||||
initial_balance: number;
|
||||
current_balance: number;
|
||||
position: number;
|
||||
position_token: string;
|
||||
entry_price: number;
|
||||
current_price: number;
|
||||
}
|
||||
|
||||
export interface Signal {
|
||||
id: string;
|
||||
bot_id: string;
|
||||
run_id: string;
|
||||
signal_type: 'buy' | 'sell' | 'hold';
|
||||
token: string;
|
||||
price: number;
|
||||
confidence: number | null;
|
||||
reasoning: string | null;
|
||||
executed: boolean;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface AuthResponse {
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
}
|
||||
|
||||
export interface BotChatRequest {
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface BotChatResponse {
|
||||
response: string;
|
||||
thinking: string | null;
|
||||
strategy_config: StrategyConfig | null;
|
||||
success: boolean;
|
||||
strategy_needs_confirmation?: boolean;
|
||||
strategy_data?: StrategyConfig | null;
|
||||
token_search_results?: TokenSearchResult[] | null;
|
||||
}
|
||||
|
||||
export interface TokenSearchResult {
|
||||
symbol: string;
|
||||
name: string;
|
||||
address: string;
|
||||
chain: string;
|
||||
}
|
||||
|
||||
export interface Conversation {
|
||||
id: string;
|
||||
user_id: string | null;
|
||||
bot_id: string | null;
|
||||
title: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export interface Message {
|
||||
id: string;
|
||||
conversation_id: string;
|
||||
role: 'user' | 'assistant';
|
||||
content: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface ConversationWithMessages extends Conversation {
|
||||
messages: Message[];
|
||||
}
|
||||
1
src/frontend/src/lib/assets/favicon.svg
Normal file
1
src/frontend/src/lib/assets/favicon.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="107" height="128" viewBox="0 0 107 128"><title>svelte-logo</title><path d="M94.157 22.819c-10.4-14.885-30.94-19.297-45.792-9.835L22.282 29.608A29.92 29.92 0 0 0 8.764 49.65a31.5 31.5 0 0 0 3.108 20.231 30 30 0 0 0-4.477 11.183 31.9 31.9 0 0 0 5.448 24.116c10.402 14.887 30.942 19.297 45.791 9.835l26.083-16.624A29.92 29.92 0 0 0 98.235 78.35a31.53 31.53 0 0 0-3.105-20.232 30 30 0 0 0 4.474-11.182 31.88 31.88 0 0 0-5.447-24.116" style="fill:#ff3e00"/><path d="M45.817 106.582a20.72 20.72 0 0 1-22.237-8.243 19.17 19.17 0 0 1-3.277-14.503 18 18 0 0 1 .624-2.435l.49-1.498 1.337.981a33.6 33.6 0 0 0 10.203 5.098l.97.294-.09.968a5.85 5.85 0 0 0 1.052 3.878 6.24 6.24 0 0 0 6.695 2.485 5.8 5.8 0 0 0 1.603-.704L69.27 76.28a5.43 5.43 0 0 0 2.45-3.631 5.8 5.8 0 0 0-.987-4.371 6.24 6.24 0 0 0-6.698-2.487 5.7 5.7 0 0 0-1.6.704l-9.953 6.345a19 19 0 0 1-5.296 2.326 20.72 20.72 0 0 1-22.237-8.243 19.17 19.17 0 0 1-3.277-14.502 17.99 17.99 0 0 1 8.13-12.052l26.081-16.623a19 19 0 0 1 5.3-2.329 20.72 20.72 0 0 1 22.237 8.243 19.17 19.17 0 0 1 3.277 14.503 18 18 0 0 1-.624 2.435l-.49 1.498-1.337-.98a33.6 33.6 0 0 0-10.203-5.1l-.97-.294.09-.968a5.86 5.86 0 0 0-1.052-3.878 6.24 6.24 0 0 0-6.696-2.485 5.8 5.8 0 0 0-1.602.704L37.73 51.72a5.42 5.42 0 0 0-2.449 3.63 5.79 5.79 0 0 0 .986 4.372 6.24 6.24 0 0 0 6.698 2.486 5.8 5.8 0 0 0 1.602-.704l9.952-6.342a19 19 0 0 1 5.295-2.328 20.72 20.72 0 0 1 22.237 8.242 19.17 19.17 0 0 1 3.277 14.503 18 18 0 0 1-8.13 12.053l-26.081 16.622a19 19 0 0 1-5.3 2.328" style="fill:#fff"/></svg>
|
||||
|
After Width: | Height: | Size: 1.5 KiB |
68
src/frontend/src/lib/components/AnonymousBanner.svelte
Normal file
68
src/frontend/src/lib/components/AnonymousBanner.svelte
Normal file
@@ -0,0 +1,68 @@
|
||||
<script lang="ts">
|
||||
interface Props {
|
||||
chatCount?: number;
|
||||
}
|
||||
|
||||
let { chatCount = 0 }: Props = $props();
|
||||
|
||||
const showWarning = chatCount >= 40;
|
||||
const limit = 50;
|
||||
</script>
|
||||
|
||||
<div class="anonymous-banner" class:warning={showWarning}>
|
||||
<div class="banner-content">
|
||||
{#if showWarning}
|
||||
<span class="icon">⚠️</span>
|
||||
<span>
|
||||
Warning: You've used {chatCount}/{limit} messages.
|
||||
<a href="/login" class="link">Login to continue</a>
|
||||
</span>
|
||||
{:else}
|
||||
<span class="icon">💬</span>
|
||||
<span>
|
||||
Your progress is not saved.
|
||||
<a href="/login" class="link">Login to save</a>
|
||||
</span>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.anonymous-banner {
|
||||
padding: 0.5rem 1rem;
|
||||
background: rgba(251, 191, 36, 0.1);
|
||||
border-bottom: 1px solid rgba(251, 191, 36, 0.2);
|
||||
}
|
||||
|
||||
.anonymous-banner.warning {
|
||||
background: rgba(220, 38, 38, 0.15);
|
||||
border-bottom-color: rgba(220, 38, 38, 0.2);
|
||||
}
|
||||
|
||||
.banner-content {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 0.5rem;
|
||||
font-size: 0.85rem;
|
||||
color: #fbbf24;
|
||||
}
|
||||
|
||||
.warning .banner-content {
|
||||
color: #fca5a5;
|
||||
}
|
||||
|
||||
.icon {
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.link {
|
||||
color: inherit;
|
||||
text-decoration: underline;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.link:hover {
|
||||
opacity: 0.8;
|
||||
}
|
||||
</style>
|
||||
115
src/frontend/src/lib/components/AppHeader.svelte
Normal file
115
src/frontend/src/lib/components/AppHeader.svelte
Normal file
@@ -0,0 +1,115 @@
|
||||
<script lang="ts">
|
||||
import { isAuthenticated, logout, userStore } from '$lib/stores';
|
||||
import { goto } from '$app/navigation';
|
||||
|
||||
function handleLogout() {
|
||||
logout();
|
||||
goto('/');
|
||||
}
|
||||
</script>
|
||||
|
||||
<header class="app-header">
|
||||
<div class="header-left">
|
||||
<a href="/home" class="logo">
|
||||
<span class="logo-text">Randebu</span>
|
||||
</a>
|
||||
{#if $isAuthenticated}
|
||||
<a href="/dashboard" class="nav-link">Dashboard</a>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="header-right">
|
||||
{#if $isAuthenticated}
|
||||
<span class="user-info">{$userStore?.username || 'User'}</span>
|
||||
<button class="btn btn-ghost" onclick={handleLogout}>
|
||||
Logout
|
||||
</button>
|
||||
{:else}
|
||||
<a href="/login" class="btn btn-ghost">Login</a>
|
||||
<a href="/register" class="btn btn-primary">Register</a>
|
||||
{/if}
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<style>
|
||||
.app-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 0 1.25rem;
|
||||
height: 56px;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.header-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1.5rem;
|
||||
}
|
||||
|
||||
.logo {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.logo-text {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 700;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
-webkit-background-clip: text;
|
||||
-webkit-text-fill-color: transparent;
|
||||
background-clip: text;
|
||||
}
|
||||
|
||||
.nav-link {
|
||||
color: #888;
|
||||
text-decoration: none;
|
||||
font-size: 0.9rem;
|
||||
transition: color 0.2s;
|
||||
}
|
||||
|
||||
.nav-link:hover {
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.header-right {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.user-info {
|
||||
color: #888;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 0.5rem 1rem;
|
||||
border-radius: 8px;
|
||||
font-size: 0.9rem;
|
||||
font-weight: 500;
|
||||
text-decoration: none;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.btn-ghost {
|
||||
background: transparent;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.btn-ghost:hover {
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-primary:hover {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
</style>
|
||||
313
src/frontend/src/lib/components/BacktestChart.svelte
Normal file
313
src/frontend/src/lib/components/BacktestChart.svelte
Normal file
@@ -0,0 +1,313 @@
|
||||
<script lang="ts">
|
||||
import type { BacktestResult } from '$lib/api';
|
||||
|
||||
interface ChartDataPoint {
|
||||
timestamp: string;
|
||||
value: number;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
results: BacktestResult | null;
|
||||
signals?: Array<{ created_at: string; signal_type: string; price: number }>;
|
||||
height?: number;
|
||||
}
|
||||
|
||||
let { results, signals = [], height = 300 }: Props = $props();
|
||||
|
||||
let width = $state(800);
|
||||
let containerEl: HTMLDivElement;
|
||||
|
||||
$effect(() => {
|
||||
if (containerEl) {
|
||||
width = containerEl.clientWidth;
|
||||
}
|
||||
});
|
||||
|
||||
function generatePortfolioCurve(): ChartDataPoint[] {
|
||||
if (!results || signals.length === 0) return [];
|
||||
|
||||
const points: ChartDataPoint[] = [];
|
||||
const startValue = 10000;
|
||||
let currentValue = startValue;
|
||||
|
||||
const sortedSignals = [...signals].sort(
|
||||
(a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime()
|
||||
);
|
||||
|
||||
points.push({
|
||||
timestamp: sortedSignals[0]?.created_at || new Date().toISOString(),
|
||||
value: currentValue
|
||||
});
|
||||
|
||||
for (const signal of sortedSignals) {
|
||||
if (signal.signal_type === 'buy') {
|
||||
currentValue *= 1.05;
|
||||
} else if (signal.signal_type === 'sell') {
|
||||
currentValue *= 0.95;
|
||||
}
|
||||
points.push({
|
||||
timestamp: signal.created_at,
|
||||
value: currentValue
|
||||
});
|
||||
}
|
||||
|
||||
return points;
|
||||
}
|
||||
|
||||
function getChartArea(w: number, h: number): { x: number; y: number; width: number; height: number } {
|
||||
const padding = { top: 20, right: 20, bottom: 40, left: 60 };
|
||||
return {
|
||||
x: padding.left,
|
||||
y: padding.top,
|
||||
width: w - padding.left - padding.right,
|
||||
height: h - padding.top - padding.bottom
|
||||
};
|
||||
}
|
||||
|
||||
function getValueRange(pts: ChartDataPoint[]): { min: number; max: number } {
|
||||
if (pts.length === 0) return { min: 0, max: 10000 };
|
||||
const values = pts.map(p => p.value);
|
||||
const min = Math.min(...values);
|
||||
const max = Math.max(...values);
|
||||
const padding = (max - min) * 0.1 || 1000;
|
||||
return { min: min - padding, max: max + padding };
|
||||
}
|
||||
|
||||
function getPointPosition(point: ChartDataPoint, index: number, total: number, area: { x: number; y: number; width: number; height: number }, range: { min: number; max: number }): { x: number; y: number } {
|
||||
const x = area.x + (index / Math.max(total - 1, 1)) * area.width;
|
||||
const normalizedValue = (point.value - range.min) / (range.max - range.min);
|
||||
const y = area.y + area.height - normalizedValue * area.height;
|
||||
return { x, y };
|
||||
}
|
||||
|
||||
function getYAxisLabels(area: { x: number; y: number; width: number; height: number }, range: { min: number; max: number }): Array<{ value: number; y: number }> {
|
||||
const step = (range.max - range.min) / 4;
|
||||
return [0, 1, 2, 3, 4].map(i => ({
|
||||
value: range.max - i * step,
|
||||
y: area.y + (i / 4) * area.height
|
||||
}));
|
||||
}
|
||||
|
||||
function getXAxisLabels(pts: ChartDataPoint[], area: { x: number; y: number; width: number; height: number }, range: { min: number; max: number }): Array<{ label: string; x: number }> {
|
||||
if (pts.length === 0) return [];
|
||||
const step = Math.max(1, Math.floor(pts.length / 5));
|
||||
return pts
|
||||
.filter((_, i) => i % step === 0 || i === pts.length - 1)
|
||||
.map((p, i, arr) => ({
|
||||
label: new Date(p.timestamp).toLocaleDateString(),
|
||||
x: getPointPosition(p, pts.indexOf(p), pts.length, area, range).x
|
||||
}));
|
||||
}
|
||||
|
||||
function getReturnColor(): string {
|
||||
if (!results) return '#888';
|
||||
return results.total_return >= 0 ? '#22c55e' : '#ef4444';
|
||||
}
|
||||
|
||||
let points = $derived(generatePortfolioCurve());
|
||||
let area = $derived(getChartArea(width, height));
|
||||
let range = $derived(getValueRange(points));
|
||||
let yAxisLabels = $derived(getYAxisLabels(area, range));
|
||||
let xAxisLabels = $derived(getXAxisLabels(points, area, range));
|
||||
</script>
|
||||
|
||||
<div class="backtest-chart" bind:this={containerEl}>
|
||||
{#if !results}
|
||||
<div class="empty-state">
|
||||
<p>No backtest results to display</p>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="chart-header">
|
||||
<div class="metric">
|
||||
<span class="metric-label">Total Return</span>
|
||||
<span class="metric-value" style="color: {getReturnColor()}">
|
||||
{results.total_return >= 0 ? '+' : ''}{results.total_return.toFixed(2)}%
|
||||
</span>
|
||||
</div>
|
||||
<div class="metric">
|
||||
<span class="metric-label">Win Rate</span>
|
||||
<span class="metric-value">{results.win_rate.toFixed(1)}%</span>
|
||||
</div>
|
||||
<div class="metric">
|
||||
<span class="metric-label">Total Trades</span>
|
||||
<span class="metric-value">{results.total_trades}</span>
|
||||
</div>
|
||||
<div class="metric">
|
||||
<span class="metric-label">Sharpe Ratio</span>
|
||||
<span class="metric-value">{results.sharpe_ratio.toFixed(2)}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<svg {width} {height} viewBox="0 0 {width} {height}">
|
||||
<defs>
|
||||
<linearGradient id="portfolioGradient" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0%" stop-color="rgba(102, 126, 234, 0.4)" />
|
||||
<stop offset="100%" stop-color="rgba(102, 126, 234, 0)" />
|
||||
</linearGradient>
|
||||
</defs>
|
||||
|
||||
<g class="grid-lines">
|
||||
{#each [0, 1, 2, 3, 4] as i}
|
||||
{@const y = area.y + (i / 4) * area.height}
|
||||
<line
|
||||
x1={area.x}
|
||||
y1={y}
|
||||
x2={area.x + area.width}
|
||||
y2={y}
|
||||
stroke="rgba(255,255,255,0.08)"
|
||||
stroke-dasharray="4,4"
|
||||
/>
|
||||
{/each}
|
||||
</g>
|
||||
|
||||
<g class="y-axis">
|
||||
{#each yAxisLabels as label}
|
||||
<text x={area.x - 8} y={label.y + 4} class="axis-label" text-anchor="end">
|
||||
${label.value.toLocaleString()}
|
||||
</text>
|
||||
{/each}
|
||||
</g>
|
||||
|
||||
<g class="x-axis">
|
||||
{#each xAxisLabels as label}
|
||||
<text x={label.x} y={height - 8} class="axis-label" text-anchor="middle">
|
||||
{label.label}
|
||||
</text>
|
||||
{/each}
|
||||
</g>
|
||||
|
||||
{#if points.length > 1}
|
||||
<path
|
||||
d={points.map((p, i) => {
|
||||
const pos = getPointPosition(p, i, points.length, area, range);
|
||||
if (i === 0) {
|
||||
return `M ${pos.x} ${area.y + area.height} L ${pos.x} ${pos.y}`;
|
||||
}
|
||||
return `L ${pos.x} ${pos.y}`;
|
||||
}).join(' ') + ` L ${getPointPosition(points[points.length - 1], points.length - 1, points.length, area, range).x} ${area.y + area.height} Z`}
|
||||
fill="url(#portfolioGradient)"
|
||||
/>
|
||||
|
||||
<path
|
||||
d={points.map((p, i) => {
|
||||
const pos = getPointPosition(p, i, points.length, area, range);
|
||||
return `${i === 0 ? 'M' : 'L'} ${pos.x} ${pos.y}`;
|
||||
}).join(' ')}
|
||||
fill="none"
|
||||
stroke="#667eea"
|
||||
stroke-width="2.5"
|
||||
/>
|
||||
{/if}
|
||||
</svg>
|
||||
|
||||
<div class="chart-footer">
|
||||
<div class="stat">
|
||||
<span class="stat-label">Buy Signals</span>
|
||||
<span class="stat-value buy">{results.buy_signals}</span>
|
||||
</div>
|
||||
<div class="stat">
|
||||
<span class="stat-label">Sell Signals</span>
|
||||
<span class="stat-value sell">{results.sell_signals}</span>
|
||||
</div>
|
||||
<div class="stat">
|
||||
<span class="stat-label">Max Drawdown</span>
|
||||
<span class="stat-value negative">-{results.max_drawdown.toFixed(2)}%</span>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.backtest-chart {
|
||||
width: 100%;
|
||||
background: rgba(255, 255, 255, 0.02);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 12px;
|
||||
padding: 1rem;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 300px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.chart-header {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(4, 1fr);
|
||||
gap: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
padding-bottom: 1rem;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
.metric {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.25rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.metric-label {
|
||||
font-size: 0.75rem;
|
||||
color: #888;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.metric-value {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
svg {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
.axis-label {
|
||||
font-size: 10px;
|
||||
fill: #666;
|
||||
}
|
||||
|
||||
.chart-footer {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
gap: 2rem;
|
||||
margin-top: 1rem;
|
||||
padding-top: 1rem;
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
.stat {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
|
||||
.stat-label {
|
||||
font-size: 0.75rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.stat-value {
|
||||
font-size: 1rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.buy {
|
||||
color: #22c55e;
|
||||
}
|
||||
|
||||
.sell {
|
||||
color: #ef4444;
|
||||
}
|
||||
|
||||
.negative {
|
||||
color: #ef4444;
|
||||
}
|
||||
</style>
|
||||
131
src/frontend/src/lib/components/BotCard.svelte
Normal file
131
src/frontend/src/lib/components/BotCard.svelte
Normal file
@@ -0,0 +1,131 @@
|
||||
<script lang="ts">
|
||||
import type { Bot } from '$lib/api';
|
||||
|
||||
interface Props {
|
||||
bot: Bot;
|
||||
onDelete?: (botId: string) => void;
|
||||
showActions?: boolean;
|
||||
}
|
||||
|
||||
let { bot, onDelete, showActions = true }: Props = $props();
|
||||
|
||||
function handleDelete(e: Event) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
onDelete?.(bot.id);
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="bot-card">
|
||||
<a href="/chat/{bot.id}" class="bot-card-link" data-sveltekit-preload-data="hover" aria-label="Open {bot.name}"></a>
|
||||
<div class="bot-info">
|
||||
<h3>{bot.name}</h3>
|
||||
{#if bot.description}
|
||||
<p class="bot-description">{bot.description}</p>
|
||||
{/if}
|
||||
<span class="bot-status status-{bot.status}">{bot.status}</span>
|
||||
</div>
|
||||
{#if showActions}
|
||||
<div class="bot-actions" role="group">
|
||||
<a href="/chat/{bot.id}" class="btn btn-primary" data-sveltekit-preload-data="hover">Open</a>
|
||||
<button class="btn btn-danger" onclick={handleDelete}>Delete</button>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.bot-card {
|
||||
position: relative;
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 12px;
|
||||
padding: 1.5rem;
|
||||
transition: transform 0.2s, border-color 0.2s;
|
||||
}
|
||||
|
||||
.bot-card:hover {
|
||||
transform: translateY(-2px);
|
||||
border-color: rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
.bot-card-link {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
border-radius: 12px;
|
||||
z-index: 0;
|
||||
}
|
||||
|
||||
.bot-info {
|
||||
margin-bottom: 1rem;
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
.bot-info h3 {
|
||||
margin: 0 0 0.5rem;
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
|
||||
.bot-description {
|
||||
color: #888;
|
||||
font-size: 0.9rem;
|
||||
margin: 0 0 0.75rem;
|
||||
}
|
||||
|
||||
.bot-status {
|
||||
display: inline-block;
|
||||
padding: 0.25rem 0.75rem;
|
||||
border-radius: 9999px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.status-draft {
|
||||
background: rgba(251, 191, 36, 0.2);
|
||||
color: #fbbf24;
|
||||
}
|
||||
|
||||
.status-active {
|
||||
background: rgba(34, 197, 94, 0.2);
|
||||
color: #22c55e;
|
||||
}
|
||||
|
||||
.status-paused {
|
||||
background: rgba(251, 191, 36, 0.2);
|
||||
color: #fbbf24;
|
||||
}
|
||||
|
||||
.bot-actions {
|
||||
display: flex;
|
||||
gap: 0.75rem;
|
||||
position: relative;
|
||||
z-index: 2;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 0.5rem 1rem;
|
||||
border-radius: 8px;
|
||||
font-weight: 500;
|
||||
font-size: 0.9rem;
|
||||
cursor: pointer;
|
||||
border: none;
|
||||
transition: transform 0.2s, opacity 0.2s;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
color: #fca5a5;
|
||||
border: 1px solid rgba(239, 68, 68, 0.4);
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
</style>
|
||||
434
src/frontend/src/lib/components/BotInfoPanel.svelte
Normal file
434
src/frontend/src/lib/components/BotInfoPanel.svelte
Normal file
@@ -0,0 +1,434 @@
|
||||
<script lang="ts">
|
||||
import type { Bot, Condition, Action, RiskManagement } from '$lib/api';
|
||||
|
||||
interface Props {
|
||||
bot?: Bot | null;
|
||||
onSelectBot?: () => void;
|
||||
onBacktest?: () => void;
|
||||
onSimulate?: () => void;
|
||||
}
|
||||
|
||||
let { bot = null, onSelectBot, onBacktest, onSimulate }: Props = $props();
|
||||
|
||||
// Helper to get human-readable condition name
|
||||
function getConditionLabel(condition: Condition): string {
|
||||
const labels: Record<string, string> = {
|
||||
'price_drop': 'Price Drop',
|
||||
'price_rise': 'Price Rise',
|
||||
'volume_spike': 'Volume Spike',
|
||||
'price_level': 'Price Level'
|
||||
};
|
||||
return labels[condition.type] || condition.type;
|
||||
}
|
||||
|
||||
// Format condition to readable string
|
||||
function formatCondition(condition: Condition): string {
|
||||
const parts: string[] = [getConditionLabel(condition)];
|
||||
|
||||
if (condition.token) {
|
||||
parts.push(condition.token.toUpperCase());
|
||||
}
|
||||
|
||||
if (condition.direction) {
|
||||
parts.push(condition.direction);
|
||||
}
|
||||
|
||||
if (condition.threshold) {
|
||||
parts.push(`>${condition.threshold}%`);
|
||||
}
|
||||
|
||||
if (condition.price) {
|
||||
parts.push(`$${condition.price}`);
|
||||
}
|
||||
|
||||
if (condition.timeframe) {
|
||||
parts.push(`(${condition.timeframe})`);
|
||||
}
|
||||
|
||||
return parts.join(' ');
|
||||
}
|
||||
|
||||
// Format action to readable string
|
||||
function formatAction(action: Action): string {
|
||||
const parts: string[] = [];
|
||||
|
||||
if (action.type === 'buy') {
|
||||
parts.push('Buy');
|
||||
if (action.amount_percent) {
|
||||
parts.push(`${action.amount_percent}%`);
|
||||
}
|
||||
if (action.token) {
|
||||
parts.push(`of ${action.token.toUpperCase()}`);
|
||||
}
|
||||
} else if (action.type === 'sell') {
|
||||
parts.push('Sell');
|
||||
if (action.amount_percent) {
|
||||
parts.push(`${action.amount_percent}%`);
|
||||
}
|
||||
} else {
|
||||
parts.push('Hold');
|
||||
}
|
||||
|
||||
return parts.join(' ');
|
||||
}
|
||||
|
||||
let hasStrategy = $derived(
|
||||
bot?.strategy_config &&
|
||||
((bot.strategy_config.conditions && bot.strategy_config.conditions.length > 0) ||
|
||||
(bot.strategy_config.actions && bot.strategy_config.actions.length > 0) ||
|
||||
bot.strategy_config.risk_management)
|
||||
);
|
||||
</script>
|
||||
|
||||
<div class="bot-info-panel">
|
||||
<h3>Bot Details</h3>
|
||||
|
||||
{#if !bot}
|
||||
<div class="no-bot">
|
||||
<p>No bot selected</p>
|
||||
{#if onSelectBot}
|
||||
<button class="btn btn-secondary" onclick={onSelectBot}>
|
||||
Select Bot
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
{:else}
|
||||
<div class="bot-details">
|
||||
<div class="bot-name">{bot.name}</div>
|
||||
|
||||
<div class="detail-row">
|
||||
<span class="label">Status:</span>
|
||||
<span class="value" class:active={bot.status === 'active'}>
|
||||
{bot.status}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="strategy-section">
|
||||
<h4>Strategy</h4>
|
||||
|
||||
{#if hasStrategy}
|
||||
{#if bot.strategy_config?.conditions && bot.strategy_config.conditions.length > 0}
|
||||
<div class="strategy-group">
|
||||
<div class="strategy-group-label">When:</div>
|
||||
{#each bot.strategy_config.conditions as condition}
|
||||
<div class="condition-item">
|
||||
<span class="condition-icon">📊</span>
|
||||
{formatCondition(condition)}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if bot.strategy_config?.actions && bot.strategy_config.actions.length > 0}
|
||||
<div class="strategy-group">
|
||||
<div class="strategy-group-label">Then:</div>
|
||||
{#each bot.strategy_config.actions as action}
|
||||
<div class="action-item">
|
||||
<span class="action-icon">{action.type === 'buy' ? '🟢' : action.type === 'sell' ? '🔴' : '⏸️'}</span>
|
||||
{formatAction(action)}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if bot.strategy_config?.risk_management}
|
||||
<div class="strategy-group">
|
||||
<div class="strategy-group-label">Risk:</div>
|
||||
<div class="risk-item">
|
||||
{#if bot.strategy_config.risk_management.stop_loss_percent}
|
||||
<span class="risk-tag loss">Stop Loss: {bot.strategy_config.risk_management.stop_loss_percent}%</span>
|
||||
{/if}
|
||||
{#if bot.strategy_config.risk_management.take_profit_percent}
|
||||
<span class="risk-tag profit">Take Profit: {bot.strategy_config.risk_management.take_profit_percent}%</span>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
{:else}
|
||||
<div class="no-strategy">
|
||||
<div class="no-strategy-icon">⚙️</div>
|
||||
<p>No strategy configured</p>
|
||||
<span class="no-strategy-hint">Chat with the bot to set up your trading strategy</span>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if onSelectBot}
|
||||
<button class="btn btn-outline" onclick={onSelectBot}>
|
||||
Change Bot
|
||||
</button>
|
||||
{/if}
|
||||
|
||||
<div class="action-buttons">
|
||||
<h4>Tools</h4>
|
||||
<div class="button-row">
|
||||
<button
|
||||
class="btn btn-tool btn-backtest"
|
||||
onclick={onBacktest}
|
||||
disabled={!bot || !hasStrategy}
|
||||
>
|
||||
📊 Backtest
|
||||
</button>
|
||||
<button
|
||||
class="btn btn-tool btn-simulate"
|
||||
onclick={onSimulate}
|
||||
disabled={!bot || !hasStrategy}
|
||||
>
|
||||
🎮 Simulate
|
||||
</button>
|
||||
</div>
|
||||
{#if !hasStrategy}
|
||||
<p class="tool-hint">Configure a strategy first to use these tools</p>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.bot-info-panel {
|
||||
padding: 1.25rem;
|
||||
height: 100%;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
h3 {
|
||||
font-size: 0.85rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
color: #888;
|
||||
margin: 0 0 1rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
h4 {
|
||||
font-size: 0.8rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
color: #666;
|
||||
margin: 1rem 0 0.75rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.no-bot {
|
||||
text-align: center;
|
||||
padding: 1rem 0;
|
||||
}
|
||||
|
||||
.no-bot p {
|
||||
color: #666;
|
||||
margin: 0 0 1rem;
|
||||
}
|
||||
|
||||
.bot-details {
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.bot-name {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 600;
|
||||
color: #fff;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.detail-row {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
padding: 0.5rem 0;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
.label {
|
||||
color: #888;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.value {
|
||||
color: #ccc;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.value.active {
|
||||
color: #3fb950;
|
||||
}
|
||||
|
||||
.strategy-section {
|
||||
background: rgba(255, 255, 255, 0.03);
|
||||
border-radius: 8px;
|
||||
padding: 1rem;
|
||||
margin-top: 0.5rem;
|
||||
}
|
||||
|
||||
.strategy-group {
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.strategy-group:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.strategy-group-label {
|
||||
font-size: 0.75rem;
|
||||
color: #667eea;
|
||||
font-weight: 600;
|
||||
text-transform: uppercase;
|
||||
margin-bottom: 0.25rem;
|
||||
}
|
||||
|
||||
.condition-item,
|
||||
.action-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
padding: 0.35rem 0;
|
||||
font-size: 0.85rem;
|
||||
color: #ccc;
|
||||
}
|
||||
|
||||
.condition-icon,
|
||||
.action-icon {
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.risk-item {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.risk-tag {
|
||||
font-size: 0.75rem;
|
||||
padding: 0.25rem 0.5rem;
|
||||
border-radius: 4px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.risk-tag.loss {
|
||||
background: rgba(248, 81, 73, 0.2);
|
||||
color: #f85149;
|
||||
}
|
||||
|
||||
.risk-tag.profit {
|
||||
background: rgba(63, 185, 80, 0.2);
|
||||
color: #3fb950;
|
||||
}
|
||||
|
||||
.no-strategy {
|
||||
text-align: center;
|
||||
padding: 1rem 0.5rem;
|
||||
}
|
||||
|
||||
.no-strategy-icon {
|
||||
font-size: 2rem;
|
||||
margin-bottom: 0.5rem;
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.no-strategy p {
|
||||
color: #888;
|
||||
margin: 0 0 0.25rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.no-strategy-hint {
|
||||
font-size: 0.75rem;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.action-buttons {
|
||||
margin-top: 1.5rem;
|
||||
padding-top: 1rem;
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.action-buttons h4 {
|
||||
font-size: 0.75rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
color: #666;
|
||||
margin: 0 0 0.75rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.button-row {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.btn-tool {
|
||||
flex: 1;
|
||||
padding: 0.6rem 0.5rem;
|
||||
border-radius: 6px;
|
||||
font-size: 0.8rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 0.35rem;
|
||||
}
|
||||
|
||||
.btn-tool:disabled {
|
||||
opacity: 0.4;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.btn-backtest {
|
||||
background: rgba(102, 126, 234, 0.15);
|
||||
color: #667eea;
|
||||
border: 1px solid rgba(102, 126, 234, 0.3);
|
||||
}
|
||||
|
||||
.btn-backtest:hover:not(:disabled) {
|
||||
background: rgba(102, 126, 234, 0.25);
|
||||
}
|
||||
|
||||
.btn-simulate {
|
||||
background: rgba(63, 185, 80, 0.15);
|
||||
color: #3fb950;
|
||||
border: 1px solid rgba(63, 185, 80, 0.3);
|
||||
}
|
||||
|
||||
.btn-simulate:hover:not(:disabled) {
|
||||
background: rgba(63, 185, 80, 0.25);
|
||||
}
|
||||
|
||||
.tool-hint {
|
||||
font-size: 0.7rem;
|
||||
color: #555;
|
||||
margin: 0.5rem 0 0;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.btn {
|
||||
width: 100%;
|
||||
padding: 0.75rem 1rem;
|
||||
border-radius: 8px;
|
||||
font-size: 0.9rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
color: #fff;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
.btn-secondary:hover {
|
||||
background: rgba(255, 255, 255, 0.15);
|
||||
}
|
||||
|
||||
.btn-outline {
|
||||
background: transparent;
|
||||
color: #667eea;
|
||||
border: 1px solid #667eea;
|
||||
}
|
||||
|
||||
.btn-outline:hover {
|
||||
background: rgba(102, 126, 234, 0.1);
|
||||
}
|
||||
</style>
|
||||
94
src/frontend/src/lib/components/BotSelector.svelte
Normal file
94
src/frontend/src/lib/components/BotSelector.svelte
Normal file
@@ -0,0 +1,94 @@
|
||||
<script lang="ts">
|
||||
import type { Bot } from '$lib/api';
|
||||
|
||||
interface Props {
|
||||
bots: Bot[];
|
||||
selectedBotId?: string | null;
|
||||
onSelect: (botId: string) => void;
|
||||
disabled?: boolean;
|
||||
label?: string;
|
||||
}
|
||||
|
||||
let { bots, selectedBotId = null, onSelect, disabled = false, label = 'Select Bot' }: Props = $props();
|
||||
|
||||
function handleChange(e: Event) {
|
||||
const target = e.target as HTMLSelectElement;
|
||||
onSelect(target.value);
|
||||
}
|
||||
|
||||
const MAX_BOTS = 3;
|
||||
</script>
|
||||
|
||||
<div class="bot-selector">
|
||||
{#if label}
|
||||
<label for="bot-select">{label}</label>
|
||||
{/if}
|
||||
<div class="select-wrapper">
|
||||
<select
|
||||
id="bot-select"
|
||||
onchange={handleChange}
|
||||
disabled={disabled || bots.length === 0}
|
||||
value={selectedBotId || ''}
|
||||
>
|
||||
{#if bots.length === 0}
|
||||
<option value="" disabled>No bots available</option>
|
||||
{:else}
|
||||
{#each bots as bot}
|
||||
<option value={bot.id}>{bot.name}</option>
|
||||
{/each}
|
||||
{/if}
|
||||
</select>
|
||||
<span class="bot-count">{bots.length}/{MAX_BOTS}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.bot-selector {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
label {
|
||||
font-size: 0.9rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.select-wrapper {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
select {
|
||||
flex: 1;
|
||||
padding: 0.75rem 1rem;
|
||||
border-radius: 8px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: #fff;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
appearance: none;
|
||||
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23888' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
|
||||
background-repeat: no-repeat;
|
||||
background-position: right 1rem center;
|
||||
padding-right: 2.5rem;
|
||||
}
|
||||
|
||||
select:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
select:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.bot-count {
|
||||
font-size: 0.8rem;
|
||||
color: #666;
|
||||
white-space: nowrap;
|
||||
}
|
||||
</style>
|
||||
47
src/frontend/src/lib/components/CandlestickLoader.svelte
Normal file
47
src/frontend/src/lib/components/CandlestickLoader.svelte
Normal file
@@ -0,0 +1,47 @@
|
||||
<script lang="ts">
|
||||
let { class: className = '' } = $props();
|
||||
|
||||
const bars = [0, 1, 2, 3];
|
||||
const heights = ['12px', '18px', '14px', '20px'];
|
||||
const delays = ['0s', '0.15s', '0.3s', '0.45s'];
|
||||
</script>
|
||||
|
||||
<div class="flex items-center gap-1 h-8 {className}">
|
||||
{#each bars as i}
|
||||
<div
|
||||
class="w-2 bg-green-500 rounded-sm animate-pulse"
|
||||
style="height: {heights[i]}; animation-delay: {delays[i]};"
|
||||
></div>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.flex {
|
||||
display: flex;
|
||||
}
|
||||
.items-center {
|
||||
align-items: center;
|
||||
}
|
||||
.gap-1 {
|
||||
gap: 0.25rem;
|
||||
}
|
||||
.h-8 {
|
||||
height: 2rem;
|
||||
}
|
||||
.w-2 {
|
||||
width: 0.5rem;
|
||||
}
|
||||
.bg-green-500 {
|
||||
background-color: rgb(34 197 94);
|
||||
}
|
||||
.rounded-sm {
|
||||
border-radius: 0.125rem;
|
||||
}
|
||||
.animate-pulse {
|
||||
animation: pulse 1.5s ease-in-out infinite;
|
||||
}
|
||||
@keyframes pulse {
|
||||
0%, 100% { opacity: 0.4; transform: scaleY(1); }
|
||||
50% { opacity: 1; transform: scaleY(1.2); }
|
||||
}
|
||||
</style>
|
||||
188
src/frontend/src/lib/components/ChatArea.svelte
Normal file
188
src/frontend/src/lib/components/ChatArea.svelte
Normal file
@@ -0,0 +1,188 @@
|
||||
<script lang="ts">
|
||||
import type { Message } from '$lib/api';
|
||||
import { parseMarkdown, type ParsedSegment } from '$lib/utils/markdown';
|
||||
import CandlestickLoader from './CandlestickLoader.svelte';
|
||||
|
||||
interface Props {
|
||||
conversationId?: string;
|
||||
messages?: Message[];
|
||||
isLoading?: boolean;
|
||||
}
|
||||
|
||||
let { conversationId, messages = [], isLoading = false }: Props = $props();
|
||||
|
||||
let chatContainer: HTMLDivElement;
|
||||
|
||||
function renderContent(content: string): ParsedSegment[] {
|
||||
return parseMarkdown(content);
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (messages.length && chatContainer) {
|
||||
setTimeout(() => {
|
||||
chatContainer.scrollTop = chatContainer.scrollHeight;
|
||||
}, 50);
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<div class="chat-area" bind:this={chatContainer}>
|
||||
{#if !conversationId}
|
||||
<div class="empty-state">
|
||||
Select a conversation or start a new one
|
||||
</div>
|
||||
{:else if messages.length === 0 && !isLoading}
|
||||
<div class="empty-state">
|
||||
Send a message to start the conversation
|
||||
</div>
|
||||
{:else}
|
||||
<div class="messages">
|
||||
{#each messages as msg (msg.id)}
|
||||
<div class="message" class:user={msg.role === 'user'} class:assistant={msg.role === 'assistant'}>
|
||||
<div class="message-content">
|
||||
{#each renderContent(msg.content) as segment}
|
||||
{#if segment.type === 'bold'}
|
||||
<strong>{segment.content}</strong>
|
||||
{:else if segment.type === 'italic'}
|
||||
<em>{segment.content}</em>
|
||||
{:else if segment.type === 'code'}
|
||||
<code class="inline-code">{segment.content}</code>
|
||||
{:else if segment.type === 'codeBlock'}
|
||||
<pre class="code-block"><code>{segment.content}</code></pre>
|
||||
{:else if segment.type === 'link'}
|
||||
<a href={segment.content} target="_blank" rel="noopener noreferrer">{segment.content}</a>
|
||||
{:else if segment.type === 'list' && segment.items}
|
||||
<ul>
|
||||
{#each segment.items as item}
|
||||
<li>{item}</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{:else if segment.type === 'lineBreak'}
|
||||
<br />
|
||||
{:else}
|
||||
{segment.content}
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
<div class="message-time">
|
||||
{new Date(msg.created_at).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if isLoading}
|
||||
<div class="message assistant">
|
||||
<div class="message-content">
|
||||
<CandlestickLoader />
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.chat-area {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 1.5rem;
|
||||
padding-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
height: 100%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: #666;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.messages {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.message {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
max-width: 75%;
|
||||
}
|
||||
|
||||
.message.user {
|
||||
align-self: flex-end;
|
||||
align-items: flex-end;
|
||||
}
|
||||
|
||||
.message.assistant {
|
||||
align-self: flex-start;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.message-content {
|
||||
padding: 0.875rem 1rem;
|
||||
border-radius: 12px;
|
||||
line-height: 1.6;
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.message.user .message-content {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border-bottom-right-radius: 4px;
|
||||
}
|
||||
|
||||
.message.assistant .message-content {
|
||||
background: rgba(255, 255, 255, 0.08);
|
||||
color: #e0e0e0;
|
||||
border-bottom-left-radius: 4px;
|
||||
}
|
||||
|
||||
.message-time {
|
||||
font-size: 0.7rem;
|
||||
color: #555;
|
||||
margin-top: 0.25rem;
|
||||
padding: 0 0.5rem;
|
||||
}
|
||||
|
||||
.inline-code {
|
||||
background: rgba(0, 0, 0, 0.3);
|
||||
padding: 0.15rem 0.4rem;
|
||||
border-radius: 4px;
|
||||
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
|
||||
font-size: 0.85em;
|
||||
}
|
||||
|
||||
.code-block {
|
||||
background: rgba(0, 0, 0, 0.4);
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
overflow-x: auto;
|
||||
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
|
||||
font-size: 0.85rem;
|
||||
margin: 0.5rem 0;
|
||||
}
|
||||
|
||||
pre.code-block code {
|
||||
white-space: pre;
|
||||
}
|
||||
|
||||
ul {
|
||||
margin: 0.5rem 0;
|
||||
padding-left: 1.25rem;
|
||||
}
|
||||
|
||||
li {
|
||||
margin: 0.25rem 0;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #667eea;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
</style>
|
||||
113
src/frontend/src/lib/components/ChatInput.svelte
Normal file
113
src/frontend/src/lib/components/ChatInput.svelte
Normal file
@@ -0,0 +1,113 @@
|
||||
<script lang="ts">
|
||||
interface Props {
|
||||
onSend: (message: string) => void;
|
||||
disabled?: boolean;
|
||||
placeholder?: string;
|
||||
}
|
||||
|
||||
let { onSend, disabled = false, placeholder = "Type a message..." }: Props = $props();
|
||||
|
||||
let messageInput = $state('');
|
||||
let textarea: HTMLTextAreaElement;
|
||||
|
||||
function handleSend() {
|
||||
if (!messageInput.trim() || disabled) return;
|
||||
onSend(messageInput);
|
||||
messageInput = '';
|
||||
if (textarea) {
|
||||
textarea.style.height = 'auto';
|
||||
}
|
||||
}
|
||||
|
||||
function handleKeydown(e: KeyboardEvent) {
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
handleSend();
|
||||
}
|
||||
}
|
||||
|
||||
function handleInput() {
|
||||
if (textarea) {
|
||||
textarea.style.height = 'auto';
|
||||
textarea.style.height = Math.min(textarea.scrollHeight, 150) + 'px';
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="chat-input">
|
||||
<textarea
|
||||
bind:this={textarea}
|
||||
bind:value={messageInput}
|
||||
onkeydown={handleKeydown}
|
||||
oninput={handleInput}
|
||||
{placeholder}
|
||||
{disabled}
|
||||
rows="1"
|
||||
></textarea>
|
||||
<button onclick={handleSend} {disabled}>
|
||||
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
||||
<path d="M22 2L11 13M22 2l-7 20-4-9-9-4 20-7z"/>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.chat-input {
|
||||
display: flex;
|
||||
gap: 0.75rem;
|
||||
padding: 1rem 1.25rem;
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.1);
|
||||
background: rgba(0, 0, 0, 0.3);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
textarea {
|
||||
flex: 1;
|
||||
padding: 0.875rem 1rem;
|
||||
border-radius: 12px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: #fff;
|
||||
font-size: 1rem;
|
||||
font-family: inherit;
|
||||
resize: none;
|
||||
min-height: 48px;
|
||||
max-height: 150px;
|
||||
}
|
||||
|
||||
textarea:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
textarea::placeholder {
|
||||
color: #666;
|
||||
}
|
||||
|
||||
textarea:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
button {
|
||||
padding: 0.75rem 1.25rem;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 12px;
|
||||
cursor: pointer;
|
||||
transition: transform 0.2s, opacity 0.2s;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
button:hover:not(:disabled) {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
</style>
|
||||
857
src/frontend/src/lib/components/ChatInterface.svelte
Normal file
857
src/frontend/src/lib/components/ChatInterface.svelte
Normal file
@@ -0,0 +1,857 @@
|
||||
<script lang="ts">
|
||||
import type { Bot } from '$lib/api';
|
||||
import type { ChatMessage } from '$lib/stores/chatStore';
|
||||
import { parseMarkdown, parseInlineElements, type InlineSegment } from '$lib/utils/markdown';
|
||||
|
||||
interface ToolGroup {
|
||||
category: string;
|
||||
label: string;
|
||||
requiresBot: boolean;
|
||||
tools: ToolItem[];
|
||||
}
|
||||
|
||||
interface ToolItem {
|
||||
name: string;
|
||||
description: string;
|
||||
command: string;
|
||||
}
|
||||
|
||||
const TOOLS: ToolGroup[] = [
|
||||
{
|
||||
category: 'randebu',
|
||||
label: '🤖 Randebu Built-in',
|
||||
requiresBot: true,
|
||||
tools: [
|
||||
{ name: 'backtest', description: 'Run strategy backtest', command: '/backtest' },
|
||||
{ name: 'simulate', description: 'Start/stop simulation', command: '/simulate' },
|
||||
{ name: 'strategy', description: 'View/update strategy', command: '/strategy' },
|
||||
]
|
||||
},
|
||||
{
|
||||
category: 'ave',
|
||||
label: '☁️ AVE Cloud Skills',
|
||||
requiresBot: false,
|
||||
tools: [
|
||||
{ name: 'search', description: 'Token search', command: '/search' },
|
||||
{ name: 'trending', description: 'Popular tokens', command: '/trending' },
|
||||
{ name: 'risk', description: 'Honeypot detection', command: '/risk' },
|
||||
{ name: 'token', description: 'Token details', command: '/token' },
|
||||
{ name: 'price', description: 'Batch prices', command: '/price' },
|
||||
]
|
||||
}
|
||||
];
|
||||
|
||||
interface Props {
|
||||
bot: Bot | null;
|
||||
messages: ChatMessage[];
|
||||
isSending?: boolean;
|
||||
isBlocked?: boolean;
|
||||
blockedReason?: string | null;
|
||||
onSendMessage: (message: string) => void;
|
||||
onSelectBot?: (botId: string) => void;
|
||||
onLogin?: () => void;
|
||||
availableBots?: Bot[];
|
||||
showBotSelector?: boolean;
|
||||
}
|
||||
|
||||
let {
|
||||
bot,
|
||||
messages,
|
||||
isSending = false,
|
||||
isBlocked = false,
|
||||
blockedReason = null,
|
||||
onSendMessage,
|
||||
onSelectBot,
|
||||
onLogin,
|
||||
availableBots = [],
|
||||
showBotSelector = false
|
||||
}: Props = $props();
|
||||
|
||||
let messageInput = $state('');
|
||||
let chatContainer: HTMLDivElement;
|
||||
let expandedThinking: Record<string, boolean> = $state({});
|
||||
let showSlashMenu = $state(false);
|
||||
let slashMenuPosition = $state({ top: 0, left: 0 });
|
||||
let selectedIndex = $state(0);
|
||||
|
||||
// Use $derived for filteredTools
|
||||
// Filter tools based on whether user has a bot
|
||||
let availableTools = $derived(
|
||||
TOOLS.flatMap(t => !t.requiresBot || bot ? t.tools : [])
|
||||
);
|
||||
|
||||
let filteredTools = $derived(
|
||||
messageInput.startsWith('/')
|
||||
? availableTools.filter(tool =>
|
||||
tool.name.toLowerCase().startsWith(messageInput.slice(1).toLowerCase()) ||
|
||||
tool.command.toLowerCase().startsWith(messageInput.slice(1).toLowerCase())
|
||||
)
|
||||
: []
|
||||
);
|
||||
|
||||
// Get visible tool groups for the menu
|
||||
let visibleGroups = $derived(
|
||||
TOOLS.filter(group => !group.requiresBot || bot)
|
||||
);
|
||||
|
||||
function handleSend() {
|
||||
if (!messageInput.trim()) return;
|
||||
showSlashMenu = false;
|
||||
onSendMessage(messageInput);
|
||||
messageInput = '';
|
||||
}
|
||||
|
||||
function handleKeydown(e: KeyboardEvent) {
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
if (showSlashMenu && filteredTools.length > 0) {
|
||||
selectTool(filteredTools[selectedIndex]);
|
||||
} else {
|
||||
handleSend();
|
||||
}
|
||||
} else if (e.key === 'ArrowDown' && showSlashMenu) {
|
||||
e.preventDefault();
|
||||
selectedIndex = Math.min(selectedIndex + 1, filteredTools.length - 1);
|
||||
} else if (e.key === 'ArrowUp' && showSlashMenu) {
|
||||
e.preventDefault();
|
||||
selectedIndex = Math.max(selectedIndex - 1, 0);
|
||||
} else if (e.key === 'Escape' && showSlashMenu) {
|
||||
showSlashMenu = false;
|
||||
} else if (e.key === 'Tab' && showSlashMenu && filteredTools.length > 0) {
|
||||
e.preventDefault();
|
||||
selectTool(filteredTools[selectedIndex]);
|
||||
}
|
||||
}
|
||||
|
||||
function handleInput(e: Event) {
|
||||
const target = e.target as HTMLTextAreaElement;
|
||||
const value = target.value;
|
||||
messageInput = value;
|
||||
|
||||
if (value.startsWith('/')) {
|
||||
selectedIndex = 0;
|
||||
showSlashMenu = filteredTools.length > 0;
|
||||
|
||||
if (showSlashMenu) {
|
||||
// Position menu above the textarea
|
||||
const rect = target.getBoundingClientRect();
|
||||
const menuHeight = 300;
|
||||
slashMenuPosition = {
|
||||
top: Math.max(10, rect.top - menuHeight),
|
||||
left: rect.left
|
||||
};
|
||||
}
|
||||
} else {
|
||||
showSlashMenu = false;
|
||||
}
|
||||
}
|
||||
|
||||
function selectTool(tool: ToolItem) {
|
||||
messageInput = tool.command + ' ';
|
||||
showSlashMenu = false;
|
||||
const textarea = document.querySelector('.input-container textarea') as HTMLTextAreaElement;
|
||||
if (textarea) {
|
||||
textarea.focus();
|
||||
}
|
||||
}
|
||||
|
||||
function handleBotChange(e: Event) {
|
||||
const target = e.target as HTMLSelectElement;
|
||||
if (onSelectBot && target.value) {
|
||||
onSelectBot(target.value);
|
||||
}
|
||||
}
|
||||
|
||||
function toggleThinkingExpand(messageId: string) {
|
||||
expandedThinking[messageId] = !expandedThinking[messageId];
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (messages.length && chatContainer) {
|
||||
setTimeout(() => {
|
||||
chatContainer.scrollTop = chatContainer.scrollHeight;
|
||||
}, 50);
|
||||
}
|
||||
});
|
||||
|
||||
function renderContent(content: string) {
|
||||
return parseMarkdown(content);
|
||||
}
|
||||
|
||||
function renderInline(segments: InlineSegment[]): string {
|
||||
return segments.map(seg => {
|
||||
switch (seg.type) {
|
||||
case 'bold': return `<strong>${seg.content}</strong>`;
|
||||
case 'italic': return `<em>${seg.content}</em>`;
|
||||
case 'code': return `<code class="inline-code">${seg.content}</code>`;
|
||||
case 'link': return `<a href="${seg.href || '#'}" target="_blank" rel="noopener noreferrer">${seg.content}</a>`;
|
||||
default: return seg.content;
|
||||
}
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function handleClickOutside(e: MouseEvent) {
|
||||
const target = e.target as HTMLElement;
|
||||
if (!target.closest('.slash-menu') && !target.closest('.input-container textarea')) {
|
||||
showSlashMenu = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:window on:click={handleClickOutside} />
|
||||
|
||||
<div class="chat-interface">
|
||||
{#if showBotSelector && availableBots.length > 0}
|
||||
<div class="bot-selector">
|
||||
<label for="bot-select">Active Bot:</label>
|
||||
<select id="bot-select" onchange={handleBotChange}>
|
||||
{#each availableBots as availableBot}
|
||||
<option value={availableBot.id} selected={availableBot.id === bot?.id}>
|
||||
{availableBot.name}
|
||||
</option>
|
||||
{/each}
|
||||
</select>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="chat-messages" bind:this={chatContainer}>
|
||||
{#if messages.length === 0}
|
||||
<div class="welcome-message">
|
||||
<p>Welcome to {bot?.name || 'your bot'}! Describe your trading strategy in plain English.</p>
|
||||
<p class="hint">Example: "Buy PEPE when the price drops by 5% within 1 hour"</p>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#each messages as message}
|
||||
<div class="message {message.role}">
|
||||
{#if message.role === 'assistant' && message.thinking}
|
||||
{@const firstLine = message.thinking.split('\n')[0]}
|
||||
{@const isExpanded = expandedThinking[message.id] ?? false}
|
||||
<div class="thinking-section">
|
||||
<button class="thinking-toggle" onclick={() => toggleThinkingExpand(message.id)}>
|
||||
<span class="thinking-icon">{isExpanded ? '▼' : '▶'}</span>
|
||||
<span class="thinking-label">{isExpanded ? 'Hide reasoning' : 'Show reasoning'}</span>
|
||||
{#if !isExpanded}
|
||||
<span class="thinking-preview"> — {firstLine.slice(0, 60)}{firstLine.length > 60 ? '...' : ''}</span>
|
||||
{/if}
|
||||
</button>
|
||||
{#if isExpanded}
|
||||
<div class="thinking-content">
|
||||
{message.thinking}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
<div class="message-content">
|
||||
{#each renderContent(message.content) as segment}
|
||||
{#if segment.type === 'bold'}
|
||||
<strong>{segment.content}</strong>
|
||||
{:else if segment.type === 'italic'}
|
||||
<em>{segment.content}</em>
|
||||
{:else if segment.type === 'code'}
|
||||
<code class="inline-code">{segment.content}</code>
|
||||
{:else if segment.type === 'codeBlock'}
|
||||
<pre class="code-block"><code>{segment.content}</code></pre>
|
||||
{:else if segment.type === 'link'}
|
||||
<a href={segment.content} target="_blank" rel="noopener noreferrer">{segment.content}</a>
|
||||
{:else if segment.type === 'list' && segment.items}
|
||||
<ul>
|
||||
{#each segment.items as item}
|
||||
<li>{@html renderInline(parseInlineElements(item))}</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{:else if segment.type === 'table' && segment.headers && segment.rows}
|
||||
<div class="table-wrapper">
|
||||
<table class="markdown-table">
|
||||
<thead>
|
||||
<tr>
|
||||
{#each segment.headers as header}
|
||||
<th>
|
||||
{#each header as cellSeg}
|
||||
{#if cellSeg.type === 'bold'}
|
||||
<strong>{cellSeg.content}</strong>
|
||||
{:else if cellSeg.type === 'italic'}
|
||||
<em>{cellSeg.content}</em>
|
||||
{:else if cellSeg.type === 'code'}
|
||||
<code class="inline-code">{cellSeg.content}</code>
|
||||
{:else if cellSeg.type === 'link'}
|
||||
<a href={cellSeg.href} target="_blank" rel="noopener noreferrer">{cellSeg.content}</a>
|
||||
{:else}
|
||||
{cellSeg.content}
|
||||
{/if}
|
||||
{/each}
|
||||
</th>
|
||||
{/each}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{#each segment.rows as row}
|
||||
<tr>
|
||||
{#each row as cell}
|
||||
<td>
|
||||
{#each cell as cellSeg}
|
||||
{#if cellSeg.type === 'bold'}
|
||||
<strong>{cellSeg.content}</strong>
|
||||
{:else if cellSeg.type === 'italic'}
|
||||
<em>{cellSeg.content}</em>
|
||||
{:else if cellSeg.type === 'code'}
|
||||
<code class="inline-code">{cellSeg.content}</code>
|
||||
{:else if cellSeg.type === 'link'}
|
||||
<a href={cellSeg.href} target="_blank" rel="noopener noreferrer">{cellSeg.content}</a>
|
||||
{:else}
|
||||
{cellSeg.content}
|
||||
{/if}
|
||||
{/each}
|
||||
</td>
|
||||
{/each}
|
||||
</tr>
|
||||
{/each}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{:else if segment.type === 'heading'}
|
||||
<h4 class="content-heading">{segment.content}</h4>
|
||||
{:else if segment.type === 'lineBreak'}
|
||||
<br />
|
||||
{:else}
|
||||
{segment.content}
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
<div class="message-time">
|
||||
{message.timestamp.toLocaleTimeString()}
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
|
||||
{#if isSending}
|
||||
<div class="message assistant">
|
||||
<div class="message-content">
|
||||
<div class="typing">
|
||||
<span class="dot"></span>
|
||||
<span class="dot"></span>
|
||||
<span class="dot"></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="input-container">
|
||||
{#if isBlocked}
|
||||
<div class="blocked-message">
|
||||
<div class="blocked-icon">🚫</div>
|
||||
<div class="blocked-text">
|
||||
{#if blockedReason === 'message_limit'}
|
||||
<p><strong>Message Limit Reached</strong></p>
|
||||
<p>You've used all 50 messages as an anonymous user.</p>
|
||||
<p>Login to continue chatting with unlimited messages.</p>
|
||||
{:else if blockedReason === 'bot_limit'}
|
||||
<p><strong>Bot Limit Reached</strong></p>
|
||||
<p>You've created the maximum of 1 bot as an anonymous user.</p>
|
||||
<p>Login to create more bots.</p>
|
||||
{:else if blockedReason === 'backtest_limit'}
|
||||
<p><strong>Backtest Limit Reached</strong></p>
|
||||
<p>You've run the maximum of 1 backtest as an anonymous user.</p>
|
||||
<p>Login to run more backtests.</p>
|
||||
{:else}
|
||||
<p><strong>Action Blocked</strong></p>
|
||||
<p>Please login to continue.</p>
|
||||
{/if}
|
||||
<button class="login-button" onclick={() => onLogin?.()}>
|
||||
Login to Continue
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
{#if showSlashMenu && filteredTools.length > 0}
|
||||
<div class="slash-menu" style="top: {slashMenuPosition.top}px; left: {slashMenuPosition.left}px;">
|
||||
<div class="slash-menu-header">Available Commands</div>
|
||||
{#each visibleGroups as group}
|
||||
{#if group.tools.some(t => filteredTools.includes(t))}
|
||||
<div class="slash-menu-category">{group.label}</div>
|
||||
{#each group.tools.filter(t => filteredTools.includes(t)) as tool, i}
|
||||
<button
|
||||
class="slash-menu-item"
|
||||
class:selected={filteredTools.indexOf(tool) === selectedIndex}
|
||||
onclick={() => selectTool(tool)}
|
||||
>
|
||||
<span class="slash-command">{tool.command}</span>
|
||||
<span class="slash-description">{tool.description}</span>
|
||||
</button>
|
||||
{/each}
|
||||
{/if}
|
||||
{/each}
|
||||
<div class="slash-menu-hint">
|
||||
{#if !bot}
|
||||
Login to access bot commands
|
||||
{:else}
|
||||
Press Tab to select, Enter to send
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
<textarea
|
||||
value={messageInput}
|
||||
oninput={handleInput}
|
||||
onkeydown={handleKeydown}
|
||||
placeholder="Describe your trading strategy... (or type / for commands)"
|
||||
rows="1"
|
||||
disabled={isSending}
|
||||
></textarea>
|
||||
<button onclick={handleSend} disabled={isSending || !messageInput.trim()}>
|
||||
Send
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
<style>
|
||||
.chat-interface {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.bot-selector {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
padding: 0.75rem 1rem;
|
||||
background: rgba(255, 255, 255, 0.03);
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.bot-selector label {
|
||||
font-size: 0.9rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.bot-selector select {
|
||||
padding: 0.5rem 0.75rem;
|
||||
border-radius: 6px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: #fff;
|
||||
font-size: 0.9rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.bot-selector select:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.chat-messages {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.welcome-message {
|
||||
text-align: center;
|
||||
padding: 2rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.welcome-message .hint {
|
||||
font-size: 0.85rem;
|
||||
margin-top: 1rem;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.message {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.message.user {
|
||||
align-items: flex-end;
|
||||
}
|
||||
|
||||
.message.assistant {
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.message.system {
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.message-content {
|
||||
max-width: 70%;
|
||||
padding: 0.75rem 1rem;
|
||||
border-radius: 12px;
|
||||
line-height: 1.5;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
.message.user .message-content {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border-bottom-right-radius: 4px;
|
||||
}
|
||||
|
||||
.message.assistant .message-content {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border-bottom-left-radius: 4px;
|
||||
}
|
||||
|
||||
.thinking-section {
|
||||
margin-bottom: 0.5rem;
|
||||
padding: 0.5rem 0.75rem;
|
||||
background: rgba(255, 255, 255, 0.03);
|
||||
border-radius: 8px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.thinking-toggle {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
background: none;
|
||||
border: none;
|
||||
color: #888;
|
||||
cursor: pointer;
|
||||
padding: 0.25rem 0.5rem;
|
||||
border-radius: 4px;
|
||||
font-size: 0.8rem;
|
||||
transition: background 0.2s;
|
||||
width: 100%;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.thinking-toggle:hover {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.thinking-icon {
|
||||
font-size: 0.6rem;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.thinking-label {
|
||||
font-weight: 500;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.thinking-preview {
|
||||
color: #666;
|
||||
font-style: italic;
|
||||
font-weight: normal;
|
||||
text-transform: none;
|
||||
letter-spacing: normal;
|
||||
}
|
||||
|
||||
.thinking-content {
|
||||
color: #888;
|
||||
font-size: 0.85rem;
|
||||
padding: 0.75rem 0.5rem;
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.1);
|
||||
margin-top: 0.5rem;
|
||||
white-space: pre-wrap;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.message.system .message-content {
|
||||
background: rgba(251, 191, 36, 0.1);
|
||||
color: #fbbf24;
|
||||
font-size: 0.9rem;
|
||||
border: 1px solid rgba(251, 191, 36, 0.3);
|
||||
}
|
||||
|
||||
.inline-code {
|
||||
background: rgba(0, 0, 0, 0.3);
|
||||
padding: 0.15rem 0.4rem;
|
||||
border-radius: 4px;
|
||||
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
|
||||
font-size: 0.85em;
|
||||
}
|
||||
|
||||
.code-block {
|
||||
background: rgba(0, 0, 0, 0.4);
|
||||
padding: 0.75rem;
|
||||
border-radius: 6px;
|
||||
overflow-x: auto;
|
||||
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
|
||||
font-size: 0.85rem;
|
||||
margin: 0.5rem 0;
|
||||
}
|
||||
|
||||
.code-block code {
|
||||
white-space: pre;
|
||||
}
|
||||
|
||||
ul {
|
||||
margin: 0.5rem 0;
|
||||
padding-left: 1.5rem;
|
||||
}
|
||||
|
||||
li {
|
||||
margin: 0.25rem 0;
|
||||
}
|
||||
|
||||
.content-heading {
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
margin: 1rem 0 0.5rem;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.content-heading:first-child {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.table-wrapper {
|
||||
overflow-x: auto;
|
||||
margin: 0.75rem 0;
|
||||
}
|
||||
|
||||
.markdown-table {
|
||||
border-collapse: collapse;
|
||||
width: 100%;
|
||||
font-size: 0.85rem;
|
||||
background: rgba(0, 0, 0, 0.2);
|
||||
border-radius: 6px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.markdown-table th,
|
||||
.markdown-table td {
|
||||
padding: 0.5rem 0.75rem;
|
||||
text-align: left;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.markdown-table th {
|
||||
background: rgba(102, 126, 234, 0.2);
|
||||
font-weight: 600;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.markdown-table tr:last-child td {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.markdown-table tr:hover td {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
a {
|
||||
color: #667eea;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.message-time {
|
||||
font-size: 0.7rem;
|
||||
color: #666;
|
||||
margin-top: 0.25rem;
|
||||
padding: 0 0.5rem;
|
||||
}
|
||||
|
||||
.typing {
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
padding: 0.5rem;
|
||||
}
|
||||
|
||||
.dot {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
background: #888;
|
||||
border-radius: 50%;
|
||||
animation: typing 1.4s infinite;
|
||||
}
|
||||
|
||||
.dot:nth-child(2) {
|
||||
animation-delay: 0.2s;
|
||||
}
|
||||
|
||||
.dot:nth-child(3) {
|
||||
animation-delay: 0.4s;
|
||||
}
|
||||
|
||||
@keyframes typing {
|
||||
0%, 60%, 100% {
|
||||
transform: translateY(0);
|
||||
opacity: 0.4;
|
||||
}
|
||||
30% {
|
||||
transform: translateY(-4px);
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
.blocked-message {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
padding: 1rem;
|
||||
background: rgba(251, 191, 36, 0.1);
|
||||
border: 1px solid rgba(251, 191, 36, 0.3);
|
||||
border-radius: 12px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.blocked-icon {
|
||||
font-size: 2rem;
|
||||
}
|
||||
|
||||
.blocked-text {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.blocked-text p {
|
||||
margin: 0.25rem 0;
|
||||
font-size: 0.9rem;
|
||||
color: #fbbf24;
|
||||
}
|
||||
|
||||
.blocked-text p:first-child {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.blocked-text p:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.login-button {
|
||||
padding: 0.75rem 1.5rem;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 0.9rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: transform 0.2s;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.login-button:hover {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.input-container {
|
||||
display: flex;
|
||||
gap: 0.75rem;
|
||||
padding: 1rem;
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
textarea {
|
||||
flex: 1;
|
||||
padding: 0.75rem 1rem;
|
||||
border-radius: 12px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: #fff;
|
||||
font-size: 1rem;
|
||||
resize: none;
|
||||
font-family: inherit;
|
||||
}
|
||||
|
||||
textarea:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
button {
|
||||
padding: 0.75rem 1.5rem;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 12px;
|
||||
font-size: 1rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
|
||||
button:hover:not(:disabled) {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.slash-menu {
|
||||
position: fixed;
|
||||
background: rgba(20, 20, 20, 0.98);
|
||||
border: 1px solid rgba(255, 255, 255, 0.15);
|
||||
border-radius: 12px;
|
||||
padding: 0.5rem;
|
||||
min-width: 280px;
|
||||
max-width: 400px;
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
z-index: 1000;
|
||||
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
|
||||
.slash-menu-header {
|
||||
font-size: 0.75rem;
|
||||
color: #888;
|
||||
padding: 0.5rem 0.75rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.slash-menu-category {
|
||||
font-size: 0.75rem;
|
||||
color: #666;
|
||||
padding: 0.5rem 0.75rem 0.25rem;
|
||||
}
|
||||
|
||||
.slash-menu-item {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
width: 100%;
|
||||
padding: 0.5rem 0.75rem;
|
||||
background: transparent;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
text-align: left;
|
||||
transition: background 0.15s;
|
||||
margin: 0.15rem 0;
|
||||
}
|
||||
|
||||
.slash-menu-item:hover,
|
||||
.slash-menu-item.selected {
|
||||
background: rgba(102, 126, 234, 0.2);
|
||||
}
|
||||
|
||||
.slash-command {
|
||||
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
|
||||
font-size: 0.9rem;
|
||||
color: #667eea;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.slash-description {
|
||||
font-size: 0.8rem;
|
||||
color: #888;
|
||||
margin-top: 0.15rem;
|
||||
}
|
||||
|
||||
.slash-menu-hint {
|
||||
font-size: 0.7rem;
|
||||
color: #555;
|
||||
padding: 0.5rem 0.75rem;
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.1);
|
||||
margin-top: 0.5rem;
|
||||
text-align: center;
|
||||
}
|
||||
</style>
|
||||
82
src/frontend/src/lib/components/ChatLayout.svelte
Normal file
82
src/frontend/src/lib/components/ChatLayout.svelte
Normal file
@@ -0,0 +1,82 @@
|
||||
<script lang="ts">
|
||||
import type { Component } from 'svelte';
|
||||
|
||||
interface Props {
|
||||
leftPane?: Component;
|
||||
rightPane?: Component;
|
||||
rightPaneProps?: Record<string, any>;
|
||||
children?: any;
|
||||
}
|
||||
|
||||
let { leftPane: LeftPane, rightPane: RightPane, rightPaneProps = {}, children }: Props = $props();
|
||||
</script>
|
||||
|
||||
<div class="chat-layout">
|
||||
{#if LeftPane}
|
||||
<aside class="sidebar-left">
|
||||
<LeftPane />
|
||||
</aside>
|
||||
{/if}
|
||||
|
||||
<main class="main-content">
|
||||
{#if children}
|
||||
{@render children()}
|
||||
{/if}
|
||||
</main>
|
||||
|
||||
{#if RightPane}
|
||||
<aside class="sidebar-right">
|
||||
<RightPane {...rightPaneProps} />
|
||||
</aside>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.chat-layout {
|
||||
display: flex;
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.sidebar-left {
|
||||
width: 280px;
|
||||
border-right: 1px solid rgba(255, 255, 255, 0.1);
|
||||
flex-shrink: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.main-content {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
background: #0f0f0f;
|
||||
}
|
||||
|
||||
.sidebar-right {
|
||||
width: 300px;
|
||||
border-left: 1px solid rgba(255, 255, 255, 0.1);
|
||||
flex-shrink: 0;
|
||||
overflow-y: auto;
|
||||
background: rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
@media (max-width: 1024px) {
|
||||
.sidebar-right {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.sidebar-left {
|
||||
width: 100%;
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
z-index: 100;
|
||||
background: #0f0f0f;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
348
src/frontend/src/lib/components/ConditionBuilder.svelte
Normal file
348
src/frontend/src/lib/components/ConditionBuilder.svelte
Normal file
@@ -0,0 +1,348 @@
|
||||
<script lang="ts">
|
||||
import type { Condition } from '$lib/api';
|
||||
import TokenPicker from './TokenPicker.svelte';
|
||||
|
||||
interface Props {
|
||||
conditions: Condition[];
|
||||
onUpdate: (conditions: Condition[]) => void;
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
let { conditions, onUpdate, disabled = false }: Props = $props();
|
||||
|
||||
type ConditionType = Condition['type'];
|
||||
|
||||
const conditionTypes: { value: ConditionType; label: string; description: string }[] = [
|
||||
{ value: 'price_drop', label: 'Price Drop', description: 'Trigger when price falls by X%' },
|
||||
{ value: 'price_rise', label: 'Price Rise', description: 'Trigger when price rises by X%' },
|
||||
{ value: 'volume_spike', label: 'Volume Spike', description: 'Trigger when volume increases by X%' },
|
||||
{ value: 'price_level', label: 'Price Level', description: 'Trigger when price crosses a level' },
|
||||
];
|
||||
|
||||
const timeframes = ['1m', '5m', '15m', '1h', '4h', '1d'];
|
||||
|
||||
function addCondition() {
|
||||
const newCondition: Condition = {
|
||||
type: 'price_drop',
|
||||
token: '',
|
||||
threshold: 5,
|
||||
timeframe: '1h'
|
||||
};
|
||||
onUpdate([...conditions, newCondition]);
|
||||
}
|
||||
|
||||
function removeCondition(index: number) {
|
||||
onUpdate(conditions.filter((_, i) => i !== index));
|
||||
}
|
||||
|
||||
function updateCondition(index: number, updates: Partial<Condition>) {
|
||||
const updated = conditions.map((c, i) =>
|
||||
i === index ? { ...c, ...updates } : c
|
||||
);
|
||||
onUpdate(updated);
|
||||
}
|
||||
|
||||
function getConditionDescription(condition: Condition): string {
|
||||
switch (condition.type) {
|
||||
case 'price_drop':
|
||||
return `Price drops ${condition.threshold || 0}% within ${condition.timeframe || '1h'}`;
|
||||
case 'price_rise':
|
||||
return `Price rises ${condition.threshold || 0}% within ${condition.timeframe || '1h'}`;
|
||||
case 'volume_spike':
|
||||
return `Volume spikes ${condition.threshold || 0}% within ${condition.timeframe || '1h'}`;
|
||||
case 'price_level':
|
||||
return `Price crosses ${condition.direction || 'above'} $${condition.price || 0}`;
|
||||
default:
|
||||
return 'Unknown condition';
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="condition-builder">
|
||||
<div class="conditions-header">
|
||||
<h4>Conditions</h4>
|
||||
<button type="button" class="add-btn" onclick={addCondition} {disabled}>
|
||||
+ Add Condition
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{#if conditions.length === 0}
|
||||
<div class="empty-state">
|
||||
<p>No conditions set</p>
|
||||
<p class="hint">Add a condition to define when your strategy triggers</p>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="conditions-list">
|
||||
{#each conditions as condition, index}
|
||||
<div class="condition-card">
|
||||
<div class="condition-header">
|
||||
<span class="condition-number">#{index + 1}</span>
|
||||
<button
|
||||
type="button"
|
||||
class="remove-btn"
|
||||
onclick={() => removeCondition(index)}
|
||||
disabled={disabled}
|
||||
aria-label="Remove condition"
|
||||
>
|
||||
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
||||
<line x1="18" y1="6" x2="6" y2="18"></line>
|
||||
<line x1="6" y1="6" x2="18" y2="18"></line>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="condition-fields">
|
||||
<div class="field">
|
||||
<label for="type-{index}">Type</label>
|
||||
<select
|
||||
id="type-{index}"
|
||||
value={condition.type}
|
||||
onchange={(e) => updateCondition(index, { type: (e.target as HTMLSelectElement).value as ConditionType })}
|
||||
disabled={disabled}
|
||||
>
|
||||
{#each conditionTypes as ct}
|
||||
<option value={ct.value}>{ct.label}</option>
|
||||
{/each}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<TokenPicker
|
||||
label="Token"
|
||||
selectedToken={condition.token}
|
||||
selectedChain={condition.chain || ''}
|
||||
onSelect={(token, chain) => updateCondition(index, { token, chain })}
|
||||
disabled={disabled}
|
||||
/>
|
||||
|
||||
{#if condition.type === 'price_level'}
|
||||
<div class="field">
|
||||
<label for="direction-{index}">Direction</label>
|
||||
<select
|
||||
id="direction-{index}"
|
||||
value={condition.direction || 'above'}
|
||||
onchange={(e) => updateCondition(index, { direction: (e.target as HTMLSelectElement).value as 'above' | 'below' })}
|
||||
disabled={disabled}
|
||||
>
|
||||
<option value="above">Above</option>
|
||||
<option value="below">Below</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="price-{index}">Price ($)</label>
|
||||
<input
|
||||
id="price-{index}"
|
||||
type="number"
|
||||
value={condition.price || ''}
|
||||
oninput={(e) => updateCondition(index, { price: parseFloat((e.target as HTMLInputElement).value) || undefined })}
|
||||
placeholder="0.000001"
|
||||
step="any"
|
||||
min="0"
|
||||
disabled={disabled}
|
||||
/>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="field">
|
||||
<label for="threshold-{index}">Threshold (%)</label>
|
||||
<input
|
||||
id="threshold-{index}"
|
||||
type="number"
|
||||
value={condition.threshold || ''}
|
||||
oninput={(e) => updateCondition(index, { threshold: parseFloat((e.target as HTMLInputElement).value) || undefined })}
|
||||
placeholder="5"
|
||||
step="any"
|
||||
min="0"
|
||||
disabled={disabled}
|
||||
/>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="timeframe-{index}">Timeframe</label>
|
||||
<select
|
||||
id="timeframe-{index}"
|
||||
value={condition.timeframe || '1h'}
|
||||
onchange={(e) => updateCondition(index, { timeframe: (e.target as HTMLSelectElement).value })}
|
||||
disabled={disabled}
|
||||
>
|
||||
{#each timeframes as tf}
|
||||
<option value={tf}>{tf}</option>
|
||||
{/each}
|
||||
</select>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="condition-preview">
|
||||
<span class="preview-label">Summary:</span>
|
||||
{getConditionDescription(condition)}
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.condition-builder {
|
||||
background: rgba(255, 255, 255, 0.02);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 12px;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.conditions-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
h4 {
|
||||
margin: 0;
|
||||
font-size: 0.9rem;
|
||||
color: #888;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
}
|
||||
|
||||
.add-btn {
|
||||
padding: 0.5rem 1rem;
|
||||
background: rgba(102, 126, 234, 0.2);
|
||||
color: #667eea;
|
||||
border: 1px solid rgba(102, 126, 234, 0.4);
|
||||
border-radius: 6px;
|
||||
font-size: 0.85rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.add-btn:hover:not(:disabled) {
|
||||
background: rgba(102, 126, 234, 0.3);
|
||||
}
|
||||
|
||||
.add-btn:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 1.5rem;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.empty-state .hint {
|
||||
font-size: 0.85rem;
|
||||
margin-top: 0.5rem;
|
||||
}
|
||||
|
||||
.conditions-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.condition-card {
|
||||
background: rgba(0, 0, 0, 0.2);
|
||||
border: 1px solid rgba(255, 255, 255, 0.05);
|
||||
border-radius: 8px;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.condition-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.condition-number {
|
||||
font-size: 0.85rem;
|
||||
font-weight: 600;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.remove-btn {
|
||||
background: none;
|
||||
border: none;
|
||||
color: #888;
|
||||
cursor: pointer;
|
||||
padding: 0.25rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border-radius: 4px;
|
||||
transition: background 0.2s, color 0.2s;
|
||||
}
|
||||
|
||||
.remove-btn:hover:not(:disabled) {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
color: #ef4444;
|
||||
}
|
||||
|
||||
.remove-btn:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.condition-fields {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
||||
gap: 0.75rem;
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.35rem;
|
||||
}
|
||||
|
||||
.field label {
|
||||
font-size: 0.8rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
input,
|
||||
select {
|
||||
padding: 0.5rem 0.75rem;
|
||||
border-radius: 6px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.15);
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: #fff;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
input:focus,
|
||||
select:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
input:disabled,
|
||||
select:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
input[type="number"] {
|
||||
-moz-appearance: textfield;
|
||||
}
|
||||
|
||||
input[type="number"]::-webkit-inner-spin-button,
|
||||
input[type="number"]::-webkit-outer-spin-button {
|
||||
-webkit-appearance: none;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.condition-preview {
|
||||
padding-top: 0.75rem;
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.05);
|
||||
font-size: 0.85rem;
|
||||
color: #aaa;
|
||||
}
|
||||
|
||||
.preview-label {
|
||||
color: #666;
|
||||
margin-right: 0.5rem;
|
||||
}
|
||||
</style>
|
||||
178
src/frontend/src/lib/components/ConversationList.svelte
Normal file
178
src/frontend/src/lib/components/ConversationList.svelte
Normal file
@@ -0,0 +1,178 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { goto } from '$app/navigation';
|
||||
import { page } from '$app/stores';
|
||||
import { api } from '$lib/api';
|
||||
import { conversationStore, setConversations, addConversation } from '$lib/stores';
|
||||
|
||||
let conversations = $state<any[]>([]);
|
||||
let loading = $state(true);
|
||||
let error = $state<string | null>(null);
|
||||
|
||||
$effect(() => {
|
||||
const unsub = conversationStore.subscribe(state => {
|
||||
conversations = state.conversations;
|
||||
});
|
||||
return unsub;
|
||||
});
|
||||
|
||||
onMount(async () => {
|
||||
await loadConversations();
|
||||
});
|
||||
|
||||
async function loadConversations() {
|
||||
loading = true;
|
||||
error = null;
|
||||
try {
|
||||
const data = await api.conversations.list();
|
||||
setConversations(data);
|
||||
} catch (e) {
|
||||
error = e instanceof Error ? e.message : 'Failed to load conversations';
|
||||
setConversations([]);
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function createConversation() {
|
||||
try {
|
||||
const newConv = await api.conversations.create();
|
||||
addConversation(newConv);
|
||||
goto(`/chat/${newConv.id}`);
|
||||
} catch (e) {
|
||||
console.error('Failed to create conversation:', e);
|
||||
}
|
||||
}
|
||||
|
||||
function formatDate(dateStr: string): string {
|
||||
const date = new Date(dateStr);
|
||||
const now = new Date();
|
||||
const diffMs = now.getTime() - date.getTime();
|
||||
const diffMins = Math.floor(diffMs / 60000);
|
||||
const diffHours = Math.floor(diffMs / 3600000);
|
||||
const diffDays = Math.floor(diffMs / 86400000);
|
||||
|
||||
if (diffMins < 1) return 'just now';
|
||||
if (diffMins < 60) return `${diffMins}m ago`;
|
||||
if (diffHours < 24) return `${diffHours}h ago`;
|
||||
if (diffDays < 7) return `${diffDays}d ago`;
|
||||
return date.toLocaleDateString();
|
||||
}
|
||||
|
||||
function isActiveConversation(convId: string): boolean {
|
||||
return $page.params.conversationId === convId;
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="conversation-list">
|
||||
<div class="header">
|
||||
<button class="new-chat-btn" onclick={createConversation}>
|
||||
+ New Chat
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="list">
|
||||
{#if loading}
|
||||
<div class="loading">Loading...</div>
|
||||
{:else if error}
|
||||
<div class="error">{error}</div>
|
||||
{:else if conversations.length === 0}
|
||||
<div class="empty">No conversations yet</div>
|
||||
{:else}
|
||||
{#each conversations as conv (conv.id)}
|
||||
<button
|
||||
class="conversation-item"
|
||||
class:active={isActiveConversation(conv.id)}
|
||||
onclick={() => goto(`/chat/${conv.id}`)}
|
||||
>
|
||||
<div class="conv-title">{conv.title || 'New Chat'}</div>
|
||||
<div class="conv-date">{formatDate(conv.updated_at)}</div>
|
||||
</button>
|
||||
{/each}
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.conversation-list {
|
||||
height: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background: #0f0f0f;
|
||||
}
|
||||
|
||||
.header {
|
||||
padding: 0.75rem;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.new-chat-btn {
|
||||
width: 100%;
|
||||
padding: 0.75rem;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 0.95rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
|
||||
.new-chat-btn:hover {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.list {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.loading,
|
||||
.error,
|
||||
.empty {
|
||||
padding: 1.5rem;
|
||||
text-align: center;
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #f87171;
|
||||
}
|
||||
|
||||
.conversation-item {
|
||||
width: 100%;
|
||||
padding: 0.875rem 1rem;
|
||||
background: transparent;
|
||||
border: none;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
|
||||
text-align: left;
|
||||
cursor: pointer;
|
||||
transition: background 0.15s;
|
||||
}
|
||||
|
||||
.conversation-item:hover {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
.conversation-item.active {
|
||||
background: rgba(102, 126, 234, 0.15);
|
||||
border-left: 3px solid #667eea;
|
||||
}
|
||||
|
||||
.conv-title {
|
||||
color: #e0e0e0;
|
||||
font-size: 0.95rem;
|
||||
font-weight: 500;
|
||||
margin-bottom: 0.25rem;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.conv-date {
|
||||
color: #666;
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
</style>
|
||||
139
src/frontend/src/lib/components/PortfolioSummary.svelte
Normal file
139
src/frontend/src/lib/components/PortfolioSummary.svelte
Normal file
@@ -0,0 +1,139 @@
|
||||
<script lang="ts">
|
||||
interface Props {
|
||||
initialBalance?: number;
|
||||
currentBalance?: number;
|
||||
position?: number;
|
||||
positionToken?: string;
|
||||
entryPrice?: number;
|
||||
currentPrice?: number;
|
||||
}
|
||||
|
||||
let {
|
||||
initialBalance = 10000,
|
||||
currentBalance = 10000,
|
||||
position = 0,
|
||||
positionToken = '',
|
||||
entryPrice = 0,
|
||||
currentPrice = 0
|
||||
}: Props = $props();
|
||||
|
||||
// Calculate metrics
|
||||
let positionValue = $derived(position * currentPrice);
|
||||
let totalValue = $derived(currentBalance + positionValue);
|
||||
let pnl = $derived(totalValue - initialBalance);
|
||||
let pnlPercent = $derived((pnl / initialBalance) * 100);
|
||||
let unrealizedPnL = $derived(position > 0 && entryPrice > 0 ? (currentPrice - entryPrice) / entryPrice * 100 : 0);
|
||||
</script>
|
||||
|
||||
<div class="portfolio-summary">
|
||||
<div class="metric">
|
||||
<span class="label">Cash Balance</span>
|
||||
<span class="value">${currentBalance.toFixed(2)}</span>
|
||||
</div>
|
||||
|
||||
{#if position > 0}
|
||||
<div class="metric">
|
||||
<span class="label">Position ({positionToken || 'Token'})</span>
|
||||
<span class="value highlight">{position.toFixed(6)}</span>
|
||||
</div>
|
||||
|
||||
<div class="metric">
|
||||
<span class="label">Position Value</span>
|
||||
<span class="value">${positionValue.toFixed(2)}</span>
|
||||
</div>
|
||||
|
||||
<div class="metric">
|
||||
<span class="label">Entry Price</span>
|
||||
<span class="value">${entryPrice.toFixed(8)}</span>
|
||||
</div>
|
||||
|
||||
<div class="metric">
|
||||
<span class="label">Current Price</span>
|
||||
<span class="value">${currentPrice.toFixed(8)}</span>
|
||||
</div>
|
||||
|
||||
<div class="metric">
|
||||
<span class="label">Unrealized P&L</span>
|
||||
<span class="value" class:positive={unrealizedPnL > 0} class:negative={unrealizedPnL < 0}>
|
||||
{unrealizedPnL >= 0 ? '+' : ''}{unrealizedPnL.toFixed(2)}%
|
||||
</span>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="divider"></div>
|
||||
|
||||
<div class="metric total">
|
||||
<span class="label">Total Value</span>
|
||||
<span class="value">${totalValue.toFixed(2)}</span>
|
||||
</div>
|
||||
|
||||
<div class="metric">
|
||||
<span class="label">P&L</span>
|
||||
<span class="value large" class:positive={pnl > 0} class:negative={pnl < 0}>
|
||||
{pnl >= 0 ? '+' : ''}${pnl.toFixed(2)} ({pnlPercent >= 0 ? '+' : ''}{pnlPercent.toFixed(2)}%)
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.portfolio-summary {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
|
||||
gap: 1rem;
|
||||
padding: 1rem;
|
||||
background: rgba(255, 255, 255, 0.02);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.metric {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
|
||||
.metric .label {
|
||||
font-size: 0.75rem;
|
||||
color: #888;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
}
|
||||
|
||||
.metric .value {
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
color: #fff;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.metric .value.highlight {
|
||||
color: #fbbf24;
|
||||
}
|
||||
|
||||
.metric .value.large {
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
|
||||
.metric.total {
|
||||
grid-column: 1 / -1;
|
||||
padding-top: 0.5rem;
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
.metric.total .value {
|
||||
font-size: 1.5rem;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.positive {
|
||||
color: #22c55e !important;
|
||||
}
|
||||
|
||||
.negative {
|
||||
color: #ef4444 !important;
|
||||
}
|
||||
|
||||
.divider {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
121
src/frontend/src/lib/components/ProUpgradeBanner.svelte
Normal file
121
src/frontend/src/lib/components/ProUpgradeBanner.svelte
Normal file
@@ -0,0 +1,121 @@
|
||||
<script lang="ts">
|
||||
interface Props {
|
||||
feature?: string;
|
||||
dismissible?: boolean;
|
||||
onDismiss?: () => void;
|
||||
}
|
||||
|
||||
let { feature, dismissible = true, onDismiss }: Props = $props();
|
||||
</script>
|
||||
|
||||
<div class="pro-upgrade-banner">
|
||||
<div class="banner-content">
|
||||
<div class="banner-icon">
|
||||
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
||||
<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"></polygon>
|
||||
</svg>
|
||||
</div>
|
||||
<div class="banner-text">
|
||||
<strong>Upgrade to Pro</strong>
|
||||
{#if feature}
|
||||
<p>{feature}</p>
|
||||
{:else}
|
||||
<p>Unlock advanced features and unlimited bots</p>
|
||||
{/if}
|
||||
</div>
|
||||
<a href="/settings" class="upgrade-btn">Upgrade Now</a>
|
||||
</div>
|
||||
{#if dismissible && onDismiss}
|
||||
<button class="dismiss-btn" onclick={onDismiss} aria-label="Dismiss">
|
||||
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
||||
<line x1="18" y1="6" x2="6" y2="18"></line>
|
||||
<line x1="6" y1="6" x2="18" y2="18"></line>
|
||||
</svg>
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.pro-upgrade-banner {
|
||||
position: relative;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
background: linear-gradient(135deg, rgba(102, 126, 234, 0.15) 0%, rgba(118, 75, 162, 0.15) 100%);
|
||||
border: 1px solid rgba(102, 126, 234, 0.3);
|
||||
border-radius: 12px;
|
||||
padding: 1rem 1.5rem;
|
||||
margin: 1rem 0;
|
||||
}
|
||||
|
||||
.banner-content {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.banner-icon {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
background: rgba(102, 126, 234, 0.2);
|
||||
border-radius: 8px;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.banner-text {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.banner-text strong {
|
||||
display: block;
|
||||
color: #fff;
|
||||
font-size: 1rem;
|
||||
margin-bottom: 0.25rem;
|
||||
}
|
||||
|
||||
.banner-text p {
|
||||
margin: 0;
|
||||
font-size: 0.85rem;
|
||||
color: #aaa;
|
||||
}
|
||||
|
||||
.upgrade-btn {
|
||||
padding: 0.5rem 1rem;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border-radius: 8px;
|
||||
text-decoration: none;
|
||||
font-weight: 500;
|
||||
font-size: 0.9rem;
|
||||
transition: transform 0.2s, box-shadow 0.2s;
|
||||
}
|
||||
|
||||
.upgrade-btn:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
|
||||
}
|
||||
|
||||
.dismiss-btn {
|
||||
position: absolute;
|
||||
top: 0.5rem;
|
||||
right: 0.5rem;
|
||||
background: none;
|
||||
border: none;
|
||||
color: #888;
|
||||
cursor: pointer;
|
||||
padding: 0.25rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border-radius: 4px;
|
||||
transition: background 0.2s, color 0.2s;
|
||||
}
|
||||
|
||||
.dismiss-btn:hover {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
color: #fff;
|
||||
}
|
||||
</style>
|
||||
266
src/frontend/src/lib/components/SignalChart.svelte
Normal file
266
src/frontend/src/lib/components/SignalChart.svelte
Normal file
@@ -0,0 +1,266 @@
|
||||
<script lang="ts">
|
||||
import type { Signal } from '$lib/api';
|
||||
import { onMount, tick } from 'svelte';
|
||||
|
||||
interface Props {
|
||||
signals?: Signal[];
|
||||
klines?: { time: number; close: number }[];
|
||||
height?: number;
|
||||
}
|
||||
|
||||
let { signals = [], klines = [], height = 200 }: Props = $props();
|
||||
|
||||
let width = $state(800);
|
||||
let containerEl: HTMLDivElement;
|
||||
let canvasEl: HTMLCanvasElement;
|
||||
let initialized = $state(false);
|
||||
|
||||
onMount(() => {
|
||||
// Set initial width
|
||||
if (containerEl) {
|
||||
width = containerEl.clientWidth;
|
||||
}
|
||||
|
||||
// Resize observer
|
||||
const resizeObserver = new ResizeObserver(entries => {
|
||||
for (const entry of entries) {
|
||||
width = entry.contentRect.width;
|
||||
drawChart();
|
||||
}
|
||||
});
|
||||
|
||||
if (containerEl) {
|
||||
resizeObserver.observe(containerEl);
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
|
||||
return () => {
|
||||
resizeObserver.disconnect();
|
||||
};
|
||||
});
|
||||
|
||||
// Draw when data changes
|
||||
$effect(() => {
|
||||
// Access reactive values to trigger effect
|
||||
const currentSignals = signals;
|
||||
const currentKlines = klines;
|
||||
const currentWidth = width;
|
||||
|
||||
// Wait for DOM to be ready
|
||||
tick().then(() => {
|
||||
drawChart();
|
||||
});
|
||||
});
|
||||
|
||||
function drawChart() {
|
||||
if (!canvasEl) {
|
||||
return;
|
||||
}
|
||||
|
||||
const ctx = canvasEl.getContext('2d');
|
||||
if (!ctx) return;
|
||||
|
||||
const dpr = window.devicePixelRatio || 1;
|
||||
canvasEl.width = width * dpr;
|
||||
canvasEl.height = height * dpr;
|
||||
ctx.scale(dpr, dpr);
|
||||
|
||||
// Clear canvas
|
||||
ctx.clearRect(0, 0, width, height);
|
||||
|
||||
// Check if we have data
|
||||
if (klines.length === 0 && signals.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get price data
|
||||
let priceData: { time: number; price: number }[] = [];
|
||||
|
||||
if (klines.length > 0) {
|
||||
priceData = klines.map(k => ({
|
||||
time: k.time,
|
||||
price: typeof k.close === 'string' ? parseFloat(k.close) : k.close
|
||||
})).filter(d => !isNaN(d.price) && d.price > 0);
|
||||
} else if (signals.length > 0) {
|
||||
priceData = signals.map(s => ({ time: 0, price: s.price }));
|
||||
}
|
||||
|
||||
if (priceData.length === 0) return;
|
||||
|
||||
const prices = priceData.map(d => d.price);
|
||||
const padding = { top: 20, right: 20, bottom: 45, left: 60 }; // More bottom padding for time labels
|
||||
const chartWidth = width - padding.left - padding.right;
|
||||
const chartHeight = height - padding.top - padding.bottom;
|
||||
|
||||
// Price range with padding
|
||||
const minPrice = Math.min(...prices);
|
||||
const maxPrice = Math.max(...prices);
|
||||
const priceRange = maxPrice - minPrice || 1;
|
||||
const paddedMin = minPrice - priceRange * 0.1;
|
||||
const paddedMax = maxPrice + priceRange * 0.1;
|
||||
|
||||
function priceToY(price: number): number {
|
||||
return padding.top + (1 - (price - paddedMin) / (paddedMax - paddedMin)) * chartHeight;
|
||||
}
|
||||
|
||||
function indexToX(index: number): number {
|
||||
return padding.left + (index / Math.max(prices.length - 1, 1)) * chartWidth;
|
||||
}
|
||||
|
||||
// Draw grid lines
|
||||
ctx.strokeStyle = 'rgba(255, 255, 255, 0.05)';
|
||||
ctx.lineWidth = 1;
|
||||
for (let i = 0; i <= 4; i++) {
|
||||
const y = padding.top + (i / 4) * chartHeight;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(padding.left, y);
|
||||
ctx.lineTo(width - padding.right, y);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
// Draw Y axis labels
|
||||
ctx.fillStyle = '#888';
|
||||
ctx.font = '10px monospace';
|
||||
ctx.textAlign = 'right';
|
||||
for (let i = 0; i <= 4; i++) {
|
||||
const price = paddedMax - (i / 4) * (paddedMax - paddedMin);
|
||||
const y = padding.top + (i / 4) * chartHeight + 4;
|
||||
ctx.fillText('$' + price.toFixed(6), padding.left - 5, y);
|
||||
}
|
||||
|
||||
// Draw price line
|
||||
ctx.beginPath();
|
||||
ctx.strokeStyle = '#667eea';
|
||||
ctx.lineWidth = 2;
|
||||
ctx.moveTo(indexToX(0), priceToY(prices[0]));
|
||||
for (let i = 1; i < prices.length; i++) {
|
||||
ctx.lineTo(indexToX(i), priceToY(prices[i]));
|
||||
}
|
||||
ctx.stroke();
|
||||
|
||||
// Fill area under line
|
||||
ctx.lineTo(indexToX(prices.length - 1), padding.top + chartHeight);
|
||||
ctx.lineTo(indexToX(0), padding.top + chartHeight);
|
||||
ctx.closePath();
|
||||
const gradient = ctx.createLinearGradient(0, padding.top, 0, padding.top + chartHeight);
|
||||
gradient.addColorStop(0, 'rgba(102, 126, 234, 0.3)');
|
||||
gradient.addColorStop(1, 'rgba(102, 126, 234, 0)');
|
||||
ctx.fillStyle = gradient;
|
||||
ctx.fill();
|
||||
|
||||
// Draw signal markers
|
||||
if (signals.length > 0) {
|
||||
signals.forEach((signal) => {
|
||||
// Find closest price match
|
||||
const signalPrice = signal.price;
|
||||
let closestIndex = 0;
|
||||
let closestDiff = Infinity;
|
||||
|
||||
for (let i = 0; i < priceData.length; i++) {
|
||||
const diff = Math.abs(priceData[i].price - signalPrice);
|
||||
if (diff < closestDiff) {
|
||||
closestDiff = diff;
|
||||
closestIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
const x = indexToX(closestIndex);
|
||||
const y = priceToY(signalPrice);
|
||||
const color = signal.signal_type === 'buy' ? '#22c55e' : '#ef4444';
|
||||
|
||||
// Vertical dashed line
|
||||
ctx.beginPath();
|
||||
ctx.strokeStyle = color;
|
||||
ctx.setLineDash([4, 4]);
|
||||
ctx.moveTo(x, padding.top);
|
||||
ctx.lineTo(x, y);
|
||||
ctx.stroke();
|
||||
ctx.setLineDash([]);
|
||||
|
||||
// Signal dot
|
||||
ctx.beginPath();
|
||||
ctx.arc(x, y, 6, 0, Math.PI * 2);
|
||||
ctx.fillStyle = color;
|
||||
ctx.fill();
|
||||
ctx.strokeStyle = '#fff';
|
||||
ctx.lineWidth = 2;
|
||||
ctx.stroke();
|
||||
});
|
||||
}
|
||||
|
||||
// Draw X axis time labels
|
||||
ctx.fillStyle = '#666';
|
||||
ctx.font = '9px monospace';
|
||||
ctx.textAlign = 'center';
|
||||
|
||||
const numTimeLabels = Math.min(5, priceData.length);
|
||||
for (let i = 0; i < numTimeLabels; i++) {
|
||||
const dataIndex = Math.floor(i * (priceData.length - 1) / (numTimeLabels - 1 || 1));
|
||||
const x = indexToX(dataIndex);
|
||||
|
||||
// Convert timestamp to readable time
|
||||
let timeLabel = '';
|
||||
if (priceData[dataIndex].time > 0) {
|
||||
const date = new Date(priceData[dataIndex].time * 1000);
|
||||
timeLabel = date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
||||
} else {
|
||||
timeLabel = `${dataIndex + 1}`;
|
||||
}
|
||||
|
||||
ctx.fillText(timeLabel, x, height - 5);
|
||||
}
|
||||
|
||||
// Legend
|
||||
ctx.fillStyle = '#888';
|
||||
ctx.font = '12px sans-serif';
|
||||
ctx.textAlign = 'center';
|
||||
|
||||
if (signals.length > 0) {
|
||||
const buyCount = signals.filter(s => s.signal_type === 'buy').length;
|
||||
const sellCount = signals.filter(s => s.signal_type === 'sell').length;
|
||||
ctx.fillText(`📈 ${buyCount} Buy | ${sellCount} Sell | ${priceData.length} Candles`, width / 2, height - 20);
|
||||
} else {
|
||||
ctx.fillText(`${priceData.length} Candles (No signals generated)`, width / 2, height - 20);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="signal-chart" bind:this={containerEl}>
|
||||
{#if klines.length === 0 && signals.length === 0}
|
||||
<div class="empty-state">
|
||||
<p>No data to display. Start a simulation to see price movements.</p>
|
||||
</div>
|
||||
{:else}
|
||||
<canvas
|
||||
bind:this={canvasEl}
|
||||
style="width: 100%; height: {height}px;"
|
||||
></canvas>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.signal-chart {
|
||||
width: 100%;
|
||||
background: rgba(255, 255, 255, 0.02);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 8px;
|
||||
padding: 1rem;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 200px;
|
||||
color: #666;
|
||||
text-align: center;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
canvas {
|
||||
display: block;
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
228
src/frontend/src/lib/components/StrategyPreview.svelte
Normal file
228
src/frontend/src/lib/components/StrategyPreview.svelte
Normal file
@@ -0,0 +1,228 @@
|
||||
<script lang="ts">
|
||||
import type { StrategyConfig } from '$lib/api';
|
||||
|
||||
interface Props {
|
||||
config: StrategyConfig | null;
|
||||
editable?: boolean;
|
||||
onUpdate?: (config: StrategyConfig) => void;
|
||||
}
|
||||
|
||||
let { config, editable = false, onUpdate }: Props = $props();
|
||||
|
||||
function getConditionDescription(condition: StrategyConfig['conditions'][0]): string {
|
||||
const timeframe = condition.timeframe ? ` within ${condition.timeframe}` : '';
|
||||
switch (condition.type) {
|
||||
case 'price_drop':
|
||||
return `${condition.token} drops by ${condition.threshold}%${timeframe}`;
|
||||
case 'price_rise':
|
||||
return `${condition.token} rises by ${condition.threshold}%${timeframe}`;
|
||||
case 'volume_spike':
|
||||
return `${condition.token} volume spikes by ${condition.threshold}%${timeframe}`;
|
||||
case 'price_level':
|
||||
return `${condition.token} crosses ${condition.direction} $${condition.price}`;
|
||||
default:
|
||||
return 'Unknown condition';
|
||||
}
|
||||
}
|
||||
|
||||
function getActionDescription(action: StrategyConfig['actions'][0]): string {
|
||||
switch (action.type) {
|
||||
case 'buy':
|
||||
return `Buy ${action.amount_percent}% of ${action.token || 'portfolio'}`;
|
||||
case 'sell':
|
||||
return `Sell ${action.amount_percent}% of ${action.token || 'portfolio'}`;
|
||||
case 'hold':
|
||||
return 'Hold';
|
||||
default:
|
||||
return 'Unknown action';
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="strategy-preview">
|
||||
{#if !config || (config.conditions.length === 0 && config.actions.length === 0)}
|
||||
<div class="empty-state">
|
||||
<p>No strategy configured yet.</p>
|
||||
<p class="hint">Describe your trading strategy in the chat to create one.</p>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="strategy-section">
|
||||
<h4>Conditions</h4>
|
||||
{#if config.conditions.length === 0}
|
||||
<p class="empty">No conditions set</p>
|
||||
{:else}
|
||||
<ul class="items-list">
|
||||
{#each config.conditions as condition, i}
|
||||
<li>
|
||||
<span class="condition-badge">{condition.type.replace('_', ' ')}</span>
|
||||
{getConditionDescription(condition)}
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="strategy-section">
|
||||
<h4>Actions</h4>
|
||||
{#if config.actions.length === 0}
|
||||
<p class="empty">No actions set</p>
|
||||
{:else}
|
||||
<ul class="items-list">
|
||||
{#each config.actions as action}
|
||||
<li>
|
||||
<span class="action-badge action-{action.type}">{action.type}</span>
|
||||
{getActionDescription(action)}
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if config.risk_management}
|
||||
<div class="strategy-section">
|
||||
<h4>Risk Management</h4>
|
||||
<div class="risk-items">
|
||||
{#if config.risk_management.stop_loss_percent}
|
||||
<div class="risk-item">
|
||||
<span class="risk-label">Stop Loss</span>
|
||||
<span class="risk-value negative">{config.risk_management.stop_loss_percent}%</span>
|
||||
</div>
|
||||
{/if}
|
||||
{#if config.risk_management.take_profit_percent}
|
||||
<div class="risk-item">
|
||||
<span class="risk-label">Take Profit</span>
|
||||
<span class="risk-value positive">{config.risk_management.take_profit_percent}%</span>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.strategy-preview {
|
||||
background: rgba(255, 255, 255, 0.03);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 8px;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 1rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.empty-state .hint {
|
||||
font-size: 0.85rem;
|
||||
color: #666;
|
||||
margin-top: 0.5rem;
|
||||
}
|
||||
|
||||
.strategy-section {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.strategy-section:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
h4 {
|
||||
font-size: 0.85rem;
|
||||
font-weight: 600;
|
||||
color: #888;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
margin: 0 0 0.75rem;
|
||||
}
|
||||
|
||||
.items-list {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.items-list li {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
padding: 0.5rem 0;
|
||||
font-size: 0.9rem;
|
||||
color: #ccc;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
.items-list li:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.condition-badge,
|
||||
.action-badge {
|
||||
padding: 0.2rem 0.5rem;
|
||||
border-radius: 4px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.condition-badge {
|
||||
background: rgba(102, 126, 234, 0.2);
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.action-badge {
|
||||
min-width: 50px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.action-buy {
|
||||
background: rgba(34, 197, 94, 0.2);
|
||||
color: #22c55e;
|
||||
}
|
||||
|
||||
.action-sell {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
color: #ef4444;
|
||||
}
|
||||
|
||||
.action-hold {
|
||||
background: rgba(251, 191, 36, 0.2);
|
||||
color: #fbbf24;
|
||||
}
|
||||
|
||||
.empty {
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.risk-items {
|
||||
display: flex;
|
||||
gap: 1.5rem;
|
||||
}
|
||||
|
||||
.risk-item {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
|
||||
.risk-label {
|
||||
font-size: 0.75rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.risk-value {
|
||||
font-size: 1.1rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.positive {
|
||||
color: #22c55e;
|
||||
}
|
||||
|
||||
.negative {
|
||||
color: #ef4444;
|
||||
}
|
||||
</style>
|
||||
256
src/frontend/src/lib/components/TokenPicker.svelte
Normal file
256
src/frontend/src/lib/components/TokenPicker.svelte
Normal file
@@ -0,0 +1,256 @@
|
||||
<script lang="ts">
|
||||
import { api } from '$lib/api';
|
||||
|
||||
interface Token {
|
||||
symbol: string;
|
||||
chain: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
selectedToken?: string;
|
||||
selectedChain?: string;
|
||||
onSelect: (token: string, chain: string) => void;
|
||||
disabled?: boolean;
|
||||
label?: string;
|
||||
}
|
||||
|
||||
let { selectedToken = '', selectedChain = '', onSelect, disabled = false, label = 'Select Token' }: Props = $props();
|
||||
|
||||
let searchQuery = $state('');
|
||||
let isOpen = $state(false);
|
||||
let tokens = $state<Token[]>([]);
|
||||
let isLoading = $state(false);
|
||||
let inputEl: HTMLInputElement;
|
||||
let containerEl: HTMLDivElement;
|
||||
|
||||
const commonTokens: Token[] = [
|
||||
{ symbol: 'BTC', chain: 'btc', name: 'Bitcoin' },
|
||||
{ symbol: 'ETH', chain: 'eth', name: 'Ethereum' },
|
||||
{ symbol: 'BNB', chain: 'bsc', name: 'BNB' },
|
||||
{ symbol: 'PEPE', chain: 'bsc', name: 'Pepe' },
|
||||
{ symbol: 'SHIB', chain: 'eth', name: 'Shiba Inu' },
|
||||
{ symbol: 'DOGE', chain: 'doge', name: 'Dogecoin' },
|
||||
{ symbol: 'SOL', chain: 'sol', name: 'Solana' },
|
||||
{ symbol: 'XRP', chain: 'xrp', name: 'Ripple' },
|
||||
];
|
||||
|
||||
$effect(() => {
|
||||
function handleClickOutside(event: MouseEvent) {
|
||||
if (containerEl && !containerEl.contains(event.target as Node)) {
|
||||
isOpen = false;
|
||||
}
|
||||
}
|
||||
document.addEventListener('click', handleClickOutside);
|
||||
return () => document.removeEventListener('click', handleClickOutside);
|
||||
});
|
||||
|
||||
async function loadTokens() {
|
||||
isLoading = true;
|
||||
try {
|
||||
tokens = await api.config.getTokens();
|
||||
} catch (e) {
|
||||
tokens = commonTokens;
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
function getFilteredTokens(): Token[] {
|
||||
const allTokens = tokens.length > 0 ? tokens : commonTokens;
|
||||
if (!searchQuery) return allTokens.slice(0, 10);
|
||||
const query = searchQuery.toLowerCase();
|
||||
return allTokens.filter(
|
||||
t => t.symbol.toLowerCase().includes(query) ||
|
||||
t.name.toLowerCase().includes(query) ||
|
||||
t.chain.toLowerCase().includes(query)
|
||||
).slice(0, 10);
|
||||
}
|
||||
|
||||
function handleSelect(token: Token) {
|
||||
onSelect(token.symbol, token.chain);
|
||||
searchQuery = '';
|
||||
isOpen = false;
|
||||
}
|
||||
|
||||
function handleInputFocus() {
|
||||
isOpen = true;
|
||||
if (tokens.length === 0 && !isLoading) {
|
||||
loadTokens();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="token-picker" bind:this={containerEl}>
|
||||
{#if label}
|
||||
<label>{label}</label>
|
||||
{/if}
|
||||
|
||||
<div class="input-wrapper">
|
||||
<input
|
||||
type="text"
|
||||
bind:this={inputEl}
|
||||
bind:value={searchQuery}
|
||||
onfocus={handleInputFocus}
|
||||
placeholder={selectedToken ? `${selectedToken}${selectedChain ? ` (${selectedChain})` : ''}` : 'Search tokens...'}
|
||||
{disabled}
|
||||
class:has-value={selectedToken}
|
||||
/>
|
||||
{#if selectedToken}
|
||||
<button class="clear-btn" onclick={() => onSelect('', '')} disabled={disabled}>
|
||||
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
||||
<line x1="18" y1="6" x2="6" y2="18"></line>
|
||||
<line x1="6" y1="6" x2="18" y2="18"></line>
|
||||
</svg>
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if isOpen}
|
||||
<div class="dropdown">
|
||||
{#if isLoading}
|
||||
<div class="loading">Loading tokens...</div>
|
||||
{:else if getFilteredTokens().length === 0}
|
||||
<div class="no-results">No tokens found</div>
|
||||
{:else}
|
||||
{#each getFilteredTokens() as token}
|
||||
<button
|
||||
class="token-option"
|
||||
class:selected={token.symbol === selectedToken && token.chain === selectedChain}
|
||||
onclick={() => handleSelect(token)}
|
||||
>
|
||||
<span class="token-symbol">{token.symbol}</span>
|
||||
<span class="token-chain">{token.chain.toUpperCase()}</span>
|
||||
<span class="token-name">{token.name}</span>
|
||||
</button>
|
||||
{/each}
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.token-picker {
|
||||
position: relative;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
label {
|
||||
font-size: 0.9rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.input-wrapper {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
input {
|
||||
width: 100%;
|
||||
padding: 0.75rem 2.5rem 0.75rem 1rem;
|
||||
border-radius: 8px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: #fff;
|
||||
font-size: 1rem;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
input:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
input.has-value {
|
||||
border-color: rgba(102, 126, 234, 0.5);
|
||||
}
|
||||
|
||||
input:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.clear-btn {
|
||||
position: absolute;
|
||||
right: 0.75rem;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
background: none;
|
||||
border: none;
|
||||
color: #888;
|
||||
cursor: pointer;
|
||||
padding: 0.25rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.clear-btn:hover:not(:disabled) {
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.dropdown {
|
||||
position: absolute;
|
||||
top: 100%;
|
||||
left: 0;
|
||||
right: 0;
|
||||
margin-top: 0.5rem;
|
||||
background: #1a1a1a;
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 8px;
|
||||
max-height: 250px;
|
||||
overflow-y: auto;
|
||||
z-index: 50;
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
|
||||
.loading,
|
||||
.no-results {
|
||||
padding: 1rem;
|
||||
text-align: center;
|
||||
color: #888;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.token-option {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
width: 100%;
|
||||
padding: 0.75rem 1rem;
|
||||
background: none;
|
||||
border: none;
|
||||
color: #fff;
|
||||
text-align: left;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.token-option:hover {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
.token-option.selected {
|
||||
background: rgba(102, 126, 234, 0.2);
|
||||
}
|
||||
|
||||
.token-symbol {
|
||||
font-weight: 600;
|
||||
min-width: 60px;
|
||||
}
|
||||
|
||||
.token-chain {
|
||||
font-size: 0.75rem;
|
||||
padding: 0.15rem 0.4rem;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border-radius: 4px;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.token-name {
|
||||
flex: 1;
|
||||
color: #888;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
</style>
|
||||
180
src/frontend/src/lib/components/TradeDashboard.svelte
Normal file
180
src/frontend/src/lib/components/TradeDashboard.svelte
Normal file
@@ -0,0 +1,180 @@
|
||||
<script lang="ts">
|
||||
import type { TradeLogEntry } from '$lib/stores/simulationStore';
|
||||
|
||||
interface Props {
|
||||
tradeLog: TradeLogEntry[];
|
||||
}
|
||||
|
||||
let { tradeLog }: Props = $props();
|
||||
|
||||
function formatTime(timestamp: number): string {
|
||||
const date = new Date(timestamp * 1000);
|
||||
return date.toLocaleString();
|
||||
}
|
||||
|
||||
function getActionColor(action: string): string {
|
||||
switch (action) {
|
||||
case 'buy': return '#22c55e';
|
||||
case 'sell': return '#ef4444';
|
||||
default: return '#666';
|
||||
}
|
||||
}
|
||||
|
||||
function getActionIcon(action: string): string {
|
||||
switch (action) {
|
||||
case 'buy': return '📈';
|
||||
case 'sell': return '📉';
|
||||
default: return '➡️';
|
||||
}
|
||||
}
|
||||
|
||||
// Filter to show only buy/sell actions
|
||||
let tradeActions = $derived(tradeLog.filter(t => t.action !== 'hold'));
|
||||
</script>
|
||||
|
||||
<div class="trade-dashboard">
|
||||
<div class="dashboard-header">
|
||||
<h3>Trade Activity</h3>
|
||||
<span class="trade-count">
|
||||
{tradeActions.length} trades
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{#if tradeActions.length === 0}
|
||||
<div class="empty-state">
|
||||
<p>No trades executed yet. Check the strategy configuration.</p>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="trade-list">
|
||||
{#each tradeActions as entry}
|
||||
<div class="trade-entry action-{entry.action}">
|
||||
<div class="trade-time">
|
||||
<span class="action-icon">{getActionIcon(entry.action)}</span>
|
||||
<span class="action-badge" style="background: {getActionColor(entry.action)}">
|
||||
{entry.action.toUpperCase()}
|
||||
</span>
|
||||
<span class="time">{formatTime(entry.time)}</span>
|
||||
</div>
|
||||
<div class="trade-details">
|
||||
<div class="price">
|
||||
<span class="label">Price:</span>
|
||||
<span class="value">${entry.price.toFixed(8)}</span>
|
||||
</div>
|
||||
<div class="reason">
|
||||
<span class="label">Reason:</span>
|
||||
<span class="value">{entry.reason}</span>
|
||||
</div>
|
||||
{#if entry.action === 'sell' && entry.position > 0}
|
||||
<div class="pnl">
|
||||
<span class="label">Position:</span>
|
||||
<span class="value">{entry.position.toFixed(6)}</span>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.trade-dashboard {
|
||||
background: rgba(255, 255, 255, 0.02);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.dashboard-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 1rem;
|
||||
background: rgba(255, 255, 255, 0.02);
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
.dashboard-header h3 {
|
||||
margin: 0;
|
||||
font-size: 1rem;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.trade-count {
|
||||
font-size: 0.85rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
padding: 2rem;
|
||||
text-align: center;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.trade-list {
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.trade-entry {
|
||||
padding: 0.75rem 1rem;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.05);
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.trade-entry:hover {
|
||||
background: rgba(255, 255, 255, 0.02);
|
||||
}
|
||||
|
||||
.trade-entry.action-buy {
|
||||
border-left: 3px solid #22c55e;
|
||||
}
|
||||
|
||||
.trade-entry.action-sell {
|
||||
border-left: 3px solid #ef4444;
|
||||
}
|
||||
|
||||
.trade-time {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.action-icon {
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.action-badge {
|
||||
padding: 0.125rem 0.5rem;
|
||||
border-radius: 4px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: bold;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.time {
|
||||
font-size: 0.85rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.trade-details {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
||||
gap: 0.5rem;
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.trade-details .label {
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.trade-details .value {
|
||||
color: #fff;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.pnl .value {
|
||||
color: #fbbf24;
|
||||
}
|
||||
</style>
|
||||
19
src/frontend/src/lib/components/index.ts
Normal file
19
src/frontend/src/lib/components/index.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
export { default as ChatInterface } from './ChatInterface.svelte';
|
||||
export { default as BotCard } from './BotCard.svelte';
|
||||
export { default as BotSelector } from './BotSelector.svelte';
|
||||
export { default as StrategyPreview } from './StrategyPreview.svelte';
|
||||
export { default as SignalChart } from './SignalChart.svelte';
|
||||
export { default as TradeDashboard } from './TradeDashboard.svelte';
|
||||
export { default as PortfolioSummary } from './PortfolioSummary.svelte';
|
||||
export { default as BacktestChart } from './BacktestChart.svelte';
|
||||
export { default as ProUpgradeBanner } from './ProUpgradeBanner.svelte';
|
||||
export { default as TokenPicker } from './TokenPicker.svelte';
|
||||
export { default as ConditionBuilder } from './ConditionBuilder.svelte';
|
||||
export { default as ChatLayout } from './ChatLayout.svelte';
|
||||
export { default as ConversationList } from './ConversationList.svelte';
|
||||
export { default as ChatArea } from './ChatArea.svelte';
|
||||
export { default as ChatInput } from './ChatInput.svelte';
|
||||
export { default as BotInfoPanel } from './BotInfoPanel.svelte';
|
||||
export { default as AnonymousBanner } from './AnonymousBanner.svelte';
|
||||
export { default as CandlestickLoader } from './CandlestickLoader.svelte';
|
||||
export { default as AppHeader } from './AppHeader.svelte';
|
||||
1
src/frontend/src/lib/index.ts
Normal file
1
src/frontend/src/lib/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
// place files you want to import through the `$lib` alias in this folder.
|
||||
50
src/frontend/src/lib/stores/authStore.ts
Normal file
50
src/frontend/src/lib/stores/authStore.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { writable, get } from 'svelte/store';
|
||||
import { api } from '$lib/api';
|
||||
import { setUser, clearUser, clearBots } from './index';
|
||||
import { clearSimulationState } from './simulationStore';
|
||||
import { clearBacktestState } from './backtestStore';
|
||||
|
||||
export const isAuthenticated = writable(false);
|
||||
export const isLoading = writable(true);
|
||||
|
||||
export async function initAuth() {
|
||||
isLoading.set(true);
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
try {
|
||||
const user = await api.auth.me();
|
||||
setUser(user);
|
||||
isAuthenticated.set(true);
|
||||
} catch {
|
||||
localStorage.removeItem('token');
|
||||
isAuthenticated.set(false);
|
||||
}
|
||||
}
|
||||
isLoading.set(false);
|
||||
}
|
||||
|
||||
export async function login(email: string, password: string) {
|
||||
const response = await api.auth.login(email, password);
|
||||
localStorage.setItem('token', response.access_token);
|
||||
const user = await api.auth.me();
|
||||
setUser(user);
|
||||
isAuthenticated.set(true);
|
||||
}
|
||||
|
||||
export async function register(email: string, password: string) {
|
||||
const response = await api.auth.register(email, password);
|
||||
localStorage.setItem('token', response.access_token);
|
||||
const user = await api.auth.me();
|
||||
setUser(user);
|
||||
isAuthenticated.set(true);
|
||||
}
|
||||
|
||||
export function logout() {
|
||||
api.auth.logout().catch(() => {});
|
||||
localStorage.removeItem('token');
|
||||
clearUser();
|
||||
clearBots();
|
||||
clearBacktestState();
|
||||
clearSimulationState();
|
||||
isAuthenticated.set(false);
|
||||
}
|
||||
45
src/frontend/src/lib/stores/backtestStore.ts
Normal file
45
src/frontend/src/lib/stores/backtestStore.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { writable } from 'svelte/store';
|
||||
import type { Backtest, BacktestResult } from '$lib/api';
|
||||
|
||||
export interface BacktestState {
|
||||
currentBacktest: Backtest | null;
|
||||
backtestHistory: Backtest[];
|
||||
isLoading: boolean;
|
||||
error: string | null;
|
||||
}
|
||||
|
||||
const initialState: BacktestState = {
|
||||
currentBacktest: null,
|
||||
backtestHistory: [],
|
||||
isLoading: false,
|
||||
error: null
|
||||
};
|
||||
|
||||
export const backtestStore = writable<BacktestState>(initialState);
|
||||
|
||||
export function setCurrentBacktest(backtest: Backtest | null) {
|
||||
backtestStore.update(state => ({ ...state, currentBacktest: backtest }));
|
||||
}
|
||||
|
||||
export function addBacktestToHistory(backtest: Backtest) {
|
||||
backtestStore.update(state => ({
|
||||
...state,
|
||||
backtestHistory: [backtest, ...state.backtestHistory]
|
||||
}));
|
||||
}
|
||||
|
||||
export function setBacktestHistory(backtests: Backtest[]) {
|
||||
backtestStore.update(state => ({ ...state, backtestHistory: backtests }));
|
||||
}
|
||||
|
||||
export function setBacktestLoading(loading: boolean) {
|
||||
backtestStore.update(state => ({ ...state, isLoading: loading }));
|
||||
}
|
||||
|
||||
export function setBacktestError(error: string | null) {
|
||||
backtestStore.update(state => ({ ...state, error }));
|
||||
}
|
||||
|
||||
export function clearBacktestState() {
|
||||
backtestStore.set(initialState);
|
||||
}
|
||||
24
src/frontend/src/lib/stores/botsStore.ts
Normal file
24
src/frontend/src/lib/stores/botsStore.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { writable } from 'svelte/store';
|
||||
import type { Bot } from '$lib/api';
|
||||
|
||||
export const botsStore = writable<Bot[]>([]);
|
||||
|
||||
export function setBots(bots: Bot[]) {
|
||||
botsStore.set(bots);
|
||||
}
|
||||
|
||||
export function addBot(bot: Bot) {
|
||||
botsStore.update(bots => [...bots, bot]);
|
||||
}
|
||||
|
||||
export function updateBot(bot: Bot) {
|
||||
botsStore.update(bots => bots.map(b => b.id === bot.id ? bot : b));
|
||||
}
|
||||
|
||||
export function removeBot(botId: string) {
|
||||
botsStore.update(bots => bots.filter(b => b.id !== botId));
|
||||
}
|
||||
|
||||
export function clearBots() {
|
||||
botsStore.set([]);
|
||||
}
|
||||
48
src/frontend/src/lib/stores/chatStore.ts
Normal file
48
src/frontend/src/lib/stores/chatStore.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { writable } from 'svelte/store';
|
||||
import type { BotConversation } from '$lib/api';
|
||||
|
||||
export interface ChatMessage {
|
||||
id: string;
|
||||
role: 'user' | 'assistant' | 'system';
|
||||
content: string;
|
||||
thinking: string | null;
|
||||
timestamp: Date;
|
||||
}
|
||||
|
||||
// Fallback UUID generator for environments where crypto.randomUUID is not available
|
||||
export function generateId(): string {
|
||||
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
// Fallback: simple UUID v4 implementation
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
||||
const r = (Math.random() * 16) | 0;
|
||||
const v = c === 'x' ? r : (r & 0x3) | 0x8;
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
|
||||
export const chatStore = writable<ChatMessage[]>([]);
|
||||
|
||||
export function addMessage(message: Omit<ChatMessage, 'id' | 'timestamp'>) {
|
||||
const newMessage: ChatMessage = {
|
||||
...message,
|
||||
id: generateId(),
|
||||
timestamp: new Date()
|
||||
};
|
||||
chatStore.update(messages => [...messages, newMessage]);
|
||||
}
|
||||
|
||||
export function setMessages(messages: BotConversation[]) {
|
||||
chatStore.set(messages.map(m => ({
|
||||
id: m.id,
|
||||
role: m.role,
|
||||
content: m.content,
|
||||
thinking: null,
|
||||
timestamp: new Date(m.created_at)
|
||||
})));
|
||||
}
|
||||
|
||||
export function clearChat() {
|
||||
chatStore.set([]);
|
||||
}
|
||||
96
src/frontend/src/lib/stores/conversationStore.ts
Normal file
96
src/frontend/src/lib/stores/conversationStore.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { writable, derived } from 'svelte/store';
|
||||
import type { Conversation, Message } from '$lib/api';
|
||||
|
||||
export interface ConversationState {
|
||||
conversations: Conversation[];
|
||||
currentConversationId: string | null;
|
||||
messages: Message[];
|
||||
isLoading: boolean;
|
||||
error: string | null;
|
||||
anonymousChatCount: number;
|
||||
}
|
||||
|
||||
const initialState: ConversationState = {
|
||||
conversations: [],
|
||||
currentConversationId: null,
|
||||
messages: [],
|
||||
isLoading: false,
|
||||
error: null,
|
||||
anonymousChatCount: 0
|
||||
};
|
||||
|
||||
export const conversationStore = writable<ConversationState>(initialState);
|
||||
|
||||
export function setConversations(conversations: Conversation[]) {
|
||||
conversationStore.update(state => ({ ...state, conversations }));
|
||||
}
|
||||
|
||||
export function addConversation(conversation: Conversation) {
|
||||
conversationStore.update(state => ({
|
||||
...state,
|
||||
conversations: [conversation, ...state.conversations]
|
||||
}));
|
||||
}
|
||||
|
||||
export function removeConversation(conversationId: string) {
|
||||
conversationStore.update(state => ({
|
||||
...state,
|
||||
conversations: state.conversations.filter(c => c.id !== conversationId),
|
||||
currentConversationId: state.currentConversationId === conversationId ? null : state.currentConversationId
|
||||
}));
|
||||
}
|
||||
|
||||
export function setCurrentConversation(conversationId: string | null) {
|
||||
conversationStore.update(state => ({
|
||||
...state,
|
||||
currentConversationId: conversationId,
|
||||
messages: []
|
||||
}));
|
||||
}
|
||||
|
||||
export function setMessages(messages: Message[]) {
|
||||
conversationStore.update(state => ({ ...state, messages }));
|
||||
}
|
||||
|
||||
export function addMessage(message: Message) {
|
||||
conversationStore.update(state => ({
|
||||
...state,
|
||||
messages: [...state.messages, message]
|
||||
}));
|
||||
}
|
||||
|
||||
export function setLoading(isLoading: boolean) {
|
||||
conversationStore.update(state => ({ ...state, isLoading }));
|
||||
}
|
||||
|
||||
export function setError(error: string | null) {
|
||||
conversationStore.update(state => ({ ...state, error }));
|
||||
}
|
||||
|
||||
export function incrementAnonymousChatCount() {
|
||||
conversationStore.update(state => ({
|
||||
...state,
|
||||
anonymousChatCount: state.anonymousChatCount + 1
|
||||
}));
|
||||
}
|
||||
|
||||
export function resetAnonymousChatCount() {
|
||||
conversationStore.update(state => ({
|
||||
...state,
|
||||
anonymousChatCount: 0
|
||||
}));
|
||||
}
|
||||
|
||||
export function updateConversationTitle(conversationId: string, title: string) {
|
||||
conversationStore.update(state => ({
|
||||
...state,
|
||||
conversations: state.conversations.map(c =>
|
||||
c.id === conversationId ? { ...c, title, updated_at: new Date().toISOString() } : c
|
||||
)
|
||||
}));
|
||||
}
|
||||
|
||||
export const currentConversation = derived(
|
||||
conversationStore,
|
||||
$state => $state.conversations.find(c => c.id === $state.currentConversationId) ?? null
|
||||
);
|
||||
12
src/frontend/src/lib/stores/currentBotStore.ts
Normal file
12
src/frontend/src/lib/stores/currentBotStore.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { writable } from 'svelte/store';
|
||||
import type { Bot } from '$lib/api';
|
||||
|
||||
export const currentBotStore = writable<Bot | null>(null);
|
||||
|
||||
export function setCurrentBot(bot: Bot | null) {
|
||||
currentBotStore.set(bot);
|
||||
}
|
||||
|
||||
export function clearCurrentBot() {
|
||||
currentBotStore.set(null);
|
||||
}
|
||||
45
src/frontend/src/lib/stores/index.ts
Normal file
45
src/frontend/src/lib/stores/index.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
export { userStore, setUser, clearUser } from './userStore';
|
||||
export { botsStore, setBots, addBot, updateBot, removeBot, clearBots } from './botsStore';
|
||||
export { currentBotStore, setCurrentBot, clearCurrentBot } from './currentBotStore';
|
||||
export { chatStore, addMessage, setMessages, clearChat } from './chatStore';
|
||||
export {
|
||||
backtestStore,
|
||||
setCurrentBacktest,
|
||||
addBacktestToHistory,
|
||||
setBacktestHistory,
|
||||
setBacktestLoading,
|
||||
setBacktestError,
|
||||
clearBacktestState
|
||||
} from './backtestStore';
|
||||
export {
|
||||
simulationStore,
|
||||
setCurrentSimulation,
|
||||
addSignals,
|
||||
clearSignals,
|
||||
setSimulationLoading,
|
||||
setSimulationError,
|
||||
clearSimulationState
|
||||
} from './simulationStore';
|
||||
export {
|
||||
isAuthenticated,
|
||||
isLoading,
|
||||
initAuth,
|
||||
login,
|
||||
register,
|
||||
logout
|
||||
} from './authStore';
|
||||
export {
|
||||
conversationStore,
|
||||
setConversations,
|
||||
addConversation,
|
||||
removeConversation,
|
||||
setCurrentConversation,
|
||||
setMessages as setConversationMessages,
|
||||
addMessage as addConversationMessage,
|
||||
setLoading as setConversationLoading,
|
||||
setError as setConversationError,
|
||||
incrementAnonymousChatCount,
|
||||
resetAnonymousChatCount,
|
||||
updateConversationTitle,
|
||||
currentConversation
|
||||
} from './conversationStore';
|
||||
94
src/frontend/src/lib/stores/simulationStore.ts
Normal file
94
src/frontend/src/lib/stores/simulationStore.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { writable } from 'svelte/store';
|
||||
import type { Simulation, Signal } from '$lib/api';
|
||||
|
||||
export interface KlineData {
|
||||
time: number;
|
||||
close: number;
|
||||
}
|
||||
|
||||
export interface TradeLogEntry {
|
||||
time: number;
|
||||
price: number;
|
||||
action: 'buy' | 'sell' | 'hold';
|
||||
reason: string;
|
||||
position: number;
|
||||
entry_price: number | null;
|
||||
}
|
||||
|
||||
export interface Portfolio {
|
||||
initial_balance: number;
|
||||
current_balance: number;
|
||||
position: number;
|
||||
position_token: string;
|
||||
entry_price: number;
|
||||
current_price: number;
|
||||
}
|
||||
|
||||
export interface SimulationState {
|
||||
currentSimulation: Simulation | null;
|
||||
signals: Signal[];
|
||||
klines: KlineData[];
|
||||
tradeLog: TradeLogEntry[];
|
||||
portfolio: Portfolio;
|
||||
isLoading: boolean;
|
||||
error: string | null;
|
||||
}
|
||||
|
||||
const initialState: SimulationState = {
|
||||
currentSimulation: null,
|
||||
signals: [],
|
||||
klines: [],
|
||||
tradeLog: [],
|
||||
portfolio: {
|
||||
initial_balance: 10000,
|
||||
current_balance: 10000,
|
||||
position: 0,
|
||||
position_token: '',
|
||||
entry_price: 0,
|
||||
current_price: 0
|
||||
},
|
||||
isLoading: false,
|
||||
error: null
|
||||
};
|
||||
|
||||
export const simulationStore = writable<SimulationState>(initialState);
|
||||
|
||||
export function setCurrentSimulation(simulation: Simulation | null) {
|
||||
simulationStore.update(state => ({
|
||||
...state,
|
||||
currentSimulation: simulation,
|
||||
klines: simulation?.klines || [],
|
||||
tradeLog: simulation?.trade_log || [],
|
||||
portfolio: simulation?.portfolio || state.portfolio
|
||||
}));
|
||||
}
|
||||
|
||||
export function updatePortfolio(portfolio: Partial<Portfolio>) {
|
||||
simulationStore.update(state => ({
|
||||
...state,
|
||||
portfolio: { ...state.portfolio, ...portfolio }
|
||||
}));
|
||||
}
|
||||
|
||||
export function addSignals(newSignals: Signal[]) {
|
||||
simulationStore.update(state => ({
|
||||
...state,
|
||||
signals: [...state.signals, ...newSignals]
|
||||
}));
|
||||
}
|
||||
|
||||
export function clearSignals() {
|
||||
simulationStore.update(state => ({ ...state, signals: [] }));
|
||||
}
|
||||
|
||||
export function setSimulationLoading(loading: boolean) {
|
||||
simulationStore.update(state => ({ ...state, isLoading: loading }));
|
||||
}
|
||||
|
||||
export function setSimulationError(error: string | null) {
|
||||
simulationStore.update(state => ({ ...state, error }));
|
||||
}
|
||||
|
||||
export function clearSimulationState() {
|
||||
simulationStore.set(initialState);
|
||||
}
|
||||
12
src/frontend/src/lib/stores/userStore.ts
Normal file
12
src/frontend/src/lib/stores/userStore.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { writable } from 'svelte/store';
|
||||
import type { User } from '$lib/api';
|
||||
|
||||
export const userStore = writable<User | null>(null);
|
||||
|
||||
export function setUser(user: User | null) {
|
||||
userStore.set(user);
|
||||
}
|
||||
|
||||
export function clearUser() {
|
||||
userStore.set(null);
|
||||
}
|
||||
256
src/frontend/src/lib/utils/markdown.ts
Normal file
256
src/frontend/src/lib/utils/markdown.ts
Normal file
@@ -0,0 +1,256 @@
|
||||
/**
|
||||
* Simple markdown parser for rendering AI responses
|
||||
* Supports: bold, italic, code blocks, inline code, links, lists, tables, headings, line breaks
|
||||
*/
|
||||
|
||||
export interface InlineSegment {
|
||||
type: 'text' | 'bold' | 'italic' | 'code' | 'link';
|
||||
content: string;
|
||||
href?: string;
|
||||
}
|
||||
|
||||
export interface ParsedSegment {
|
||||
type: 'text' | 'bold' | 'italic' | 'code' | 'codeBlock' | 'link' | 'list' | 'table' | 'lineBreak' | 'heading';
|
||||
content: string;
|
||||
items?: string[];
|
||||
headers?: InlineSegment[][];
|
||||
rows?: InlineSegment[][][];
|
||||
}
|
||||
|
||||
export function parseMarkdown(text: string): ParsedSegment[] {
|
||||
const segments: ParsedSegment[] = [];
|
||||
|
||||
// Normalize line endings
|
||||
text = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
||||
|
||||
// First, extract code blocks
|
||||
const codeBlockRegex = /```[\s\S]*?```/g;
|
||||
const parts = text.split(codeBlockRegex);
|
||||
const codeBlocks = text.match(codeBlockRegex) || [];
|
||||
|
||||
let partIndex = 0;
|
||||
|
||||
while (partIndex < parts.length) {
|
||||
const part = parts[partIndex];
|
||||
|
||||
if (part.trim()) {
|
||||
// Process non-code content
|
||||
segments.push(...parseInlineContent(part));
|
||||
}
|
||||
|
||||
// Add code block if there's one after this part
|
||||
if (partIndex < codeBlocks.length) {
|
||||
const codeContent = codeBlocks[partIndex].replace(/^```\w*\n?/, '').replace(/```$/, '');
|
||||
segments.push({ type: 'codeBlock', content: codeContent });
|
||||
}
|
||||
|
||||
partIndex++;
|
||||
}
|
||||
|
||||
return segments;
|
||||
}
|
||||
|
||||
function parseInlineContent(text: string): ParsedSegment[] {
|
||||
const segments: ParsedSegment[] = [];
|
||||
|
||||
// Check for tables - match table pattern anywhere in text
|
||||
// Table pattern: | header | ... |\n|---|...|\n| row | ... |
|
||||
const tableRegex = /\|.+\|\n\|[-:\s|]+\|\n((?:\|.+\|\n?)*)/g;
|
||||
let lastIndex = 0;
|
||||
let tableMatch;
|
||||
|
||||
while ((tableMatch = tableRegex.exec(text)) !== null) {
|
||||
// Add content before table
|
||||
const beforeTable = text.substring(lastIndex, tableMatch.index);
|
||||
if (beforeTable.trim()) {
|
||||
segments.push(...parseLines(beforeTable));
|
||||
}
|
||||
|
||||
// Parse table
|
||||
const tableContent = tableMatch[0];
|
||||
const tableSegments = parseTable(tableContent);
|
||||
if (tableSegments.length > 0) {
|
||||
segments.push(...tableSegments);
|
||||
} else {
|
||||
// If table parsing failed, treat as text
|
||||
segments.push(...parseLines(tableContent));
|
||||
}
|
||||
|
||||
lastIndex = tableMatch.index + tableContent.length;
|
||||
}
|
||||
|
||||
// Add remaining content
|
||||
if (lastIndex < text.length) {
|
||||
const remaining = text.substring(lastIndex);
|
||||
if (remaining.trim()) {
|
||||
segments.push(...parseLines(remaining));
|
||||
}
|
||||
}
|
||||
|
||||
return segments;
|
||||
}
|
||||
|
||||
function parseTable(tableStr: string): ParsedSegment[] {
|
||||
const lines = tableStr.trim().split('\n').filter(line => line.trim());
|
||||
if (lines.length < 2) return [];
|
||||
|
||||
// Skip separator line (|---|---|)
|
||||
const dataLines = lines.filter(line => !line.match(/^[\|\s\-:]+$/));
|
||||
if (dataLines.length < 2) return [];
|
||||
|
||||
const headers = parseTableRow(dataLines[0]);
|
||||
const rows = dataLines.slice(1).map(row => parseTableRow(row));
|
||||
|
||||
return [{
|
||||
type: 'table',
|
||||
content: '',
|
||||
headers,
|
||||
rows
|
||||
}];
|
||||
}
|
||||
|
||||
function parseTableRow(row: string): InlineSegment[][] {
|
||||
return row.split('|')
|
||||
.map(cell => cell.trim())
|
||||
.filter(cell => cell !== '')
|
||||
.map(cell => parseInlineElements(cell));
|
||||
}
|
||||
|
||||
export function parseInlineElements(text: string): InlineSegment[] {
|
||||
const segments: InlineSegment[] = [];
|
||||
|
||||
const inlineRegex = /(\*\*[^*]+\*\*|\*[^*]+\*|`[^`]+`|\[.*?\]\(.*?\))/g;
|
||||
const parts = text.split(inlineRegex);
|
||||
|
||||
for (const part of parts) {
|
||||
if (!part) continue;
|
||||
|
||||
if (part.startsWith('**') && part.endsWith('**')) {
|
||||
segments.push({ type: 'bold', content: part.slice(2, -2) });
|
||||
} else if (part.startsWith('*') && part.endsWith('*')) {
|
||||
segments.push({ type: 'italic', content: part.slice(1, -1) });
|
||||
} else if (part.startsWith('`') && part.endsWith('`')) {
|
||||
segments.push({ type: 'code', content: part.slice(1, -1) });
|
||||
} else if (part.startsWith('[') && part.includes('](')) {
|
||||
const linkMatch = part.match(/\[(.*?)\]\((.*?)\)/);
|
||||
if (linkMatch) {
|
||||
segments.push({ type: 'link', content: linkMatch[1], href: linkMatch[2] });
|
||||
}
|
||||
} else if (part) {
|
||||
segments.push({ type: 'text', content: part });
|
||||
}
|
||||
}
|
||||
|
||||
return segments;
|
||||
}
|
||||
|
||||
// Render inline segments to HTML string
|
||||
function renderInlineSegments(segments: InlineSegment[]): string {
|
||||
return segments.map(seg => {
|
||||
switch (seg.type) {
|
||||
case 'bold': return `<strong>${seg.content}</strong>`;
|
||||
case 'italic': return `<em>${seg.content}</em>`;
|
||||
case 'code': return `<code class="inline-code">${seg.content}</code>`;
|
||||
case 'link': return `<a href="${seg.href || '#'}" target="_blank" rel="noopener noreferrer">${seg.content}</a>`;
|
||||
default: return seg.content;
|
||||
}
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function parseLines(text: string): ParsedSegment[] {
|
||||
const segments: ParsedSegment[] = [];
|
||||
|
||||
// Combined regex for inline formatting
|
||||
const inlineRegex = /(\*\*[^*]+\*\*|\*[^*]+\*|`[^`]+`|\[.*?\]\(.*?\))/g;
|
||||
const lines = text.split('\n');
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
|
||||
if (!line.trim()) {
|
||||
// Empty line - add line break for paragraph separation
|
||||
segments.push({ type: 'lineBreak', content: '' });
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for headings
|
||||
if (line.match(/^#{1,6}\s/)) {
|
||||
segments.push({ type: 'heading', content: line.replace(/^#+\s/, '') });
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for list items
|
||||
if (line.match(/^[\-\*]\s/)) {
|
||||
const listMatch = line.match(/^([\-\*])\s(.*)/);
|
||||
if (listMatch) {
|
||||
// Parse inline formatting for list item
|
||||
const itemContent = listMatch[2];
|
||||
const inlineSegments = parseInlineElements(itemContent);
|
||||
|
||||
// Check if previous segment is a list
|
||||
const lastSeg = segments[segments.length - 1];
|
||||
if (lastSeg && lastSeg.type === 'list') {
|
||||
lastSeg.items?.push(itemContent);
|
||||
} else {
|
||||
segments.push({ type: 'list', content: '', items: [itemContent] });
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for numbered lists
|
||||
if (line.match(/^\d+\.\s/)) {
|
||||
const listMatch = line.match(/^\d+\.\s(.*)/);
|
||||
if (listMatch) {
|
||||
const itemContent = listMatch[1];
|
||||
|
||||
const lastSeg = segments[segments.length - 1];
|
||||
if (lastSeg && lastSeg.type === 'list') {
|
||||
lastSeg.items?.push(itemContent);
|
||||
} else {
|
||||
segments.push({ type: 'list', content: '', items: [itemContent] });
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Process inline formatting
|
||||
const inlineSegments = parseInlineElementsAsText(line);
|
||||
segments.push(...inlineSegments);
|
||||
|
||||
// Add line break after non-empty lines (except last in a paragraph)
|
||||
if (i < lines.length - 1 && line.trim()) {
|
||||
segments.push({ type: 'lineBreak', content: '' });
|
||||
}
|
||||
}
|
||||
|
||||
return segments;
|
||||
}
|
||||
|
||||
function parseInlineElementsAsText(text: string): ParsedSegment[] {
|
||||
const segments: ParsedSegment[] = [];
|
||||
|
||||
const inlineRegex = /(\*\*[^*]+\*\*|\*[^*]+\*|`[^`]+`|\[.*?\]\(.*?\))/g;
|
||||
const parts = text.split(inlineRegex);
|
||||
|
||||
for (const part of parts) {
|
||||
if (!part) continue;
|
||||
|
||||
if (part.startsWith('**') && part.endsWith('**')) {
|
||||
segments.push({ type: 'bold', content: part.slice(2, -2) });
|
||||
} else if (part.startsWith('*') && part.endsWith('*')) {
|
||||
segments.push({ type: 'italic', content: part.slice(1, -1) });
|
||||
} else if (part.startsWith('`') && part.endsWith('`')) {
|
||||
segments.push({ type: 'code', content: part.slice(1, -1) });
|
||||
} else if (part.startsWith('[') && part.includes('](')) {
|
||||
const linkMatch = part.match(/\[(.*?)\]\((.*?)\)/);
|
||||
if (linkMatch) {
|
||||
segments.push({ type: 'link', content: linkMatch[1] });
|
||||
}
|
||||
} else if (part) {
|
||||
segments.push({ type: 'text', content: part });
|
||||
}
|
||||
}
|
||||
|
||||
return segments;
|
||||
}
|
||||
61
src/frontend/src/routes/+layout.svelte
Normal file
61
src/frontend/src/routes/+layout.svelte
Normal file
@@ -0,0 +1,61 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { initAuth, isLoading } from '$lib/stores';
|
||||
import favicon from '$lib/assets/favicon.svg';
|
||||
|
||||
let { children } = $props();
|
||||
|
||||
onMount(() => {
|
||||
initAuth();
|
||||
|
||||
// Reset anonymous counts on layout load (for debugging)
|
||||
const count = localStorage.getItem('anonymous_chat_count');
|
||||
if (count && parseInt(count) >= 50) {
|
||||
console.log('Resetting anonymous_chat_count from', count, 'to 0');
|
||||
localStorage.setItem('anonymous_chat_count', '0');
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<link rel="icon" href={favicon} />
|
||||
</svelte:head>
|
||||
|
||||
{#if $isLoading}
|
||||
<div class="loading">
|
||||
<div class="spinner"></div>
|
||||
</div>
|
||||
{:else}
|
||||
{@render children()}
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
:global(body) {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: #0f0f0f;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.loading {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.spinner {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border: 3px solid rgba(255, 255, 255, 0.1);
|
||||
border-top-color: #667eea;
|
||||
border-radius: 50%;
|
||||
animation: spin 1s linear infinite;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
134
src/frontend/src/routes/+page.svelte
Normal file
134
src/frontend/src/routes/+page.svelte
Normal file
@@ -0,0 +1,134 @@
|
||||
<script lang="ts">
|
||||
import { isAuthenticated } from '$lib/stores';
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Randebu - AI Trading Bot Platform</title>
|
||||
</svelte:head>
|
||||
|
||||
{#if $isAuthenticated}
|
||||
<script>
|
||||
window.location.href = '/dashboard';
|
||||
</script>
|
||||
{:else}
|
||||
<main>
|
||||
<div class="hero">
|
||||
<h1>Randebu</h1>
|
||||
<p class="tagline">Create trading bots through conversation with AI</p>
|
||||
<div class="cta">
|
||||
<a href="/chat" class="btn btn-primary">Try Now - Free</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<section class="features">
|
||||
<h2>How It Works</h2>
|
||||
<div class="feature-grid">
|
||||
<div class="feature">
|
||||
<h3>1. Describe Your Strategy</h3>
|
||||
<p>Tell our AI what kind of trading you want to do in plain English</p>
|
||||
</div>
|
||||
<div class="feature">
|
||||
<h3>2. Backtest & Validate</h3>
|
||||
<p>Test your strategy against historical data before risking real funds</p>
|
||||
</div>
|
||||
<div class="feature">
|
||||
<h3>3. Simulate & Monitor</h3>
|
||||
<p>Run real-time simulations and watch for trading signals</p>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
:global(body) {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: #0f0f0f;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
main {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.hero {
|
||||
text-align: center;
|
||||
max-width: 600px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 3.5rem;
|
||||
margin: 0;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
-webkit-background-clip: text;
|
||||
-webkit-text-fill-color: transparent;
|
||||
}
|
||||
|
||||
.tagline {
|
||||
font-size: 1.25rem;
|
||||
color: #aaa;
|
||||
margin: 1rem 0 2rem;
|
||||
}
|
||||
|
||||
.cta {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 0.75rem 1.5rem;
|
||||
border-radius: 8px;
|
||||
text-decoration: none;
|
||||
font-weight: 500;
|
||||
transition: transform 0.2s, opacity 0.2s;
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
color: white;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
.features {
|
||||
margin-top: 4rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.features h2 {
|
||||
font-size: 2rem;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.feature-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||||
gap: 2rem;
|
||||
max-width: 800px;
|
||||
}
|
||||
|
||||
.feature h3 {
|
||||
font-size: 1.25rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.feature p {
|
||||
color: #888;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
</style>
|
||||
446
src/frontend/src/routes/bot/[id]/+page.svelte
Normal file
446
src/frontend/src/routes/bot/[id]/+page.svelte
Normal file
@@ -0,0 +1,446 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { page } from '$app/stores';
|
||||
import { goto } from '$app/navigation';
|
||||
import { isAuthenticated, isLoading, chatStore, addMessage, setMessages, clearChat, currentBotStore, setCurrentBot } from '$lib/stores';
|
||||
import { api } from '$lib/api';
|
||||
import { ChatInterface, StrategyPreview } from '$lib/components';
|
||||
import type { TokenSearchResult } from '$lib/api';
|
||||
|
||||
let botId = $derived($page.params.id);
|
||||
let isSending = $state(false);
|
||||
let showStrategy = $state(false);
|
||||
|
||||
// Token address confirmation modal state
|
||||
let showTokenConfirm = $state(false);
|
||||
let pendingStrategyData = $state<any>(null);
|
||||
let tokenAddressInput = $state('');
|
||||
let confirmingMessage = $state('');
|
||||
let tokenSearchResults = $state<TokenSearchResult[]>([]);
|
||||
|
||||
onMount(async () => {
|
||||
if (!$isAuthenticated && !$isLoading) {
|
||||
goto('/login');
|
||||
return;
|
||||
}
|
||||
if ($isAuthenticated && botId) {
|
||||
await loadBot();
|
||||
await loadChatHistory();
|
||||
}
|
||||
});
|
||||
|
||||
async function loadBot() {
|
||||
try {
|
||||
const bot = await api.bots.get(botId);
|
||||
setCurrentBot(bot);
|
||||
} catch (e) {
|
||||
goto('/dashboard');
|
||||
}
|
||||
}
|
||||
|
||||
async function loadChatHistory() {
|
||||
try {
|
||||
const history = await api.bots.getHistory(botId);
|
||||
setMessages(history);
|
||||
} catch (e) {
|
||||
console.error('Failed to load chat history:', e);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSendMessage(message: string) {
|
||||
if (isSending) return;
|
||||
|
||||
isSending = true;
|
||||
|
||||
// Add user's message immediately so it shows even before API response
|
||||
addMessage({ role: 'user', content: message, thinking: null });
|
||||
|
||||
try {
|
||||
// Add timeout to prevent hanging requests
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 30000);
|
||||
|
||||
const response = await api.bots.chat(botId, message, controller.signal);
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
// Check if token address confirmation is needed
|
||||
if (response.strategy_needs_confirmation && response.strategy_data) {
|
||||
// Show token confirmation modal
|
||||
pendingStrategyData = response.strategy_data;
|
||||
confirmingMessage = response.response;
|
||||
tokenAddressInput = '';
|
||||
tokenSearchResults = response.token_search_results || [];
|
||||
showTokenConfirm = true;
|
||||
}
|
||||
|
||||
// Add assistant response with thinking
|
||||
addMessage({ role: 'assistant', content: response.response, thinking: response.thinking || null });
|
||||
|
||||
if (response.strategy_config) {
|
||||
const bot = await api.bots.get(botId);
|
||||
setCurrentBot(bot);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof Error && e.name === 'AbortError') {
|
||||
addMessage({ role: 'assistant', content: 'Request timed out. Please try again.', thinking: null });
|
||||
} else {
|
||||
addMessage({ role: 'assistant', content: 'Sorry, I encountered an error. Please try again.', thinking: null });
|
||||
}
|
||||
} finally {
|
||||
isSending = false;
|
||||
}
|
||||
}
|
||||
|
||||
function toggleStrategy() {
|
||||
showStrategy = !showStrategy;
|
||||
}
|
||||
|
||||
async function confirmTokenAddress() {
|
||||
if (!tokenAddressInput.trim() || !pendingStrategyData) {
|
||||
showTokenConfirm = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// Update the pending strategy with the token address
|
||||
const updatedStrategy = { ...pendingStrategyData };
|
||||
|
||||
// Update conditions with token address
|
||||
if (updatedStrategy.conditions) {
|
||||
updatedStrategy.conditions = updatedStrategy.conditions.map((cond: any) => ({
|
||||
...cond,
|
||||
token_address: tokenAddressInput.trim()
|
||||
}));
|
||||
}
|
||||
|
||||
// Update actions with token address
|
||||
if (updatedStrategy.actions) {
|
||||
updatedStrategy.actions = updatedStrategy.actions.map((action: any) => ({
|
||||
...action,
|
||||
token_address: tokenAddressInput.trim()
|
||||
}));
|
||||
}
|
||||
|
||||
try {
|
||||
// Update bot with the strategy
|
||||
await api.bots.update(botId, { strategy_config: updatedStrategy });
|
||||
|
||||
// Refresh bot data
|
||||
const bot = await api.bots.get(botId);
|
||||
setCurrentBot(bot);
|
||||
|
||||
// Add success message
|
||||
addMessage({ role: 'assistant', content: `Perfect! I've saved your strategy with the token address. You can now run backtests!`, thinking: null });
|
||||
} catch (e) {
|
||||
addMessage({ role: 'assistant', content: 'Failed to save strategy. Please try again.', thinking: null });
|
||||
}
|
||||
|
||||
showTokenConfirm = false;
|
||||
pendingStrategyData = null;
|
||||
tokenAddressInput = '';
|
||||
tokenSearchResults = [];
|
||||
}
|
||||
|
||||
function selectTokenResult(result: TokenSearchResult) {
|
||||
tokenAddressInput = result.address;
|
||||
}
|
||||
|
||||
function cancelTokenConfirm() {
|
||||
showTokenConfirm = false;
|
||||
pendingStrategyData = null;
|
||||
tokenAddressInput = '';
|
||||
tokenSearchResults = [];
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>{$currentBotStore?.name || 'Bot'} - Randebu</title>
|
||||
</svelte:head>
|
||||
|
||||
<main>
|
||||
{#if showTokenConfirm}
|
||||
<div class="modal-overlay" onclick={cancelTokenConfirm}>
|
||||
<div class="modal-content" onclick={(e) => e.stopPropagation()}>
|
||||
<h3>Select Token Address</h3>
|
||||
<p class="modal-message">{confirmingMessage}</p>
|
||||
|
||||
{#if tokenSearchResults.length > 0}
|
||||
<div class="token-results">
|
||||
<p class="modal-hint">Select a token:</p>
|
||||
{#each tokenSearchResults as result}
|
||||
<button class="token-result" onclick={() => selectTokenResult(result)}>
|
||||
<span class="token-symbol">{result.symbol}</span>
|
||||
<span class="token-name">{result.name}</span>
|
||||
<span class="token-address">{result.address.slice(0, 10)}...{result.address.slice(-8)}</span>
|
||||
</button>
|
||||
{/each}
|
||||
</div>
|
||||
<p class="modal-divider">or enter manually:</p>
|
||||
{/if}
|
||||
|
||||
<input type="text" class="token-input" bind:value={tokenAddressInput} placeholder="0x..."/>
|
||||
<div class="modal-actions">
|
||||
<button class="btn btn-secondary" onclick={cancelTokenConfirm}>Cancel</button>
|
||||
<button class="btn btn-primary" onclick={confirmTokenAddress} disabled={!tokenAddressInput.trim()}>Confirm</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
<header>
|
||||
<div class="header-left">
|
||||
<a href="/dashboard" class="back-link">← Dashboard</a>
|
||||
<h1>{$currentBotStore?.name || 'Loading...'}</h1>
|
||||
</div>
|
||||
<div class="header-actions">
|
||||
{#if $currentBotStore?.strategy_config}
|
||||
<button class="btn btn-secondary" onclick={toggleStrategy}>
|
||||
{showStrategy ? 'Hide' : 'Show'} Strategy
|
||||
</button>
|
||||
{/if}
|
||||
<a href="/bot/{botId}/backtest" class="btn btn-secondary">Backtest</a>
|
||||
<a href="/bot/{botId}/simulate" class="btn btn-secondary">Simulate</a>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
{#if showStrategy && $currentBotStore?.strategy_config}
|
||||
<div class="strategy-panel">
|
||||
<StrategyPreview config={$currentBotStore.strategy_config} />
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<div class="chat-wrapper">
|
||||
<ChatInterface
|
||||
bot={$currentBotStore}
|
||||
messages={$chatStore}
|
||||
isSending={isSending}
|
||||
onSendMessage={handleSendMessage}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- <ProUpgradeBanner feature="Auto-execute trades with your bot" /> -->
|
||||
</main>
|
||||
|
||||
<style>
|
||||
:global(body) {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: #0f0f0f;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
main {
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
max-width: 900px;
|
||||
margin: 0 auto;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding-bottom: 1rem;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.header-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.back-link {
|
||||
color: #667eea;
|
||||
text-decoration: none;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.back-link:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin: 0;
|
||||
font-size: 1.5rem;
|
||||
}
|
||||
|
||||
.header-actions {
|
||||
display: flex;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 0.5rem 1rem;
|
||||
border-radius: 8px;
|
||||
text-decoration: none;
|
||||
font-weight: 500;
|
||||
font-size: 0.9rem;
|
||||
cursor: pointer;
|
||||
border: none;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
color: white;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.strategy-panel {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.chat-wrapper {
|
||||
flex: 1;
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
/* Modal Styles */
|
||||
.modal-overlay {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background: rgba(0, 0, 0, 0.7);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.modal-content {
|
||||
background: rgba(20, 20, 20, 0.95);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 16px;
|
||||
padding: 1.5rem;
|
||||
max-width: 450px;
|
||||
width: 90%;
|
||||
}
|
||||
|
||||
.modal-content h3 {
|
||||
margin: 0 0 1rem;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.modal-message {
|
||||
color: #ccc;
|
||||
margin-bottom: 0.5rem;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.modal-hint {
|
||||
color: #888;
|
||||
font-size: 0.9rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.token-input {
|
||||
width: 100%;
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: #fff;
|
||||
font-size: 1rem;
|
||||
font-family: 'Monaco', 'Menlo', monospace;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.token-input:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.modal-actions {
|
||||
display: flex;
|
||||
gap: 0.75rem;
|
||||
margin-top: 1rem;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
padding: 0.75rem 1.5rem;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 1rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.btn-primary:hover:not(:disabled) {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.btn-primary:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
/* Token Results */
|
||||
.token-results {
|
||||
max-height: 200px;
|
||||
overflow-y: auto;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.token-result {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
padding: 0.75rem;
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 8px;
|
||||
margin-bottom: 0.5rem;
|
||||
cursor: pointer;
|
||||
text-align: left;
|
||||
color: #fff;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.token-result:hover {
|
||||
background: rgba(102, 126, 234, 0.2);
|
||||
border-color: rgba(102, 126, 234, 0.5);
|
||||
}
|
||||
|
||||
.token-result:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.token-symbol {
|
||||
font-weight: 600;
|
||||
color: #667eea;
|
||||
min-width: 60px;
|
||||
}
|
||||
|
||||
.token-name {
|
||||
flex: 1;
|
||||
color: #ccc;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.token-address {
|
||||
font-size: 0.75rem;
|
||||
color: #666;
|
||||
font-family: 'Monaco', 'Menlo', monospace;
|
||||
}
|
||||
|
||||
.modal-divider {
|
||||
text-align: center;
|
||||
color: #666;
|
||||
font-size: 0.85rem;
|
||||
margin: 1rem 0;
|
||||
}
|
||||
</style>
|
||||
922
src/frontend/src/routes/bot/[id]/backtest/+page.svelte
Normal file
922
src/frontend/src/routes/bot/[id]/backtest/+page.svelte
Normal file
@@ -0,0 +1,922 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { page } from '$app/stores';
|
||||
import { goto } from '$app/navigation';
|
||||
import { isAuthenticated, isLoading, currentBotStore, setCurrentBot, backtestStore, setCurrentBacktest, addBacktestToHistory, setBacktestLoading, setBacktestError } from '$lib/stores';
|
||||
import { api } from '$lib/api';
|
||||
import { BacktestChart } from '$lib/components';
|
||||
import type { Backtest } from '$lib/api';
|
||||
|
||||
let botId = $derived($page.params.id);
|
||||
let tokenName = $state('');
|
||||
let tokenAddress = $state('');
|
||||
let timeframe = $state('1h');
|
||||
let startDate = $state('');
|
||||
let endDate = $state('');
|
||||
let isRunning = $state(false);
|
||||
let selectedBacktest = $state<Backtest | null>(null);
|
||||
|
||||
// Expandable trades state
|
||||
let expandedTrades = $state<Set<string>>(new Set());
|
||||
|
||||
// Pagination state for each backtest
|
||||
let tradesPage = $state<Record<string, number>>({});
|
||||
let tradesData = $state<Record<string, any>>({});
|
||||
const TRADES_PER_PAGE = 5;
|
||||
|
||||
onMount(async () => {
|
||||
// Set default dates - yesterday only (1 day range for fast testing)
|
||||
const yesterday = new Date();
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
|
||||
// Set max date to yesterday
|
||||
const maxDate = yesterday.toISOString().split('T')[0];
|
||||
|
||||
// Set end to yesterday, start to day before (1 day range)
|
||||
endDate = maxDate;
|
||||
const dayBefore = new Date(yesterday);
|
||||
dayBefore.setDate(dayBefore.getDate() - 1);
|
||||
startDate = dayBefore.toISOString().split('T')[0];
|
||||
|
||||
if (!$isAuthenticated && !$isLoading) {
|
||||
goto('/login');
|
||||
return;
|
||||
}
|
||||
if ($isAuthenticated && botId) {
|
||||
await loadBot();
|
||||
await loadBacktests();
|
||||
}
|
||||
});
|
||||
|
||||
async function loadBot() {
|
||||
try {
|
||||
const bot = await api.bots.get(botId);
|
||||
setCurrentBot(bot);
|
||||
|
||||
// Extract token info from strategy config
|
||||
const strategy = bot.strategy_config;
|
||||
if (strategy) {
|
||||
// Try conditions first, then actions
|
||||
const condition = strategy.conditions?.[0];
|
||||
const action = strategy.actions?.[0];
|
||||
tokenName = condition?.token || action?.token || '';
|
||||
tokenAddress = condition?.token_address || action?.token_address || '';
|
||||
}
|
||||
} catch (e) {
|
||||
goto('/dashboard');
|
||||
}
|
||||
}
|
||||
|
||||
async function loadBacktests() {
|
||||
try {
|
||||
const backtests = await api.backtest.list(botId);
|
||||
setBacktestHistory(backtests);
|
||||
} catch (e) {
|
||||
console.error('Failed to load backtests:', e);
|
||||
}
|
||||
}
|
||||
|
||||
async function startBacktest() {
|
||||
if (!startDate || !endDate) return;
|
||||
|
||||
// Validate date range (max 7 days)
|
||||
const start = new Date(startDate);
|
||||
const end = new Date(endDate);
|
||||
const daysDiff = Math.ceil((end.getTime() - start.getTime()) / (1000 * 60 * 60 * 24));
|
||||
|
||||
if (daysDiff > 7) {
|
||||
setBacktestError('Maximum backtest duration is 7 days for fast testing');
|
||||
return;
|
||||
}
|
||||
|
||||
setBacktestError(null);
|
||||
setBacktestLoading(true);
|
||||
isRunning = true;
|
||||
|
||||
try {
|
||||
const backtest = await api.backtest.start(botId, {
|
||||
token: tokenAddress, // Use token address from strategy
|
||||
token_name: tokenName, // Also send token name for display
|
||||
timeframe,
|
||||
start_date: startDate,
|
||||
end_date: endDate
|
||||
});
|
||||
setCurrentBacktest(backtest);
|
||||
addBacktestToHistory(backtest);
|
||||
} catch (e) {
|
||||
setBacktestError(e instanceof Error ? e.message : 'Failed to start backtest');
|
||||
} finally {
|
||||
setBacktestLoading(false);
|
||||
isRunning = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function stopBacktest(runId: string) {
|
||||
try {
|
||||
await api.backtest.stop(botId, runId);
|
||||
await loadBacktests();
|
||||
} catch (e) {
|
||||
console.error('Failed to stop backtest:', e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
function setBacktestHistory(backtests: any[]) {
|
||||
backtestStore.update(state => ({ ...state, backtestHistory: backtests }));
|
||||
}
|
||||
|
||||
function selectBacktest(backtest: Backtest) {
|
||||
if (backtest.status === 'completed' && backtest.result && !backtest.result.error) {
|
||||
selectedBacktest = backtest;
|
||||
}
|
||||
}
|
||||
|
||||
function toggleTrades(backtestId: string) {
|
||||
if (expandedTrades.has(backtestId)) {
|
||||
expandedTrades.delete(backtestId);
|
||||
} else {
|
||||
expandedTrades.add(backtestId);
|
||||
// Load first page of trades if not loaded
|
||||
if (!tradesData[backtestId]) {
|
||||
loadTrades(backtestId, 1);
|
||||
}
|
||||
}
|
||||
expandedTrades = new Set(expandedTrades); // Trigger reactivity
|
||||
}
|
||||
|
||||
async function loadTrades(backtestId: string, page: number) {
|
||||
try {
|
||||
const data = await api.backtest.getTrades(botId, backtestId, page, TRADES_PER_PAGE);
|
||||
tradesData[backtestId] = { ...data, currentPage: page };
|
||||
tradesData = { ...tradesData }; // Trigger reactivity
|
||||
} catch (e) {
|
||||
console.error('Failed to load trades:', e);
|
||||
}
|
||||
}
|
||||
|
||||
function nextTradesPage(backtestId: string) {
|
||||
const data = tradesData[backtestId];
|
||||
if (data && data.has_next) {
|
||||
loadTrades(backtestId, data.page + 1);
|
||||
}
|
||||
}
|
||||
|
||||
function prevTradesPage(backtestId: string) {
|
||||
const data = tradesData[backtestId];
|
||||
if (data && data.has_prev) {
|
||||
loadTrades(backtestId, data.page - 1);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Backtest - {$currentBotStore?.name || 'Bot'} - Randebu</title>
|
||||
</svelte:head>
|
||||
|
||||
<main>
|
||||
<header>
|
||||
<div class="header-left">
|
||||
<a href="/bot/{botId}" class="back-link">← Back to Chat</a>
|
||||
<h1>Backtest</h1>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="content">
|
||||
<section class="config-section">
|
||||
<h2>Configure Backtest</h2>
|
||||
|
||||
{#if $backtestStore.error}
|
||||
<div class="error">{$backtestStore.error}</div>
|
||||
{/if}
|
||||
|
||||
<form onsubmit={(e) => { e.preventDefault(); startBacktest(); }}>
|
||||
<div class="form-row">
|
||||
<div class="field token-info">
|
||||
<label>Token</label>
|
||||
<div class="token-display">
|
||||
<span class="token-name">{tokenName || 'Not configured'}</span>
|
||||
{#if tokenAddress}
|
||||
<span class="token-address">{tokenAddress.slice(0, 10)}...{tokenAddress.slice(-8)}</span>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="timeframe">Timeframe</label>
|
||||
<select id="timeframe" bind:value={timeframe}>
|
||||
<option value="1h">1 hour (recommended)</option>
|
||||
<option value="4h">4 hours</option>
|
||||
<option value="1d">1 day</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<div class="field">
|
||||
<label for="startDate">Start Date</label>
|
||||
<input type="date" id="startDate" bind:value={startDate} required />
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="endDate">End Date</label>
|
||||
<input type="date" id="endDate" bind:value={endDate} required />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button type="submit" disabled={isRunning || $backtestStore.isLoading}>
|
||||
{isRunning ? 'Running...' : 'Start Backtest'}
|
||||
</button>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<section class="results-section">
|
||||
<div class="section-header">
|
||||
<h2>Backtest History</h2>
|
||||
<button class="btn-refresh" onclick={() => loadBacktests()} disabled={$backtestStore.isLoading}>
|
||||
{$backtestStore.isLoading ? 'Refreshing...' : 'Refresh'}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{#if $backtestStore.backtestHistory.length === 0}
|
||||
<p class="empty-state">No backtests yet. Run your first backtest above.</p>
|
||||
{:else}
|
||||
<div class="backtest-list">
|
||||
{#each $backtestStore.backtestHistory as backtest}
|
||||
<div class="backtest-card">
|
||||
<div class="backtest-header">
|
||||
<span class="backtest-status status-{backtest.status}">{backtest.status}</span>
|
||||
<span class="backtest-date">{new Date(backtest.started_at).toLocaleDateString()}</span>
|
||||
</div>
|
||||
{#if backtest.result && backtest.result.error}
|
||||
<div class="backtest-error">
|
||||
<span class="error-label">Error:</span> {typeof backtest.result.error === 'string' ? backtest.result.error : JSON.stringify(backtest.result.error)}
|
||||
</div>
|
||||
{:else if backtest.result}
|
||||
<div class="backtest-results">
|
||||
<div class="result-item">
|
||||
<span class="result-label">Total Return</span>
|
||||
<span class="result-value" class:positive={backtest.result.total_return > 0} class:negative={backtest.result.total_return < 0}>
|
||||
{backtest.result.total_return.toFixed(2)}%
|
||||
</span>
|
||||
</div>
|
||||
<div class="result-item">
|
||||
<span class="result-label">Win Rate</span>
|
||||
<span class="result-value">{backtest.result.win_rate.toFixed(1)}%</span>
|
||||
</div>
|
||||
<div class="result-item">
|
||||
<span class="result-label">Total Trades</span>
|
||||
<span class="result-value">{backtest.result.total_trades}</span>
|
||||
</div>
|
||||
<div class="result-item">
|
||||
<span class="result-label">Max Drawdown</span>
|
||||
<span class="result-value negative">{backtest.result.max_drawdown.toFixed(2)}%</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="backtest-config">
|
||||
<span class="config-item">
|
||||
<span class="config-label">Token:</span> {backtest.config.token || 'Unknown'}
|
||||
</span>
|
||||
<span class="config-item">
|
||||
<span class="config-label">TF:</span> {backtest.config.timeframe || '1h'}
|
||||
</span>
|
||||
<span class="config-item">
|
||||
<span class="config-label">Period:</span> {backtest.config.start_date} to {backtest.config.end_date}
|
||||
</span>
|
||||
</div>
|
||||
{#if backtest.result.trades && backtest.result.trades.length > 0}
|
||||
<button class="btn-toggle-trades" onclick={() => toggleTrades(backtest.id)}>
|
||||
{expandedTrades.has(backtest.id) ? 'Hide' : 'Show'} Trade History ({backtest.result.trades.length})
|
||||
</button>
|
||||
{#if expandedTrades.has(backtest.id)}
|
||||
<div class="trades-inline">
|
||||
{#if tradesData[backtest.id]}
|
||||
<div class="trades-pagination-header">
|
||||
<span class="trades-count">
|
||||
Showing {((tradesData[backtest.id].page - 1) * TRADES_PER_PAGE) + 1}-{Math.min(tradesData[backtest.id].page * TRADES_PER_PAGE, tradesData[backtest.id].total_trades)} of {tradesData[backtest.id].total_trades}
|
||||
</span>
|
||||
{#if tradesData[backtest.id].total_pages > 1}
|
||||
<div class="pagination-controls">
|
||||
<button class="btn-pagination" onclick={() => prevTradesPage(backtest.id)} disabled={!tradesData[backtest.id].has_prev}>← Prev</button>
|
||||
<span class="page-indicator">Page {tradesData[backtest.id].page} of {tradesData[backtest.id].total_pages}</span>
|
||||
<button class="btn-pagination" onclick={() => nextTradesPage(backtest.id)} disabled={!tradesData[backtest.id].has_next}>Next →</button>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
<div class="trades-list">
|
||||
{#each tradesData[backtest.id].trades as trade}
|
||||
<div class="trade-item">
|
||||
<span class="trade-type" class:buy={trade.type === 'buy'} class:sell={trade.type === 'sell'}>
|
||||
{trade.type.toUpperCase()}
|
||||
</span>
|
||||
<span class="trade-price">${trade.price?.toFixed(6)}</span>
|
||||
<span class="trade-amount">${trade.amount?.toFixed(2)}</span>
|
||||
<span class="trade-reason">{trade.exit_reason || 'entry'}</span>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{:else}
|
||||
<div class="trades-loading">Loading trades...</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
{/if}
|
||||
{/if}
|
||||
{#if backtest.status === 'running'}
|
||||
<div class="progress-container">
|
||||
<div class="progress-bar">
|
||||
<div class="progress-fill" style="width: {backtest.progress ?? 0}%"></div>
|
||||
</div>
|
||||
<span class="progress-text">{backtest.progress ?? 0}%</span>
|
||||
</div>
|
||||
<button onclick={() => stopBacktest(backtest.id)} class="btn btn-danger">Stop</button>
|
||||
{/if}
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
{#if selectedBacktest}
|
||||
<section class="chart-section">
|
||||
<div class="chart-header">
|
||||
<h2>Portfolio Performance</h2>
|
||||
<button class="close-btn" onclick={() => selectedBacktest = null}>×</button>
|
||||
</div>
|
||||
<BacktestChart results={selectedBacktest.result} />
|
||||
</section>
|
||||
{/if}
|
||||
|
||||
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<style>
|
||||
:global(body) {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: #0f0f0f;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
main {
|
||||
min-height: 100vh;
|
||||
max-width: 900px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
header {
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.header-left {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.back-link {
|
||||
color: #667eea;
|
||||
text-decoration: none;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin: 0;
|
||||
font-size: 1.75rem;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 1.25rem;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.section-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.section-header h2 {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.btn-refresh {
|
||||
padding: 0.5rem 1rem;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
color: white;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
border-radius: 6px;
|
||||
font-size: 0.85rem;
|
||||
cursor: pointer;
|
||||
width: auto;
|
||||
}
|
||||
|
||||
.btn-refresh:hover:not(:disabled) {
|
||||
background: rgba(255, 255, 255, 0.15);
|
||||
transform: none;
|
||||
}
|
||||
|
||||
.btn-refresh:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.btn-sm {
|
||||
padding: 0.4rem 0.75rem;
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
/* Trades Modal */
|
||||
.trades-modal {
|
||||
max-width: 800px;
|
||||
max-height: 80vh;
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.trades-modal .modal-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.trades-modal h3 {
|
||||
margin: 0;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.debug-info {
|
||||
background: yellow;
|
||||
color: black;
|
||||
padding: 0.5rem;
|
||||
margin-bottom: 1rem;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.trades-table-wrapper {
|
||||
overflow-y: auto;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.trades-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.trades-table th,
|
||||
.trades-table td {
|
||||
padding: 0.75rem;
|
||||
text-align: left;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.trades-table th {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
font-weight: 600;
|
||||
color: #ccc;
|
||||
position: sticky;
|
||||
top: 0;
|
||||
}
|
||||
|
||||
.trades-table td {
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.trade-type {
|
||||
padding: 0.25rem 0.5rem;
|
||||
border-radius: 4px;
|
||||
font-weight: 600;
|
||||
font-size: 0.8rem;
|
||||
}
|
||||
|
||||
.trade-type.buy {
|
||||
background: rgba(76, 175, 80, 0.2);
|
||||
color: #4caf50;
|
||||
}
|
||||
|
||||
.trade-type.sell {
|
||||
background: rgba(244, 67, 54, 0.2);
|
||||
color: #f44336;
|
||||
}
|
||||
|
||||
.content {
|
||||
display: grid;
|
||||
gap: 2rem;
|
||||
}
|
||||
|
||||
section {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 12px;
|
||||
padding: 1.5rem;
|
||||
}
|
||||
|
||||
.error {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
border: 1px solid #ef4444;
|
||||
color: #fca5a5;
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 1rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.backtest-error {
|
||||
background: rgba(239, 68, 68, 0.1);
|
||||
border: 1px solid rgba(239, 68, 68, 0.3);
|
||||
color: #fca5a5;
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
font-size: 0.85rem;
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.error-label {
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.form-row {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.token-display {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.25rem;
|
||||
padding: 0.75rem;
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.token-name {
|
||||
font-weight: 600;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.token-address {
|
||||
font-size: 0.8rem;
|
||||
color: #888;
|
||||
font-family: 'Monaco', 'Menlo', monospace;
|
||||
}
|
||||
|
||||
label {
|
||||
font-size: 0.9rem;
|
||||
color: #ccc;
|
||||
}
|
||||
|
||||
input, select {
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: #fff;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
input:focus, select:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
button {
|
||||
width: 100%;
|
||||
padding: 0.875rem;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 1rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
|
||||
button:hover:not(:disabled) {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
color: #888;
|
||||
text-align: center;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.backtest-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.backtest-card {
|
||||
background: rgba(0, 0, 0, 0.2);
|
||||
border-radius: 8px;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
/* Inline Trades */
|
||||
.trades-inline {
|
||||
margin-top: 1rem;
|
||||
padding-top: 1rem;
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.trades-inline h4 {
|
||||
margin: 0 0 0.5rem;
|
||||
font-size: 0.9rem;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.trades-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.trade-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
padding: 0.5rem;
|
||||
background: rgba(255, 255, 255, 0.03);
|
||||
border-radius: 6px;
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.trade-item .trade-type {
|
||||
padding: 0.2rem 0.5rem;
|
||||
border-radius: 4px;
|
||||
font-weight: 600;
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
.trade-item .trade-type.buy {
|
||||
background: rgba(76, 175, 80, 0.2);
|
||||
color: #4caf50;
|
||||
}
|
||||
|
||||
.trade-item .trade-type.sell {
|
||||
background: rgba(244, 67, 54, 0.2);
|
||||
color: #f44336;
|
||||
}
|
||||
|
||||
.trade-price {
|
||||
color: #ccc;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.trade-amount {
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.trade-reason {
|
||||
color: #666;
|
||||
font-size: 0.8rem;
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.btn-toggle-trades {
|
||||
margin-top: 0.75rem;
|
||||
padding: 0.5rem 1rem;
|
||||
background: rgba(102, 126, 234, 0.1);
|
||||
border: 1px solid rgba(102, 126, 234, 0.3);
|
||||
border-radius: 6px;
|
||||
color: #667eea;
|
||||
font-size: 0.85rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.btn-toggle-trades:hover {
|
||||
background: rgba(102, 126, 234, 0.2);
|
||||
}
|
||||
|
||||
.backtest-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.backtest-status {
|
||||
padding: 0.25rem 0.75rem;
|
||||
border-radius: 9999px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.status-running {
|
||||
background: rgba(59, 130, 246, 0.2);
|
||||
color: #60a5fa;
|
||||
}
|
||||
|
||||
.status-completed {
|
||||
background: rgba(34, 197, 94, 0.2);
|
||||
color: #22c55e;
|
||||
}
|
||||
|
||||
.status-failed {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
color: #fca5a5;
|
||||
}
|
||||
|
||||
.status-stopped {
|
||||
background: rgba(251, 191, 36, 0.2);
|
||||
color: #fbbf24;
|
||||
}
|
||||
|
||||
.backtest-date {
|
||||
color: #888;
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.backtest-results {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(4, 1fr);
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.backtest-config {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
flex-wrap: wrap;
|
||||
margin-top: 0.75rem;
|
||||
padding: 0.5rem 0;
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
.config-item {
|
||||
font-size: 0.8rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.config-label {
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.result-item {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.25rem;
|
||||
}
|
||||
|
||||
.result-label {
|
||||
font-size: 0.75rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.result-value {
|
||||
font-size: 1.1rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.positive {
|
||||
color: #22c55e;
|
||||
}
|
||||
|
||||
.negative {
|
||||
color: #ef4444;
|
||||
}
|
||||
|
||||
.progress-container {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
.progress-bar {
|
||||
flex: 1;
|
||||
height: 8px;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border-radius: 4px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.progress-fill {
|
||||
height: 100%;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
transition: width 0.3s ease;
|
||||
}
|
||||
|
||||
.progress-text {
|
||||
font-size: 0.85rem;
|
||||
color: #888;
|
||||
min-width: 40px;
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
margin-top: 0.75rem;
|
||||
width: auto;
|
||||
padding: 0.5rem 1rem;
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
color: #fca5a5;
|
||||
border: 1px solid rgba(239, 68, 68, 0.4);
|
||||
}
|
||||
|
||||
.chart-section {
|
||||
padding: 1.5rem;
|
||||
}
|
||||
|
||||
.chart-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.chart-header h2 {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.close-btn {
|
||||
width: auto;
|
||||
padding: 0.25rem 0.75rem;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border: none;
|
||||
color: #888;
|
||||
font-size: 1.5rem;
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.close-btn:hover {
|
||||
background: rgba(255, 255, 255, 0.2);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
/* Pagination styles */
|
||||
.trades-pagination-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 0.75rem;
|
||||
padding-bottom: 0.5rem;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
.trades-count {
|
||||
font-size: 0.85rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.pagination-controls {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.btn-pagination {
|
||||
width: auto;
|
||||
padding: 0.35rem 0.75rem;
|
||||
background: rgba(102, 126, 234, 0.1);
|
||||
border: 1px solid rgba(102, 126, 234, 0.3);
|
||||
border-radius: 6px;
|
||||
color: #667eea;
|
||||
font-size: 0.8rem;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.btn-pagination:hover:not(:disabled) {
|
||||
background: rgba(102, 126, 234, 0.2);
|
||||
transform: none;
|
||||
}
|
||||
|
||||
.btn-pagination:disabled {
|
||||
opacity: 0.4;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.page-indicator {
|
||||
font-size: 0.8rem;
|
||||
color: #888;
|
||||
min-width: 80px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.trades-loading {
|
||||
text-align: center;
|
||||
color: #888;
|
||||
padding: 1rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
</style>
|
||||
530
src/frontend/src/routes/bot/[id]/simulate/+page.svelte
Normal file
530
src/frontend/src/routes/bot/[id]/simulate/+page.svelte
Normal file
@@ -0,0 +1,530 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { page } from '$app/stores';
|
||||
import { goto } from '$app/navigation';
|
||||
import { isAuthenticated, isLoading, currentBotStore, setCurrentBot, simulationStore, setCurrentSimulation, addSignals, clearSignals, setSimulationLoading, setSimulationError } from '$lib/stores';
|
||||
import { api } from '$lib/api';
|
||||
import { SignalChart, TradeDashboard, PortfolioSummary } from '$lib/components';
|
||||
|
||||
let botId = $derived($page.params.id);
|
||||
let tokenName = $state('');
|
||||
let tokenAddress = $state('');
|
||||
let klineInterval = $state('1m');
|
||||
let isRunning = $state(false);
|
||||
let isRefreshing = $state(false);
|
||||
|
||||
onMount(async () => {
|
||||
if (!$isAuthenticated && !$isLoading) {
|
||||
goto('/login');
|
||||
return;
|
||||
}
|
||||
if ($isAuthenticated && botId) {
|
||||
await loadBot();
|
||||
await loadSimulations();
|
||||
}
|
||||
});
|
||||
|
||||
async function loadBot() {
|
||||
try {
|
||||
const bot = await api.bots.get(botId);
|
||||
setCurrentBot(bot);
|
||||
|
||||
// Extract token info from strategy config
|
||||
const strategy = bot.strategy_config;
|
||||
if (strategy) {
|
||||
const condition = strategy.conditions?.[0];
|
||||
const action = strategy.actions?.[0];
|
||||
tokenName = condition?.token || action?.token || '';
|
||||
tokenAddress = condition?.token_address || action?.token_address || '';
|
||||
}
|
||||
} catch (e) {
|
||||
goto('/dashboard');
|
||||
}
|
||||
}
|
||||
|
||||
async function loadSimulations() {
|
||||
isRefreshing = true;
|
||||
try {
|
||||
const simulations = await api.simulate.list(botId);
|
||||
|
||||
// Find the most recent running simulation, or fall back to most recent
|
||||
let current = simulations.find(s => s.status === 'running') || simulations[0];
|
||||
|
||||
if (current) {
|
||||
setCurrentSimulation(current);
|
||||
clearSignals();
|
||||
if (current.signals && current.signals.length > 0) {
|
||||
addSignals(current.signals);
|
||||
}
|
||||
isRunning = current.status === 'running';
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to load simulations:', e);
|
||||
} finally {
|
||||
isRefreshing = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function startSimulation() {
|
||||
setSimulationError(null);
|
||||
setSimulationLoading(true);
|
||||
isRunning = true;
|
||||
|
||||
try {
|
||||
const simulation = await api.simulate.start(botId, {
|
||||
token: tokenAddress,
|
||||
chain: 'bsc',
|
||||
kline_interval: klineInterval
|
||||
});
|
||||
setCurrentSimulation(simulation);
|
||||
clearSignals();
|
||||
} catch (e) {
|
||||
setSimulationError(e instanceof Error ? e.message : 'Failed to start simulation');
|
||||
isRunning = false;
|
||||
} finally {
|
||||
setSimulationLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function stopSimulation() {
|
||||
if (!$simulationStore.currentSimulation) return;
|
||||
|
||||
try {
|
||||
await api.simulate.stop(botId, $simulationStore.currentSimulation.id);
|
||||
await loadSimulations();
|
||||
isRunning = false;
|
||||
} catch (e) {
|
||||
console.error('Failed to stop simulation:', e);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Simulate - {$currentBotStore?.name || 'Bot'} - Randebu</title>
|
||||
</svelte:head>
|
||||
|
||||
<main>
|
||||
<header>
|
||||
<div class="header-left">
|
||||
<a href="/bot/{botId}" class="back-link">← Back to Chat</a>
|
||||
<h1>Simulation</h1>
|
||||
</div>
|
||||
<div class="header-right">
|
||||
{#if $simulationStore.currentSimulation}
|
||||
<button
|
||||
type="button"
|
||||
class="refresh-btn"
|
||||
onclick={() => loadSimulations()}
|
||||
class:refreshing={isRefreshing}
|
||||
>
|
||||
{isRefreshing ? '⟳ Refreshing...' : '⟳ Refresh'}
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="notice">
|
||||
<span class="notice-icon">⚠️</span>
|
||||
<span>Simulation Mode - Running on {klineInterval} kline data. Stop simulation to see results.</span>
|
||||
</div>
|
||||
|
||||
<div class="content">
|
||||
<section class="config-section">
|
||||
<h2>Configure Simulation</h2>
|
||||
|
||||
{#if $simulationStore.error}
|
||||
<div class="error">{$simulationStore.error}</div>
|
||||
{/if}
|
||||
|
||||
<form onsubmit={(e) => { e.preventDefault(); startSimulation(); }}>
|
||||
<div class="form-row">
|
||||
<div class="field token-info">
|
||||
<label>Token</label>
|
||||
<div class="token-display">
|
||||
<span class="token-name">{tokenName || 'Not configured'}</span>
|
||||
{#if tokenAddress}
|
||||
<span class="token-address">{tokenAddress.slice(0, 10)}...{tokenAddress.slice(-8)}</span>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="klineInterval">Kline Interval</label>
|
||||
<select id="klineInterval" bind:value={klineInterval} disabled={isRunning}>
|
||||
<option value="1m">1 minute</option>
|
||||
<option value="5m">5 minutes</option>
|
||||
<option value="15m">15 minutes</option>
|
||||
<option value="1h">1 hour</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{#if isRunning}
|
||||
<button type="button" onclick={stopSimulation} class="btn btn-danger">
|
||||
Stop Simulation
|
||||
</button>
|
||||
{:else}
|
||||
<button type="submit" disabled={$simulationStore.isLoading}>
|
||||
{$simulationStore.isLoading ? 'Starting...' : 'Start Simulation'}
|
||||
</button>
|
||||
{/if}
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<section class="signals-section">
|
||||
<h2>Portfolio</h2>
|
||||
|
||||
<PortfolioSummary
|
||||
initialBalance={$simulationStore.currentSimulation?.portfolio?.initial_balance || 10000}
|
||||
currentBalance={$simulationStore.currentSimulation?.portfolio?.current_balance || 10000}
|
||||
position={$simulationStore.currentSimulation?.portfolio?.position || 0}
|
||||
positionToken={$simulationStore.currentSimulation?.portfolio?.position_token || ''}
|
||||
entryPrice={$simulationStore.currentSimulation?.portfolio?.entry_price || 0}
|
||||
currentPrice={$simulationStore.currentSimulation?.portfolio?.current_price || 0}
|
||||
/>
|
||||
|
||||
<h2 style="margin-top: 1.5rem;">Price Chart</h2>
|
||||
|
||||
<SignalChart signals={$simulationStore.signals} klines={$simulationStore.currentSimulation?.klines || []} height={250} />
|
||||
|
||||
<h2 style="margin-top: 1.5rem;">Trade Activity</h2>
|
||||
|
||||
<TradeDashboard tradeLog={$simulationStore.currentSimulation?.trade_log || []} />
|
||||
|
||||
<h2 style="margin-top: 1.5rem;">Signals ({$simulationStore.signals.length})</h2>
|
||||
|
||||
{#if $simulationStore.signals.length === 0}
|
||||
<p class="empty-state">No signals generated. The chart above shows price movement.</p>
|
||||
{:else}
|
||||
<div class="signals-list">
|
||||
{#each $simulationStore.signals as signal}
|
||||
<div class="signal-card">
|
||||
<div class="signal-header">
|
||||
<span class="signal-type type-{signal.signal_type}">{signal.signal_type}</span>
|
||||
<span class="signal-token">{signal.token}</span>
|
||||
<span class="signal-price">${signal.price.toFixed(6)}</span>
|
||||
</div>
|
||||
{#if signal.confidence}
|
||||
<div class="signal-confidence">
|
||||
<span>Confidence: {(signal.confidence * 100).toFixed(1)}%</span>
|
||||
<div class="confidence-bar">
|
||||
<div class="confidence-fill" style="width: {signal.confidence * 100}%"></div>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
{#if signal.reasoning}
|
||||
<p class="signal-reasoning">{signal.reasoning}</p>
|
||||
{/if}
|
||||
<div class="signal-time">
|
||||
{new Date(signal.created_at).toLocaleString()}
|
||||
</div>
|
||||
</div>
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</section>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<style>
|
||||
:global(body) {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: #0f0f0f;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
main {
|
||||
min-height: 100vh;
|
||||
max-width: 900px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.header-right {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.refresh-btn {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
color: #fff;
|
||||
padding: 0.5rem 1rem;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-size: 0.875rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.refresh-btn:hover {
|
||||
background: rgba(255, 255, 255, 0.15);
|
||||
border-color: rgba(255, 255, 255, 0.3);
|
||||
}
|
||||
|
||||
.refresh-btn.refreshing {
|
||||
opacity: 0.7;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
header {
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.header-left {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.back-link {
|
||||
color: #667eea;
|
||||
text-decoration: none;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin: 0;
|
||||
font-size: 1.75rem;
|
||||
}
|
||||
|
||||
.notice {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
background: rgba(251, 191, 36, 0.1);
|
||||
border: 1px solid rgba(251, 191, 36, 0.3);
|
||||
border-radius: 8px;
|
||||
padding: 0.75rem 1rem;
|
||||
margin-bottom: 1.5rem;
|
||||
font-size: 0.9rem;
|
||||
color: #fbbf24;
|
||||
}
|
||||
|
||||
.notice-icon {
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
|
||||
.content {
|
||||
display: grid;
|
||||
gap: 2rem;
|
||||
}
|
||||
|
||||
section {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 12px;
|
||||
padding: 1.5rem;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 1.25rem;
|
||||
margin: 0 0 1rem;
|
||||
}
|
||||
|
||||
.error {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
border: 1px solid #ef4444;
|
||||
color: #fca5a5;
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 1rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.form-row {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr;
|
||||
gap: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.token-display {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.25rem;
|
||||
padding: 0.75rem;
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.token-name {
|
||||
font-weight: 600;
|
||||
color: #667eea;
|
||||
}
|
||||
|
||||
.token-address {
|
||||
font-size: 0.8rem;
|
||||
color: #888;
|
||||
font-family: 'Monaco', 'Menlo', monospace;
|
||||
}
|
||||
|
||||
.checkbox-field {
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.checkbox-field input {
|
||||
width: auto;
|
||||
margin-right: 0.5rem;
|
||||
}
|
||||
|
||||
.checkbox-field label {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
label {
|
||||
font-size: 0.9rem;
|
||||
color: #ccc;
|
||||
}
|
||||
|
||||
input, select {
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: #fff;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
input:focus, select:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
button {
|
||||
width: 100%;
|
||||
padding: 0.875rem;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 1rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
|
||||
button:hover:not(:disabled) {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
color: #fca5a5;
|
||||
border: 1px solid rgba(239, 68, 68, 0.4);
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
color: #888;
|
||||
text-align: center;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.signals-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.signal-card {
|
||||
background: rgba(0, 0, 0, 0.2);
|
||||
border-radius: 8px;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.signal-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.signal-type {
|
||||
padding: 0.25rem 0.75rem;
|
||||
border-radius: 9999px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
|
||||
.type-buy {
|
||||
background: rgba(34, 197, 94, 0.2);
|
||||
color: #22c55e;
|
||||
}
|
||||
|
||||
.type-sell {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
color: #ef4444;
|
||||
}
|
||||
|
||||
.type-hold {
|
||||
background: rgba(251, 191, 36, 0.2);
|
||||
color: #fbbf24;
|
||||
}
|
||||
|
||||
.signal-token {
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.signal-price {
|
||||
color: #888;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.signal-confidence {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
margin-bottom: 0.5rem;
|
||||
font-size: 0.85rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.confidence-bar {
|
||||
flex: 1;
|
||||
height: 4px;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
border-radius: 2px;
|
||||
}
|
||||
|
||||
.confidence-fill {
|
||||
height: 100%;
|
||||
background: linear-gradient(90deg, #667eea, #764ba2);
|
||||
border-radius: 2px;
|
||||
}
|
||||
|
||||
.signal-reasoning {
|
||||
font-size: 0.9rem;
|
||||
color: #ccc;
|
||||
margin: 0.5rem 0;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.signal-time {
|
||||
font-size: 0.75rem;
|
||||
color: #666;
|
||||
}
|
||||
</style>
|
||||
110
src/frontend/src/routes/chat/+page.svelte
Normal file
110
src/frontend/src/routes/chat/+page.svelte
Normal file
@@ -0,0 +1,110 @@
|
||||
<script lang="ts">
|
||||
import { goto } from '$app/navigation';
|
||||
import { api } from '$lib/api';
|
||||
import { isAuthenticated } from '$lib/stores';
|
||||
import ChatLayout from '$lib/components/ChatLayout.svelte';
|
||||
import ConversationList from '$lib/components/ConversationList.svelte';
|
||||
import AnonymousBanner from '$lib/components/AnonymousBanner.svelte';
|
||||
import AppHeader from '$lib/components/AppHeader.svelte';
|
||||
|
||||
let anonymousChatCount = $state(0);
|
||||
|
||||
async function createNewChat() {
|
||||
try {
|
||||
const conv = await api.conversations.create();
|
||||
goto(`/chat/${conv.id}`);
|
||||
} catch (e) {
|
||||
console.error('Failed to create conversation:', e);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Chat - Randebu</title>
|
||||
</svelte:head>
|
||||
|
||||
<main>
|
||||
<AppHeader />
|
||||
|
||||
{#if !$isAuthenticated}
|
||||
<AnonymousBanner chatCount={anonymousChatCount} />
|
||||
{/if}
|
||||
|
||||
<div class="content">
|
||||
<ChatLayout leftPane={ConversationList}>
|
||||
<div class="empty-state">
|
||||
<div class="empty-content">
|
||||
<h2>Start a Conversation</h2>
|
||||
<p>Select a conversation from the sidebar or create a new one</p>
|
||||
<button class="btn btn-primary" onclick={createNewChat}>
|
||||
+ New Chat
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</ChatLayout>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<style>
|
||||
:global(body) {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: #0f0f0f;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
main {
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.content {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: linear-gradient(135deg, #0f0f0f 0%, #1a1a2e 100%);
|
||||
}
|
||||
|
||||
.empty-content {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 1.5rem;
|
||||
margin: 0 0 0.5rem;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
p {
|
||||
font-size: 1rem;
|
||||
color: #888;
|
||||
margin: 0 0 1.5rem;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 0.75rem 1.5rem;
|
||||
border-radius: 12px;
|
||||
font-size: 1rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
border: none;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
</style>
|
||||
585
src/frontend/src/routes/chat/[conversationId]/+page.svelte
Normal file
585
src/frontend/src/routes/chat/[conversationId]/+page.svelte
Normal file
@@ -0,0 +1,585 @@
|
||||
<script lang="ts">
|
||||
|
||||
import { page } from '$app/stores';
|
||||
import { goto } from '$app/navigation';
|
||||
import { api } from '$lib/api';
|
||||
import { isAuthenticated } from '$lib/stores';
|
||||
import type { Message, Bot, ConversationWithMessages } from '$lib/api';
|
||||
import { generateId, type ChatMessage } from '$lib/stores/chatStore';
|
||||
import ChatLayout from '$lib/components/ChatLayout.svelte';
|
||||
import ConversationList from '$lib/components/ConversationList.svelte';
|
||||
import ChatInterface from '$lib/components/ChatInterface.svelte';
|
||||
import BotInfoPanel from '$lib/components/BotInfoPanel.svelte';
|
||||
import AnonymousBanner from '$lib/components/AnonymousBanner.svelte';
|
||||
import AppHeader from '$lib/components/AppHeader.svelte';
|
||||
import CandlestickLoader from '$lib/components/CandlestickLoader.svelte';
|
||||
|
||||
let conversationId = $derived($page.params.conversationId);
|
||||
let messages = $state<ChatMessage[]>([]);
|
||||
let currentBot = $state<Bot | null>(null);
|
||||
let isLoading = $state(false);
|
||||
let error = $state<string | null>(null);
|
||||
let isSending = $state(false);
|
||||
let anonymousChatCount = $state(0);
|
||||
let hasShownLoginPrompt = $state(false); // Track if we've already shown the login prompt
|
||||
let isBlocked = $state(false); // Track if user is blocked due to limits
|
||||
let blockedReason = $state<string | null>(null); // Reason for being blocked
|
||||
|
||||
// Initialize from localStorage
|
||||
$effect(() => {
|
||||
const storedCount = localStorage.getItem('anonymous_chat_count');
|
||||
if (storedCount) {
|
||||
anonymousChatCount = parseInt(storedCount, 10);
|
||||
}
|
||||
const shownPrompt = localStorage.getItem('shown_login_prompt');
|
||||
hasShownLoginPrompt = shownPrompt === 'true';
|
||||
});
|
||||
|
||||
let currentConversation = $state<ConversationWithMessages | null>(null);
|
||||
let showBacktestModal = $state(false); // Show backtest configuration modal
|
||||
let showSimulateModal = $state(false); // Show simulation configuration modal
|
||||
|
||||
// Convert Message[] to ChatMessage[] for ChatInterface
|
||||
function convertMessages(apiMessages: Message[]): ChatMessage[] {
|
||||
return apiMessages.map(msg => ({
|
||||
id: msg.id,
|
||||
role: msg.role,
|
||||
content: msg.content,
|
||||
thinking: null,
|
||||
timestamp: new Date(msg.created_at)
|
||||
}));
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (conversationId) {
|
||||
loadConversation(conversationId);
|
||||
} else {
|
||||
messages = [];
|
||||
currentBot = null;
|
||||
}
|
||||
});
|
||||
|
||||
async function loadConversation(id: string) {
|
||||
isLoading = true;
|
||||
error = null;
|
||||
try {
|
||||
// First, try to load as bot ID (direct bot link from dashboard)
|
||||
try {
|
||||
const bot = await api.bots.get(id);
|
||||
currentBot = bot;
|
||||
messages = [];
|
||||
currentConversation = null;
|
||||
isLoading = false;
|
||||
return;
|
||||
} catch {
|
||||
// Not a bot, continue to try conversation
|
||||
}
|
||||
|
||||
// Try to load as conversation
|
||||
const conv = await api.conversations.get(id);
|
||||
currentConversation = conv;
|
||||
messages = convertMessages(conv.messages);
|
||||
|
||||
if (conv.bot_id) {
|
||||
try {
|
||||
currentBot = await api.bots.get(conv.bot_id);
|
||||
} catch (e) {
|
||||
console.error('Failed to load bot:', e);
|
||||
currentBot = null;
|
||||
}
|
||||
} else {
|
||||
currentBot = null;
|
||||
}
|
||||
} catch (e) {
|
||||
error = e instanceof Error ? e.message : 'Failed to load conversation';
|
||||
messages = [];
|
||||
currentBot = null;
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSendMessage(message: string) {
|
||||
if (!conversationId) return;
|
||||
|
||||
if (!$isAuthenticated) {
|
||||
if (anonymousChatCount >= 50) {
|
||||
// Show confirmation dialog before redirecting
|
||||
isBlocked = true;
|
||||
blockedReason = 'message_limit';
|
||||
error = 'You have reached the maximum of 50 messages. Login to continue with unlimited messages.';
|
||||
return; // Don't redirect automatically, show blocked state instead
|
||||
}
|
||||
// Show login prompt only once when reaching warning threshold (40 messages)
|
||||
if (anonymousChatCount >= 40 && !hasShownLoginPrompt) {
|
||||
hasShownLoginPrompt = true;
|
||||
localStorage.setItem('shown_login_prompt', 'true');
|
||||
const proceed = confirm('You are about to reach your message limit (40/50). Login now to save your progress and continue chatting.');
|
||||
if (proceed) {
|
||||
goto('/login');
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isSending = true;
|
||||
error = null;
|
||||
|
||||
// Optimistically add user's message to the chat
|
||||
const userMessage: ChatMessage = {
|
||||
id: generateId(),
|
||||
role: 'user',
|
||||
content: message,
|
||||
thinking: null,
|
||||
timestamp: new Date()
|
||||
};
|
||||
messages = [...messages, userMessage];
|
||||
|
||||
try {
|
||||
let updatedConv;
|
||||
|
||||
// If we have a bot loaded directly (from dashboard), use bot chat
|
||||
// Otherwise, use conversation chat
|
||||
console.log('handleSendMessage: currentBot=', currentBot, 'conversationId=', conversationId);
|
||||
if (currentBot) {
|
||||
console.log('Using bot chat for bot:', currentBot.id);
|
||||
updatedConv = await api.bots.chat(currentBot.id, message);
|
||||
// Bot chat returns the assistant response directly
|
||||
const assistantMessage: ChatMessage = {
|
||||
id: generateId(),
|
||||
role: 'assistant',
|
||||
content: updatedConv.response,
|
||||
thinking: updatedConv.thinking || null,
|
||||
timestamp: new Date()
|
||||
};
|
||||
messages = [...messages, assistantMessage];
|
||||
} else {
|
||||
updatedConv = await api.conversations.chat(conversationId, message);
|
||||
messages = convertMessages(updatedConv.messages);
|
||||
currentConversation = updatedConv;
|
||||
|
||||
if (updatedConv.bot_id && (!currentBot || currentBot.id !== updatedConv.bot_id)) {
|
||||
try {
|
||||
currentBot = await api.bots.get(updatedConv.bot_id);
|
||||
} catch (e) {
|
||||
console.error('Failed to load bot:', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$isAuthenticated) {
|
||||
anonymousChatCount++;
|
||||
localStorage.setItem('anonymous_chat_count', String(anonymousChatCount));
|
||||
}
|
||||
} catch (e: any) {
|
||||
console.error('=== CHAT ERROR CAUGHT ===', e);
|
||||
console.error('Error status:', e.status);
|
||||
console.error('Error message:', e.message);
|
||||
|
||||
// Remove the optimistic user message if the request failed
|
||||
messages = messages.filter(m => m.id !== userMessage.id);
|
||||
|
||||
const status = e.status || (e.response && e.response.status);
|
||||
const errorMsg = e.message || (e.response && e.response.data && e.response.data.detail) || 'Unknown error';
|
||||
|
||||
if (status === 429) {
|
||||
error = 'Rate limited from the agent service. Please come back later.';
|
||||
} else if (status === 403 || status === 401) {
|
||||
// Check which limit was hit based on error message
|
||||
if (errorMsg.includes('bot')) {
|
||||
isBlocked = true;
|
||||
blockedReason = 'bot_limit';
|
||||
error = "You've reached the maximum number of bots (1) as an anonymous user. Login to create more bots.";
|
||||
} else if (errorMsg.includes('backtest')) {
|
||||
isBlocked = true;
|
||||
blockedReason = 'backtest_limit';
|
||||
error = "You've reached the maximum number of backtests (1) as an anonymous user. Login to run more.";
|
||||
} else {
|
||||
isBlocked = true;
|
||||
blockedReason = 'message_limit';
|
||||
error = "You've reached the maximum number of messages (50) as an anonymous user. Login to continue chatting.";
|
||||
}
|
||||
} else {
|
||||
error = errorMsg || 'Failed to send message';
|
||||
}
|
||||
} finally {
|
||||
isSending = false;
|
||||
}
|
||||
}
|
||||
|
||||
function handleSelectBot() {
|
||||
goto('/dashboard');
|
||||
}
|
||||
|
||||
function handleBacktest() {
|
||||
showBacktestModal = true;
|
||||
}
|
||||
|
||||
function handleSimulate() {
|
||||
showSimulateModal = true;
|
||||
}
|
||||
|
||||
async function runBacktest(config: { token_address: string; timeframe: string; start_date: string; end_date: string }) {
|
||||
if (!currentBot) return;
|
||||
|
||||
isSending = true;
|
||||
error = null;
|
||||
showBacktestModal = false;
|
||||
|
||||
// Add user message
|
||||
const userMessage: ChatMessage = {
|
||||
id: generateId(),
|
||||
role: 'user',
|
||||
content: `/backtest ${config.token_address} ${config.timeframe} ${config.start_date} ${config.end_date}`,
|
||||
timestamp: new Date()
|
||||
};
|
||||
messages = [...messages, userMessage];
|
||||
|
||||
try {
|
||||
const result = await api.backtest.start(currentBot.id, {
|
||||
token: config.token_address,
|
||||
timeframe: config.timeframe,
|
||||
start_date: config.start_date,
|
||||
end_date: config.end_date
|
||||
});
|
||||
|
||||
// Add bot response
|
||||
const botMessage: ChatMessage = {
|
||||
id: generateId(),
|
||||
role: 'assistant',
|
||||
content: `Backtest started! ID: ${result.id}\nStatus: ${result.status}`,
|
||||
timestamp: new Date()
|
||||
};
|
||||
messages = [...messages, botMessage];
|
||||
} catch (e: any) {
|
||||
messages = messages.filter(m => m.id !== userMessage.id);
|
||||
error = e.message || 'Failed to start backtest';
|
||||
} finally {
|
||||
isSending = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function runSimulate(config: { token_address: string; kline_interval: string; action: string }) {
|
||||
if (!currentBot) return;
|
||||
|
||||
isSending = true;
|
||||
error = null;
|
||||
showSimulateModal = false;
|
||||
|
||||
// Add user message
|
||||
const userMessage: ChatMessage = {
|
||||
id: generateId(),
|
||||
role: 'user',
|
||||
content: `/simulate ${config.action} ${config.token_address} ${config.kline_interval}`,
|
||||
timestamp: new Date()
|
||||
};
|
||||
messages = [...messages, userMessage];
|
||||
|
||||
try {
|
||||
const result = await api.simulate.start(currentBot.id, {
|
||||
token: config.token_address,
|
||||
kline_interval: config.kline_interval
|
||||
});
|
||||
|
||||
// Add bot response
|
||||
const botMessage: ChatMessage = {
|
||||
id: generateId(),
|
||||
role: 'assistant',
|
||||
content: `Simulation ${config.action}! Token: ${config.token_address}\nInterval: ${config.kline_interval}`,
|
||||
timestamp: new Date()
|
||||
};
|
||||
messages = [...messages, botMessage];
|
||||
} catch (e: any) {
|
||||
messages = messages.filter(m => m.id !== userMessage.id);
|
||||
error = e.message || 'Failed to ${config.action} simulation';
|
||||
} finally {
|
||||
isSending = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Chat - Randebu</title>
|
||||
</svelte:head>
|
||||
|
||||
<main>
|
||||
<AppHeader />
|
||||
|
||||
{#if !$isAuthenticated}
|
||||
<AnonymousBanner chatCount={anonymousChatCount} />
|
||||
{/if}
|
||||
|
||||
<div class="content">
|
||||
<ChatLayout
|
||||
leftPane={ConversationList}
|
||||
rightPane={BotInfoPanel}
|
||||
rightPaneProps={{
|
||||
bot: currentBot,
|
||||
onSelectBot: handleSelectBot,
|
||||
onBacktest: handleBacktest,
|
||||
onSimulate: handleSimulate
|
||||
}}
|
||||
>
|
||||
{#if error}
|
||||
<div class="error-banner">
|
||||
{error}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if isLoading}
|
||||
<div class="loading">
|
||||
<CandlestickLoader />
|
||||
</div>
|
||||
{:else if conversationId}
|
||||
<ChatInterface
|
||||
bot={currentBot}
|
||||
messages={messages}
|
||||
isSending={isSending}
|
||||
isBlocked={isBlocked}
|
||||
blockedReason={blockedReason}
|
||||
onSendMessage={handleSendMessage}
|
||||
onSelectBot={handleSelectBot}
|
||||
onLogin={() => goto('/login')}
|
||||
/>
|
||||
{:else}
|
||||
<div class="empty-state">
|
||||
Select a conversation or start a new one
|
||||
</div>
|
||||
{/if}
|
||||
</ChatLayout>
|
||||
|
||||
<!-- Backtest Modal -->
|
||||
{#if showBacktestModal}
|
||||
<div class="modal-overlay" onclick={() => showBacktestModal = false}>
|
||||
<div class="modal" onclick={(e) => e.stopPropagation()}>
|
||||
<h3>Run Backtest</h3>
|
||||
<form onsubmit={(e) => {
|
||||
e.preventDefault();
|
||||
const form = e.target as HTMLFormElement;
|
||||
const fd = new FormData(form);
|
||||
runBacktest({
|
||||
token_address: fd.get('token_address') as string,
|
||||
timeframe: fd.get('timeframe') as string,
|
||||
start_date: fd.get('start_date') as string,
|
||||
end_date: fd.get('end_date') as string
|
||||
});
|
||||
}}>
|
||||
<div class="form-group">
|
||||
<label for="bt-token">Token Contract Address</label>
|
||||
<input type="text" id="bt-token" name="token_address" required placeholder="0x..." />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<div class="form-group">
|
||||
<label for="bt-timeframe">Timeframe</label>
|
||||
<select id="bt-timeframe" name="timeframe">
|
||||
<option value="1d">1 Day</option>
|
||||
<option value="4h">4 Hours</option>
|
||||
<option value="1h">1 Hour</option>
|
||||
<option value="15m">15 Minutes</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="bt-start">Start Date</label>
|
||||
<input type="date" id="bt-start" name="start_date" value="2025-01-01" />
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="bt-end">End Date</label>
|
||||
<input type="date" id="bt-end" name="end_date" value="2026-01-01" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-buttons">
|
||||
<button type="button" class="btn-cancel" onclick={() => showBacktestModal = false}>Cancel</button>
|
||||
<button type="submit" class="btn-submit">Run Backtest</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<!-- Simulation Modal -->
|
||||
{#if showSimulateModal}
|
||||
<div class="modal-overlay" onclick={() => showSimulateModal = false}>
|
||||
<div class="modal" onclick={(e) => e.stopPropagation()}>
|
||||
<h3>Run Simulation</h3>
|
||||
<form onsubmit={(e) => {
|
||||
e.preventDefault();
|
||||
const form = e.target as HTMLFormElement;
|
||||
const fd = new FormData(form);
|
||||
runSimulate({
|
||||
token_address: fd.get('token_address') as string,
|
||||
kline_interval: fd.get('kline_interval') as string,
|
||||
action: 'start'
|
||||
});
|
||||
}}>
|
||||
<div class="form-group">
|
||||
<label for="sim-token">Token Contract Address</label>
|
||||
<input type="text" id="sim-token" name="token_address" required placeholder="0x..." />
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="sim-interval">Kline Interval</label>
|
||||
<select id="sim-interval" name="kline_interval">
|
||||
<option value="1m">1 Minute</option>
|
||||
<option value="5m">5 Minutes</option>
|
||||
<option value="15m">15 Minutes</option>
|
||||
<option value="1h">1 Hour</option>
|
||||
<option value="4h">4 Hours</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="modal-buttons">
|
||||
<button type="button" class="btn-cancel" onclick={() => showSimulateModal = false}>Cancel</button>
|
||||
<button type="submit" class="btn-submit">Start Simulation</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<style>
|
||||
:global(body) {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: #0f0f0f;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
main {
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.content {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.modal-overlay {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background: rgba(0, 0, 0, 0.7);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.modal {
|
||||
background: #1a1a1a;
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 12px;
|
||||
padding: 1.5rem;
|
||||
width: 400px;
|
||||
max-width: 90vw;
|
||||
}
|
||||
|
||||
.modal h3 {
|
||||
margin: 0 0 1.25rem;
|
||||
font-size: 1.1rem;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.form-group {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.form-group label {
|
||||
display: block;
|
||||
font-size: 0.8rem;
|
||||
color: #888;
|
||||
margin-bottom: 0.35rem;
|
||||
}
|
||||
|
||||
.form-group input,
|
||||
.form-group select {
|
||||
width: 100%;
|
||||
padding: 0.6rem 0.75rem;
|
||||
background: #0f0f0f;
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 6px;
|
||||
color: #fff;
|
||||
font-size: 0.9rem;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.form-group input:focus,
|
||||
.form-group select:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.form-row {
|
||||
display: flex;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.form-row .form-group {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.modal-buttons {
|
||||
display: flex;
|
||||
gap: 0.75rem;
|
||||
margin-top: 1.25rem;
|
||||
}
|
||||
|
||||
.btn-cancel {
|
||||
flex: 1;
|
||||
padding: 0.7rem;
|
||||
background: transparent;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
border-radius: 6px;
|
||||
color: #ccc;
|
||||
cursor: pointer;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.btn-cancel:hover {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
}
|
||||
|
||||
.btn-submit {
|
||||
flex: 1;
|
||||
padding: 0.7rem;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
font-size: 0.9rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.btn-submit:hover {
|
||||
opacity: 0.9;
|
||||
}
|
||||
|
||||
.error-banner {
|
||||
padding: 0.75rem 1rem;
|
||||
background: rgba(220, 38, 38, 0.2);
|
||||
border-bottom: 1px solid rgba(220, 38, 38, 0.3);
|
||||
color: #fca5a5;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.loading {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: #888;
|
||||
font-size: 1rem;
|
||||
}
|
||||
</style>
|
||||
291
src/frontend/src/routes/dashboard/+page.svelte
Normal file
291
src/frontend/src/routes/dashboard/+page.svelte
Normal file
@@ -0,0 +1,291 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { goto } from '$app/navigation';
|
||||
import { isAuthenticated, isLoading, botsStore, setBots, addBot, removeBot, userStore, logout } from '$lib/stores';
|
||||
import { api } from '$lib/api';
|
||||
import { BotCard } from '$lib/components';
|
||||
|
||||
let showCreateModal = $state(false);
|
||||
let newBotName = $state('');
|
||||
let newBotDescription = $state('');
|
||||
let isCreating = $state(false);
|
||||
let createError = $state('');
|
||||
|
||||
onMount(async () => {
|
||||
if (!$isAuthenticated && !$isLoading) {
|
||||
goto('/login');
|
||||
return;
|
||||
}
|
||||
if ($isAuthenticated) {
|
||||
await loadBots();
|
||||
}
|
||||
});
|
||||
|
||||
async function loadBots() {
|
||||
try {
|
||||
const bots = await api.bots.list();
|
||||
setBots(bots);
|
||||
} catch (e) {
|
||||
console.error('Failed to load bots:', e);
|
||||
}
|
||||
}
|
||||
|
||||
async function createBot() {
|
||||
if (!newBotName.trim()) return;
|
||||
createError = '';
|
||||
isCreating = true;
|
||||
try {
|
||||
const bot = await api.bots.create(newBotName, newBotDescription);
|
||||
addBot(bot);
|
||||
showCreateModal = false;
|
||||
newBotName = '';
|
||||
newBotDescription = '';
|
||||
goto(`/chat/${bot.id}`);
|
||||
} catch (e) {
|
||||
createError = e instanceof Error ? e.message : 'Failed to create bot';
|
||||
} finally {
|
||||
isCreating = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteBot(botId: string) {
|
||||
if (!confirm('Are you sure you want to delete this bot?')) return;
|
||||
try {
|
||||
await api.bots.delete(botId);
|
||||
removeBot(botId);
|
||||
} catch (e) {
|
||||
console.error('Failed to delete bot:', e);
|
||||
}
|
||||
}
|
||||
|
||||
function handleLogout() {
|
||||
logout();
|
||||
goto('/');
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Dashboard - Randebu</title>
|
||||
</svelte:head>
|
||||
|
||||
<main>
|
||||
<header>
|
||||
<h1>Dashboard</h1>
|
||||
<div class="header-actions">
|
||||
<span class="user-email">{$userStore?.email}</span>
|
||||
<a href="/settings" class="btn btn-secondary">Settings</a>
|
||||
<button onclick={handleLogout} class="btn btn-secondary">Logout</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<section class="bots-section">
|
||||
<div class="section-header">
|
||||
<h2>Your Bots ({$botsStore.length}/3)</h2>
|
||||
{#if $botsStore.length < 3}
|
||||
<button onclick={() => showCreateModal = true} class="btn btn-primary">
|
||||
Create New Bot
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if $botsStore.length === 0}
|
||||
<div class="empty-state">
|
||||
<p>You haven't created any bots yet.</p>
|
||||
<p>Create your first bot to start trading!</p>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="bots-grid">
|
||||
{#each $botsStore as bot}
|
||||
<BotCard {bot} onDelete={deleteBot} />
|
||||
{/each}
|
||||
</div>
|
||||
{/if}
|
||||
</section>
|
||||
|
||||
{#if showCreateModal}
|
||||
<div class="modal-overlay" onclick={() => showCreateModal = false}>
|
||||
<div class="modal" onclick={(e) => e.stopPropagation()}>
|
||||
<h2>Create New Bot</h2>
|
||||
{#if createError}
|
||||
<div class="error">{createError}</div>
|
||||
{/if}
|
||||
<form onsubmit={(e) => { e.preventDefault(); createBot(); }}>
|
||||
<div class="field">
|
||||
<label for="botName">Bot Name</label>
|
||||
<input type="text" id="botName" bind:value={newBotName} required />
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="botDescription">Description (optional)</label>
|
||||
<textarea id="botDescription" bind:value={newBotDescription} rows="3"></textarea>
|
||||
</div>
|
||||
<div class="modal-actions">
|
||||
<button type="button" onclick={() => showCreateModal = false} class="btn btn-secondary">
|
||||
Cancel
|
||||
</button>
|
||||
<button type="submit" disabled={isCreating}>
|
||||
{isCreating ? 'Creating...' : 'Create'}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
</main>
|
||||
|
||||
<style>
|
||||
:global(body) {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: #0f0f0f;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
main {
|
||||
min-height: 100vh;
|
||||
padding: 2rem;
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 2rem;
|
||||
padding-bottom: 1rem;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin: 0;
|
||||
font-size: 2rem;
|
||||
}
|
||||
|
||||
.header-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.user-email {
|
||||
color: #888;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
h2 {
|
||||
margin: 0;
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
|
||||
.section-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.bots-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
|
||||
gap: 1.5rem;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 0.5rem 1rem;
|
||||
border-radius: 8px;
|
||||
text-decoration: none;
|
||||
font-weight: 500;
|
||||
font-size: 0.9rem;
|
||||
cursor: pointer;
|
||||
border: none;
|
||||
transition: transform 0.2s, opacity 0.2s;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
color: white;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 3rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.modal-overlay {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background: rgba(0, 0, 0, 0.8);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.modal {
|
||||
background: #1a1a1a;
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 16px;
|
||||
padding: 2rem;
|
||||
width: 100%;
|
||||
max-width: 450px;
|
||||
}
|
||||
|
||||
.modal h2 {
|
||||
margin: 0 0 1.5rem;
|
||||
}
|
||||
|
||||
.error {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
border: 1px solid #ef4444;
|
||||
color: #fca5a5;
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 1rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.field {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
margin-bottom: 0.5rem;
|
||||
color: #ccc;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
input, textarea {
|
||||
width: 100%;
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: #fff;
|
||||
font-size: 1rem;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
input:focus, textarea:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.modal-actions {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
justify-content: flex-end;
|
||||
margin-top: 1.5rem;
|
||||
}
|
||||
</style>
|
||||
166
src/frontend/src/routes/home/+page.svelte
Normal file
166
src/frontend/src/routes/home/+page.svelte
Normal file
@@ -0,0 +1,166 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { goto } from '$app/navigation';
|
||||
import { api } from '$lib/api';
|
||||
import { isAuthenticated } from '$lib/stores';
|
||||
import ChatLayout from '$lib/components/ChatLayout.svelte';
|
||||
import ConversationList from '$lib/components/ConversationList.svelte';
|
||||
import AnonymousBanner from '$lib/components/AnonymousBanner.svelte';
|
||||
import AppHeader from '$lib/components/AppHeader.svelte';
|
||||
|
||||
let anonymousChatCount = $state(0);
|
||||
|
||||
onMount(async () => {
|
||||
// Check if there's an anonymous token in cookies
|
||||
const cookies = document.cookie.split(';');
|
||||
const anonToken = cookies.find(c => c.trim().startsWith('anonymous_token='));
|
||||
if (anonToken) {
|
||||
// Count would be tracked server-side, for now just track locally
|
||||
const stored = localStorage.getItem('anonymous_chat_count');
|
||||
anonymousChatCount = stored ? parseInt(stored, 10) : 0;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Randebu - AI Trading Bot Platform</title>
|
||||
</svelte:head>
|
||||
|
||||
<main>
|
||||
<AppHeader />
|
||||
|
||||
{#if !$isAuthenticated}
|
||||
<AnonymousBanner chatCount={anonymousChatCount} />
|
||||
{/if}
|
||||
|
||||
<div class="content">
|
||||
<ChatLayout leftPane={ConversationList}>
|
||||
<div class="welcome-screen">
|
||||
<div class="welcome-content">
|
||||
<h1>Welcome to Randebu</h1>
|
||||
<p class="subtitle">Create trading bots through conversation with AI</p>
|
||||
|
||||
<div class="steps">
|
||||
<div class="step">
|
||||
<div class="step-number">1</div>
|
||||
<div class="step-content">
|
||||
<h3>Describe Your Strategy</h3>
|
||||
<p>Tell our AI what kind of trading you want in plain English</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="step">
|
||||
<div class="step-number">2</div>
|
||||
<div class="step-content">
|
||||
<h3>Backtest & Validate</h3>
|
||||
<p>Test your strategy against historical data</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="step">
|
||||
<div class="step-number">3</div>
|
||||
<div class="step-content">
|
||||
<h3>Simulate & Monitor</h3>
|
||||
<p>Run real-time simulations and watch for signals</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p class="hint">Select a conversation from the left or start a new one to begin</p>
|
||||
</div>
|
||||
</div>
|
||||
</ChatLayout>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<style>
|
||||
:global(body) {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: #0f0f0f;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
main {
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.content {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.welcome-screen {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: linear-gradient(135deg, #0f0f0f 0%, #1a1a2e 100%);
|
||||
}
|
||||
|
||||
.welcome-content {
|
||||
text-align: center;
|
||||
max-width: 500px;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 2.5rem;
|
||||
margin: 0 0 0.5rem;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
-webkit-background-clip: text;
|
||||
-webkit-text-fill-color: transparent;
|
||||
background-clip: text;
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
font-size: 1.2rem;
|
||||
color: #888;
|
||||
margin: 0 0 2.5rem;
|
||||
}
|
||||
|
||||
.steps {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1.5rem;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.step {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.step-number {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
border-radius: 50%;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-weight: 600;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.step-content h3 {
|
||||
margin: 0 0 0.25rem;
|
||||
font-size: 1.1rem;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.step-content p {
|
||||
margin: 0;
|
||||
font-size: 0.9rem;
|
||||
color: #888;
|
||||
}
|
||||
|
||||
.hint {
|
||||
margin-top: 2.5rem;
|
||||
font-size: 0.9rem;
|
||||
color: #666;
|
||||
}
|
||||
</style>
|
||||
176
src/frontend/src/routes/login/+page.svelte
Normal file
176
src/frontend/src/routes/login/+page.svelte
Normal file
@@ -0,0 +1,176 @@
|
||||
<script lang="ts">
|
||||
import { goto } from '$app/navigation';
|
||||
import { login, isAuthenticated } from '$lib/stores';
|
||||
|
||||
let email = $state('');
|
||||
let password = $state('');
|
||||
let error = $state('');
|
||||
let isLoading = $state(false);
|
||||
|
||||
$effect(() => {
|
||||
if ($isAuthenticated) {
|
||||
goto('/dashboard');
|
||||
}
|
||||
});
|
||||
|
||||
async function handleSubmit() {
|
||||
error = '';
|
||||
isLoading = true;
|
||||
try {
|
||||
await login(email, password);
|
||||
goto('/dashboard');
|
||||
} catch (e) {
|
||||
error = e instanceof Error ? e.message : 'Login failed';
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Login - Randebu</title>
|
||||
</svelte:head>
|
||||
|
||||
<main>
|
||||
<div class="auth-card">
|
||||
<h1>Login</h1>
|
||||
<p class="subtitle">Welcome back</p>
|
||||
|
||||
{#if error}
|
||||
<div class="error">{error}</div>
|
||||
{/if}
|
||||
|
||||
<form onsubmit={(e) => { e.preventDefault(); handleSubmit(); }}>
|
||||
<div class="field">
|
||||
<label for="email">Email</label>
|
||||
<input type="email" id="email" bind:value={email} required />
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="password">Password</label>
|
||||
<input type="password" id="password" bind:value={password} required />
|
||||
</div>
|
||||
|
||||
<button type="submit" disabled={isLoading}>
|
||||
{isLoading ? 'Logging in...' : 'Login'}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<p class="footer">
|
||||
Don't have an account? <a href="/register">Register</a>
|
||||
</p>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<style>
|
||||
:global(body) {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: #0f0f0f;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
main {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.auth-card {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 16px;
|
||||
padding: 2.5rem;
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin: 0;
|
||||
font-size: 1.75rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
color: #888;
|
||||
text-align: center;
|
||||
margin: 0.5rem 0 2rem;
|
||||
}
|
||||
|
||||
.error {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
border: 1px solid #ef4444;
|
||||
color: #fca5a5;
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 1rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.field {
|
||||
margin-bottom: 1.25rem;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
margin-bottom: 0.5rem;
|
||||
color: #ccc;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
input {
|
||||
width: 100%;
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: #fff;
|
||||
font-size: 1rem;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
input:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
button {
|
||||
width: 100%;
|
||||
padding: 0.875rem;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 1rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
|
||||
button:hover:not(:disabled) {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.footer {
|
||||
text-align: center;
|
||||
margin-top: 1.5rem;
|
||||
color: #888;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.footer a {
|
||||
color: #667eea;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.footer a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
</style>
|
||||
193
src/frontend/src/routes/register/+page.svelte
Normal file
193
src/frontend/src/routes/register/+page.svelte
Normal file
@@ -0,0 +1,193 @@
|
||||
<script lang="ts">
|
||||
import { goto } from '$app/navigation';
|
||||
import { register, isAuthenticated } from '$lib/stores';
|
||||
|
||||
let email = $state('');
|
||||
let password = $state('');
|
||||
let confirmPassword = $state('');
|
||||
let error = $state('');
|
||||
let isLoading = $state(false);
|
||||
|
||||
$effect(() => {
|
||||
if ($isAuthenticated) {
|
||||
goto('/dashboard');
|
||||
}
|
||||
});
|
||||
|
||||
async function handleSubmit() {
|
||||
error = '';
|
||||
|
||||
if (password !== confirmPassword) {
|
||||
error = 'Passwords do not match';
|
||||
return;
|
||||
}
|
||||
|
||||
if (password.length < 6) {
|
||||
error = 'Password must be at least 6 characters';
|
||||
return;
|
||||
}
|
||||
|
||||
isLoading = true;
|
||||
try {
|
||||
await register(email, password);
|
||||
goto('/dashboard');
|
||||
} catch (e) {
|
||||
error = e instanceof Error ? e.message : 'Registration failed';
|
||||
} finally {
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Register - Randebu</title>
|
||||
</svelte:head>
|
||||
|
||||
<main>
|
||||
<div class="auth-card">
|
||||
<h1>Create Account</h1>
|
||||
<p class="subtitle">Start creating trading bots</p>
|
||||
|
||||
{#if error}
|
||||
<div class="error">{error}</div>
|
||||
{/if}
|
||||
|
||||
<form onsubmit={(e) => { e.preventDefault(); handleSubmit(); }}>
|
||||
<div class="field">
|
||||
<label for="email">Email</label>
|
||||
<input type="email" id="email" bind:value={email} required />
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="password">Password</label>
|
||||
<input type="password" id="password" bind:value={password} required minlength="6" />
|
||||
</div>
|
||||
|
||||
<div class="field">
|
||||
<label for="confirmPassword">Confirm Password</label>
|
||||
<input type="password" id="confirmPassword" bind:value={confirmPassword} required />
|
||||
</div>
|
||||
|
||||
<button type="submit" disabled={isLoading}>
|
||||
{isLoading ? 'Creating account...' : 'Create Account'}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<p class="footer">
|
||||
Already have an account? <a href="/login">Login</a>
|
||||
</p>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<style>
|
||||
:global(body) {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: #0f0f0f;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
main {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
.auth-card {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 16px;
|
||||
padding: 2.5rem;
|
||||
width: 100%;
|
||||
max-width: 400px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin: 0;
|
||||
font-size: 1.75rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
color: #888;
|
||||
text-align: center;
|
||||
margin: 0.5rem 0 2rem;
|
||||
}
|
||||
|
||||
.error {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
border: 1px solid #ef4444;
|
||||
color: #fca5a5;
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 1rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.field {
|
||||
margin-bottom: 1.25rem;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
margin-bottom: 0.5rem;
|
||||
color: #ccc;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
input {
|
||||
width: 100%;
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: #fff;
|
||||
font-size: 1rem;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
input:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
button {
|
||||
width: 100%;
|
||||
padding: 0.875rem;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 1rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
|
||||
button:hover:not(:disabled) {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.footer {
|
||||
text-align: center;
|
||||
margin-top: 1.5rem;
|
||||
color: #888;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.footer a {
|
||||
color: #667eea;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.footer a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
</style>
|
||||
281
src/frontend/src/routes/settings/+page.svelte
Normal file
281
src/frontend/src/routes/settings/+page.svelte
Normal file
@@ -0,0 +1,281 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { goto } from '$app/navigation';
|
||||
import { isAuthenticated, isLoading, userStore, logout } from '$lib/stores';
|
||||
import { api } from '$lib/api';
|
||||
|
||||
let email = $state('');
|
||||
let currentPassword = $state('');
|
||||
let newPassword = $state('');
|
||||
let confirmPassword = $state('');
|
||||
let isUpdating = $state(false);
|
||||
let updateSuccess = $state('');
|
||||
let updateError = $state('');
|
||||
|
||||
onMount(async () => {
|
||||
if (!$isAuthenticated && !$isLoading) {
|
||||
goto('/login');
|
||||
return;
|
||||
}
|
||||
if ($userStore) {
|
||||
email = $userStore.email;
|
||||
}
|
||||
});
|
||||
|
||||
async function updateEmail() {
|
||||
updateSuccess = '';
|
||||
updateError = '';
|
||||
isUpdating = true;
|
||||
try {
|
||||
await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:8000/api'}/auth/settings`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${localStorage.getItem('token')}`
|
||||
},
|
||||
body: JSON.stringify({ email })
|
||||
});
|
||||
updateSuccess = 'Email updated successfully';
|
||||
} catch (e) {
|
||||
updateError = e instanceof Error ? e.message : 'Failed to update email';
|
||||
} finally {
|
||||
isUpdating = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function updatePassword() {
|
||||
updateSuccess = '';
|
||||
updateError = '';
|
||||
|
||||
if (newPassword !== confirmPassword) {
|
||||
updateError = 'Passwords do not match';
|
||||
return;
|
||||
}
|
||||
|
||||
if (newPassword.length < 6) {
|
||||
updateError = 'Password must be at least 6 characters';
|
||||
return;
|
||||
}
|
||||
|
||||
isUpdating = true;
|
||||
try {
|
||||
await fetch(`${import.meta.env.VITE_API_URL || 'http://localhost:8000/api'}/auth/settings`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${localStorage.getItem('token')}`
|
||||
},
|
||||
body: JSON.stringify({ password: newPassword, current_password: currentPassword })
|
||||
});
|
||||
updateSuccess = 'Password updated successfully';
|
||||
currentPassword = '';
|
||||
newPassword = '';
|
||||
confirmPassword = '';
|
||||
} catch (e) {
|
||||
updateError = e instanceof Error ? e.message : 'Failed to update password';
|
||||
} finally {
|
||||
isUpdating = false;
|
||||
}
|
||||
}
|
||||
|
||||
function handleLogout() {
|
||||
logout();
|
||||
goto('/');
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Settings - Randebu</title>
|
||||
</svelte:head>
|
||||
|
||||
<main>
|
||||
<header>
|
||||
<div class="header-left">
|
||||
<a href="/dashboard" class="back-link">← Dashboard</a>
|
||||
<h1>Settings</h1>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="content">
|
||||
<section class="settings-section">
|
||||
<h2>Profile</h2>
|
||||
|
||||
{#if updateSuccess}
|
||||
<div class="success">{updateSuccess}</div>
|
||||
{/if}
|
||||
|
||||
{#if updateError}
|
||||
<div class="error">{updateError}</div>
|
||||
{/if}
|
||||
|
||||
<form onsubmit={(e) => { e.preventDefault(); updateEmail(); }}>
|
||||
<div class="field">
|
||||
<label for="email">Email</label>
|
||||
<input type="email" id="email" bind:value={email} required />
|
||||
</div>
|
||||
<button type="submit" disabled={isUpdating}>
|
||||
{isUpdating ? 'Updating...' : 'Update Email'}
|
||||
</button>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<section class="settings-section">
|
||||
<h2>Change Password</h2>
|
||||
|
||||
<form onsubmit={(e) => { e.preventDefault(); updatePassword(); }}>
|
||||
<div class="field">
|
||||
<label for="currentPassword">Current Password</label>
|
||||
<input type="password" id="currentPassword" bind:value={currentPassword} required />
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="newPassword">New Password</label>
|
||||
<input type="password" id="newPassword" bind:value={newPassword} required minlength="6" />
|
||||
</div>
|
||||
<div class="field">
|
||||
<label for="confirmPassword">Confirm New Password</label>
|
||||
<input type="password" id="confirmPassword" bind:value={confirmPassword} required />
|
||||
</div>
|
||||
<button type="submit" disabled={isUpdating}>
|
||||
{isUpdating ? 'Updating...' : 'Update Password'}
|
||||
</button>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<section class="settings-section danger-section">
|
||||
<h2>Account</h2>
|
||||
<button onclick={handleLogout} class="btn btn-danger">
|
||||
Logout
|
||||
</button>
|
||||
</section>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<style>
|
||||
:global(body) {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
background: #0f0f0f;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
main {
|
||||
min-height: 100vh;
|
||||
max-width: 600px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
}
|
||||
|
||||
header {
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.header-left {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.back-link {
|
||||
color: #667eea;
|
||||
text-decoration: none;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin: 0;
|
||||
font-size: 1.75rem;
|
||||
}
|
||||
|
||||
.content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1.5rem;
|
||||
}
|
||||
|
||||
section {
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
border-radius: 12px;
|
||||
padding: 1.5rem;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 1.1rem;
|
||||
margin: 0 0 1rem;
|
||||
}
|
||||
|
||||
.success {
|
||||
background: rgba(34, 197, 94, 0.2);
|
||||
border: 1px solid #22c55e;
|
||||
color: #86efac;
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 1rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.error {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
border: 1px solid #ef4444;
|
||||
color: #fca5a5;
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 1rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.field {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
margin-bottom: 0.5rem;
|
||||
color: #ccc;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
input {
|
||||
width: 100%;
|
||||
padding: 0.75rem;
|
||||
border-radius: 8px;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
background: rgba(255, 255, 255, 0.05);
|
||||
color: #fff;
|
||||
font-size: 1rem;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
input:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
button {
|
||||
width: 100%;
|
||||
padding: 0.875rem;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 1rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
|
||||
button:hover:not(:disabled) {
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.danger-section button {
|
||||
background: rgba(239, 68, 68, 0.2);
|
||||
color: #fca5a5;
|
||||
border: 1px solid rgba(239, 68, 68, 0.4);
|
||||
}
|
||||
</style>
|
||||
3
src/frontend/static/robots.txt
Normal file
3
src/frontend/static/robots.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
# allow crawling everything by default
|
||||
User-agent: *
|
||||
Disallow:
|
||||
17
src/frontend/svelte.config.js
Normal file
17
src/frontend/svelte.config.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import adapter from '@sveltejs/adapter-auto';
|
||||
|
||||
/** @type {import('@sveltejs/kit').Config} */
|
||||
const config = {
|
||||
compilerOptions: {
|
||||
// Force runes mode for the project, except for libraries. Can be removed in svelte 6.
|
||||
runes: ({ filename }) => (filename.split(/[/\\]/).includes('node_modules') ? undefined : true)
|
||||
},
|
||||
kit: {
|
||||
// adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list.
|
||||
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
|
||||
// See https://svelte.dev/docs/kit/adapters for more information about adapters.
|
||||
adapter: adapter()
|
||||
}
|
||||
};
|
||||
|
||||
export default config;
|
||||
20
src/frontend/tsconfig.json
Normal file
20
src/frontend/tsconfig.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"extends": "./.svelte-kit/tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"rewriteRelativeImportExtensions": true,
|
||||
"allowJs": true,
|
||||
"checkJs": true,
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"resolveJsonModule": true,
|
||||
"skipLibCheck": true,
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
"moduleResolution": "bundler"
|
||||
}
|
||||
// Path aliases are handled by https://svelte.dev/docs/kit/configuration#alias
|
||||
// except $lib which is handled by https://svelte.dev/docs/kit/configuration#files
|
||||
//
|
||||
// To make changes to top-level options such as include and exclude, we recommend extending
|
||||
// the generated config; see https://svelte.dev/docs/kit/configuration#typescript
|
||||
}
|
||||
6
src/frontend/vite.config.ts
Normal file
6
src/frontend/vite.config.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { sveltekit } from '@sveltejs/kit/vite';
|
||||
import { defineConfig } from 'vite';
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [sveltekit()]
|
||||
});
|
||||
Reference in New Issue
Block a user