Compare commits

...

26 Commits

Author SHA1 Message Date
shokollm
fccdbb4cca docs: add README.md 2026-04-15 15:46:05 +00:00
958dc3bb1f Merge pull request 'feat: conversation-based chat system with anonymous support' (#65) from fix/issue-59 into main 2026-04-14 08:19:54 +02:00
shokollm
5ae8d76bde feat: implement conversation-based chat system with anonymous support
- Add Conversation model with user/anonymous_token/bot_id fields
- Add Message model linked to conversations
- Add AnonymousUser model for tracking anonymous chat limits
- Create /api/conversations endpoints (list, create, get, delete)
- Add POST /api/conversations/{id}/chat for messaging
- Add POST /api/conversations/{id}/set-bot for linking bot
- Implement rate limiter with system-wide (500/5hrs) and anonymous limits
- Anonymous users: max 50 chats, max 1 bot, max 1 backtest
- Add warning after 40 anonymous messages
- Register conversations router in main.py
- Add create_bot, list_bots, set_bot, get_bot_info tools to registry
2026-04-14 04:25:00 +00:00
a9679bbb5d Merge pull request 'refactor: Split conversational.py into modular structure' (#64) from fix/issue-63 into main 2026-04-14 05:57:16 +02:00
shokollm
b1ddad0808 Fix intermittent UnboundLocalError for 'thinking' variable in ConversationalAgent.chat() - initialize thinking=None before conditional assignment to handle API responses missing message field 2026-04-14 03:34:36 +00:00
shokollm
f705269e34 refactor: Split conversational.py into modular structure (fixes #63)
Split conversational.py (2271 lines) into modular files:
- tools.py: TOOL_REGISTRY, get_tool_registry(), SKILL_EMOJIS
- help.py: format_* functions for slash command help
- client.py: MiniMaxClient, SYSTEM_PROMPT, TOOLS definitions
- agent.py: ConversationalAgent class with all methods
- __init__.py: Public exports from all modules

Updated bots.py import to use new module path.
Deleted conversational.py.
2026-04-14 02:36:23 +00:00
8acce849f4 Merge pull request 'feat: Add slash command help system (#57)' (#62) from fix/issue-57 into main 2026-04-14 04:03:29 +02:00
shokollm
2d125ede22 fix: Also fix price_change field in AI tool execution path
There was duplicate code handling search_tokens in the AI tool calling section.
2026-04-14 01:43:30 +00:00
shokollm
7a64632a63 fix: Correct price_change field fallback logic
Was returning 'N/A' incorrectly when token_price_change_24h was missing.
Now properly checks: price_change_24h OR token_price_change_24h OR 'N/A'
2026-04-14 00:37:48 +00:00
shokollm
bb62e53093 fix: Handle price_change_24h field name in search results
Search API returns 'price_change_24h' not 'token_price_change_24h'. Now checks for both.
2026-04-14 00:33:38 +00:00
shokollm
cf74251ad0 fix: Show token name/symbol in risk analysis and handle unknown honeypot
- Display token name and symbol in risk analysis output
- Handle is_honeypot: -1 as 'Unknown (could not determine)'
- Show risk level (Low/Medium/High) with risk score
- Use risk_level field instead of status
2026-04-14 00:28:15 +00:00
shokollm
1efc0eaba6 feat: Add context awareness for price tool
- Store recent search results in agent instance
- When price returns empty, suggest using /search tool
- Check if user input matches recent search results and use that address
2026-04-14 00:19:42 +00:00
shokollm
f4f6168f68 revert: Keep using price API for price lookups
The price API requires full contract addresses (0x...-bsc format).
Improved error handling and formatting for price responses.
2026-04-14 00:12:46 +00:00
shokollm
62bcd6e099 fix: Use search API for price lookups instead of price API
The price API requires full contract addresses (0x...-bsc), but users typically provide symbols.
Now /price TRADOOR will search for the token and show price info from search results.
2026-04-14 00:03:34 +00:00
shokollm
6b8912a7eb fix: Better error detection for AVE API commands
- Added _is_error_output helper to detect errors in CLI output
- API errors like 'API error 403' now show proper error message instead of 'No price data available'
- Updated all command execution methods to use the helper
2026-04-13 23:55:51 +00:00
shokollm
2c3b6ef073 fix: Show token name and ticker in backtest result
- Added _get_token_info helper to fetch symbol and name
- Backtest result now shows: **PEPE** (Pepe) -
2026-04-13 23:37:17 +00:00
shokollm
613ec0dc1f fix: Provide default empty string for backtest/simulate calls
- Fixed missing message argument when calling direct execution methods
- Both /backtest and /simulate now work without arguments
2026-04-13 23:34:04 +00:00
shokollm
7bdd49a56c fix: Execute backtest and simulate commands directly
- Added _execute_backtest_direct() that extracts params from message or strategy config
- Added _execute_simulate_direct() that extracts params from message or strategy config
- Both commands now execute directly when called with /backtest or /simulate
- If token address is missing, asks user for the parameter
2026-04-13 23:32:08 +00:00
shokollm
e92506a787 feat: Two-step command execution flow
Commands now execute in two steps:
1. User sends /search -> acknowledge and wait for param
2. User sends 'pepe' -> auto-execute search with 'pepe'

Commands without params (/backtest, /simulate, /trending, /strategy) execute directly.

Pending commands tracked via self.pending_command state.
2026-04-13 23:23:01 +00:00
shokollm
696d3934d5 fix: Execute trending command directly when /trending is called
- Added _execute_trending method that runs the trending CLI command
- Returns formatted list of trending tokens on BSC
- Shows error message if no tokens found or command fails
2026-04-13 23:07:43 +00:00
shokollm
466fdf1fe9 fix: Fetch strategy from database when /strategy is called
- Added _get_strategy_response method to query bot's strategy_config from DB
- Shows formatted strategy with conditions, actions, and risk management
- Shows helpful message if no strategy is configured yet
2026-04-13 23:02:49 +00:00
shokollm
39a27caf05 feat: Add slash command system with skill acknowledgments
- Reset conversational.py to pr-58 working AVE integration
- Added TOOL_REGISTRY with all available slash commands
- Added _handle_slash_command for skill activation
- Slash commands show brief acknowledgment when used alone
- Slash commands with args are passed to AI for handling
- Added dropdown menu in ChatInterface for skill discovery
- Menu positions above textarea
- Menu shows filtered tools as user types
2026-04-13 16:21:57 +00:00
shokollm
61b9da295b feat: Add /trending tool for popular tokens 2026-04-13 13:51:17 +00:00
shokollm
38e45b9fd0 fix: Use 'Commands:' instead of 'Usage:' to match issue spec 2026-04-13 13:48:17 +00:00
shokollm
e41d07486b feat: Add slash command help system for conversational interface
Implement slash command help system as described in issue #57:

- Add Tool Registry in backend with metadata for all available tools
- Add command parser for '/' prefix in ConversationalAgent
- Add slash command handling functions:
  - '/' shows list of all available tools
  - '/help' shows general help about Randebu
  - '/<tool-name>' shows detailed help for specific tool
- Update frontend ChatInterface to detect '/' and show formatted help dropdown
- Add keyboard navigation (Arrow keys, Tab, Enter, Escape) for slash menu
2026-04-13 13:05:08 +00:00
7e03101e7b Merge pull request 'feat: Add AVE Cloud Skills as conversational agent tools (#56)' (#58) from fix/issue-56 into main 2026-04-13 14:56:33 +02:00
14 changed files with 3316 additions and 1125 deletions

230
README.md Normal file
View File

@@ -0,0 +1,230 @@
# Randebu
**Create Trading Bots Through Natural Conversation**
Randebu is a web-based platform that allows traders to create and manage automated trading bots through simple chat interactions. No coding required.
---
## The Problem
Trading bots like **OpenClaw** and similar platforms are powerful but come with a steep learning curve:
- Complex configuration files
- Requires understanding of trading strategies
- CLI-based interfaces
- Steep technical barrier for non-developers
## The Solution
Randebu lets you create trading bots by simply chatting:
> "Create a bot that buys PEPE when it drops 5% and sells when it profits 10%"
That's it. Randebu handles the rest.
---
## How It Works
1. **Chat** - Tell Randebu what you want in plain English
2. **Bot Created** - Randebu creates a configured trading bot
3. **Backtest** - Test your strategy with historical data
4. **Simulate** - Run a simulation with real-time data
5. **Deploy** - Activate your bot on the blockchain
---
## Built on AVE Cloud
Randebu is powered by **AVE Cloud Skills** - the same infrastructure used by professional trading teams.
### AVE Skills Currently Integrated
Randebu uses **AVE Cloud Skills** (the skill scripts from `ave-cloud-skill` repository) for data fetching:
| Skill Script | Command | Purpose | Line in agent.py |
|-------------|---------|---------|------------------|
| `ave_data_rest.py` | `trending` | Get trending tokens | 218 |
| `ave_data_rest.py` | `search` | Search tokens by keyword | 285 |
| `ave_data_rest.py` | `risk` | Honeypot/risk analysis | 367 |
| `ave_data_rest.py` | `token` | Get token details | 487 |
| `ave_data_rest.py` | `price` | Get token prices | 509 |
### AVE Integration Points
The AVE skills are called through `_call_ave_script()` in `agent.py`:
```python
# agent.py - Calling AVE skill scripts
def _call_ave_script(self, command: str, args: list) -> tuple[int, str]:
ave_skill_path = os.path.join(
repo_root, "ave-cloud-skill", "scripts", "ave_data_rest.py"
)
result = subprocess.run(
["python3", ave_skill_path, command] + args,
...
)
```
### Direct API Usage (Not Skills)
These components use the AVE API directly (not through skills):
- `backtest/engine.py` - Uses `AveCloudClient.get_klines()` for historical kline data
- `simulate/engine.py` - Uses `AveCloudClient.get_klines()` for real-time kline data
---
## Further AVE Integration Opportunities
### 1. Trading Execution (Priority: High)
- **AVE Skills**: `trade-chain-wallet`, `trade-proxy-wallet`
- **Use**: Execute trades directly from the bot (market orders, limit orders, TP/SL)
- **Status**: Not yet integrated - this is the next major feature
### 2. Real-Time Alerts (Priority: Medium)
- **AVE Skills**: WebSocket streams (`data-wss`)
- **Use**: Notify users when price hits targets
### 3. Portfolio Tracking (Priority: Medium)
- **AVE API**: `address/walletinfo` endpoint
- **Use**: Show user's complete portfolio across chains
### 4. Advanced Risk Analysis (Priority: Low)
- **AVE Skills**: Extended token analysis
- **Use**: More detailed honeypot detection, liquidity analysis
---
## Tech Stack
| Component | Technology |
|-----------|------------|
| Frontend | SvelteKit, TypeScript |
| Backend | FastAPI, Python |
| Database | PostgreSQL |
| AI | MiniMax (extended thinking) |
| Trading Data | AVE Cloud API |
---
## Future Development Plan
### Phase 1: Core MVP (Current)
- [x] Chat-based bot creation
- [x] Strategy configuration via conversation
- [x] Backtest historical data
- [x] Simulation with real-time data
- [x] Bot management (create, list, set)
### Phase 2: Trading Execution
- [ ] AVE Trading API integration
- [ ] Chain wallet support
- [ ] Proxy wallet (bot-managed) support
- [ ] TP/SL automation
### Phase 3: Advanced Features
- [ ] Portfolio dashboard
- [ ] Multi-chain support (Solana, Base, ETH)
- [ ] Copy trading (follow other traders)
- [ ] Strategy marketplace
### Phase 4: Platform Growth
- [ ] Strategy templates
- [ ] Community strategies
- [ ] Premium features (for fees)
---
## Business Opportunity
### Target Market
1. **Retail Traders** - People who want to automate trading but can't code
2. **Crypto Enthusiasts** - Active traders looking for easier tools
3. **Small Funds** - Need automation without expensive developers
### Revenue Model
| Tier | Price | Features |
|------|-------|----------|
| Free | $0 | 1 bot, 50 chats, basic features |
| Pro | $19/mo | Unlimited bots, backtests, simulations |
| Enterprise | Custom | API access, priority support, custom integrations |
### Competitive Advantage
- **No-code** - Unlike OpenClaw, 3Commas, Cryptohopper
- **Natural Language** - Describe strategy in plain English
- **AVE Integration** - Built on professional-grade infrastructure
- **Focused UX** - Simple, clean interface designed for beginners
### Market Size
- Crypto traders: 100M+ globally
- Trading bot market: $1.5B+ by 2027
- No-code platform market: Growing rapidly
---
## Getting Started
### Prerequisites
- Python 3.10+
- Node.js 18+
- PostgreSQL
### Installation
```bash
# Clone the repo
git clone https://github.com/shoko/randebu.git
cd randebu
# Setup backend
cd src/backend
pip install -r requirements.txt
# Setup frontend
cd ../frontend
npm install
# Configure environment
cp .env.example .env
# Edit .env with your API keys
# Run
# Backend: uvicorn main:app
# Frontend: npm run dev
```
### Configuration
Required environment variables:
- `MINIMAX_API_KEY` - For AI chat
- `AVE_API_KEY` - For trading data
- `DATABASE_URL` - PostgreSQL connection
---
## Contributing
Contributions welcome! Please read our contributing guidelines before submitting PRs.
---
## License
MIT
---
## Links
- Website: [randebu.com](https://randebu.com)
- AVE Cloud: [cloud.ave.ai](https://cloud.ave.ai)
- Hackathon: [clawhackathon.aveai.trade](https://clawhackathon.aveai.trade)
---
*Built with ❤️ for traders, by traders*

View File

@@ -16,7 +16,7 @@ from ..db.schemas import (
) )
from ..db.models import Bot, BotConversation, User from ..db.models import Bot, BotConversation, User
from ..services.ai_agent.crew import get_trading_crew from ..services.ai_agent.crew import get_trading_crew
from ..services.ai_agent.conversational import get_conversational_agent from ..services.ai_agent import get_conversational_agent
router = APIRouter() router = APIRouter()
MAX_BOTS_PER_USER = 3 MAX_BOTS_PER_USER = 3
@@ -224,7 +224,9 @@ def chat(
strategy_config=bot.strategy_config if result.get("strategy_updated") else None, strategy_config=bot.strategy_config if result.get("strategy_updated") else None,
success=result.get("success", False), success=result.get("success", False),
strategy_needs_confirmation=result.get("strategy_needs_confirmation", False), strategy_needs_confirmation=result.get("strategy_needs_confirmation", False),
strategy_data=result.get("strategy_data") if result.get("strategy_needs_confirmation") else None, strategy_data=result.get("strategy_data")
if result.get("strategy_needs_confirmation")
else None,
token_search_results=result.get("token_search_results"), token_search_results=result.get("token_search_results"),
) )

View File

@@ -0,0 +1,225 @@
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 ..services.auth import get_current_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_current_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_current_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_current_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 conversation.user_id != current_user.id:
raise HTTPException(status_code=403, detail="Access denied")
return conversation
@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_current_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 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_current_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 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,
message: str,
db: Session = Depends(get_db),
current_user: Optional[User] = Depends(get_current_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 conversation.user_id != current_user.id:
raise HTTPException(status_code=403, detail="Access denied")
warning = None
if not current_user:
RateLimiter.check_system_limit(db)
anon_token = get_or_create_anonymous_token(request, response, db)
anon = RateLimiter.check_anonymous_limit(db, anon_token)
RateLimiter.increment_chat_count(db, anon_token)
if anon and anon.chat_count > 40:
warning = "Your progress is not saved."
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:]
]
if not conversation.bot_id:
return {
"response": "No bot selected for this conversation. Please set a bot first.",
"thinking": None,
"strategy_config": None,
"success": False,
"warning": warning,
}
agent = get_conversational_agent(bot_id=conversation.bot_id)
result = agent.chat(message, history_for_agent)
assistant_content = result.get("response", "I couldn't process your request.")
user_msg = Message(
conversation_id=conversation_id,
role="user",
content=message,
)
db.add(user_msg)
assistant_msg = Message(
conversation_id=conversation_id,
role="assistant",
content=assistant_content,
)
db.add(assistant_msg)
conversation.updated_at = conversation.updated_at
db.commit()
return {
"response": assistant_content,
"thinking": result.get("thinking"),
"strategy_config": result.get("strategy_config"),
"success": result.get("success", False),
"warning": warning,
}

View File

@@ -10,6 +10,7 @@ from sqlalchemy import (
ForeignKey, ForeignKey,
Index, Index,
JSON, JSON,
Integer,
) )
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from ..core.database import Base from ..core.database import Base
@@ -30,6 +31,9 @@ class User(Base):
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
bots = relationship("Bot", back_populates="user", cascade="all, delete-orphan") bots = relationship("Bot", back_populates="user", cascade="all, delete-orphan")
conversations = relationship(
"Conversation", back_populates="user", cascade="all, delete-orphan"
)
class Bot(Base): class Bot(Base):
@@ -47,7 +51,7 @@ class Bot(Base):
user = relationship("User", back_populates="bots") user = relationship("User", back_populates="bots")
conversations = relationship( conversations = relationship(
"BotConversation", back_populates="bot", cascade="all, delete-orphan" "Conversation", back_populates="bot", cascade="all, delete-orphan"
) )
backtests = relationship( backtests = relationship(
"Backtest", back_populates="bot", cascade="all, delete-orphan" "Backtest", back_populates="bot", cascade="all, delete-orphan"
@@ -58,6 +62,47 @@ class Bot(Base):
signals = relationship("Signal", back_populates="bot", cascade="all, delete-orphan") 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): class BotConversation(Base):
__tablename__ = "bot_conversations" __tablename__ = "bot_conversations"
@@ -118,6 +163,9 @@ class Signal(Base):
Index("idx_bots_user_id", Bot.user_id) Index("idx_bots_user_id", Bot.user_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_conversations_bot_id", BotConversation.bot_id) Index("idx_conversations_bot_id", BotConversation.bot_id)
Index("idx_backtests_bot_id", Backtest.bot_id) Index("idx_backtests_bot_id", Backtest.bot_id)
Index("idx_simulations_bot_id", Simulation.bot_id) Index("idx_simulations_bot_id", Simulation.bot_id)

View File

@@ -242,3 +242,57 @@ class AveChainSwapRequest(BaseModel):
class AveChainSwapResponse(BaseModel): class AveChainSwapResponse(BaseModel):
swap: Optional[dict] = None swap: Optional[dict] = None
upsell_message: Optional[str] = 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

View File

@@ -4,7 +4,7 @@ from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from slowapi import Limiter from slowapi import Limiter
from slowapi.util import get_remote_address from slowapi.util import get_remote_address
from .api import auth, bots, backtest, simulate, config, ave from .api import auth, bots, backtest, simulate, config, ave, conversations
from .core.limiter import limiter from .core.limiter import limiter
from .core.database import engine, Base from .core.database import engine, Base
@@ -15,7 +15,17 @@ logger = logging.getLogger(__name__)
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
"""Initialize database on startup.""" """Initialize database on startup."""
# Import all models to ensure they're registered # Import all models to ensure they're registered
from .db.models import User, Bot, BotConversation, Backtest, Simulation, Signal from .db.models import (
User,
Bot,
BotConversation,
Backtest,
Simulation,
Signal,
Conversation,
Message,
AnonymousUser,
)
# Create tables if they don't exist # Create tables if they don't exist
Base.metadata.create_all(bind=engine) Base.metadata.create_all(bind=engine)
@@ -44,6 +54,9 @@ app.add_middleware(
app.include_router(auth.router, prefix="/api/auth", tags=["auth"]) app.include_router(auth.router, prefix="/api/auth", tags=["auth"])
app.include_router(bots.router, prefix="/api/bots", tags=["bots"]) app.include_router(bots.router, prefix="/api/bots", tags=["bots"])
app.include_router(
conversations.router, prefix="/api/conversations", tags=["conversations"]
)
app.include_router(backtest.router, prefix="/api", tags=["backtest"]) app.include_router(backtest.router, prefix="/api", tags=["backtest"])
app.include_router(simulate.router, prefix="/api", tags=["simulate"]) app.include_router(simulate.router, prefix="/api", tags=["simulate"])
app.include_router(config.router, prefix="/api/config", tags=["config"]) app.include_router(config.router, prefix="/api/config", tags=["config"])

View File

@@ -1,4 +1,29 @@
"""AI Agent module for conversational trading."""
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 .crew import TradingCrew, get_trading_crew
from .llm_connector import MiniMaxLLM, MiniMaxConnector from .llm_connector import MiniMaxLLM, MiniMaxConnector
__all__ = ["TradingCrew", "get_trading_crew", "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",
]

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,308 @@
"""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"],
},
},
},
]
SYSTEM_PROMPT_WITH_TOOLS = (
SYSTEM_PROMPT
+ """
You have access to tools:
- search_tokens(keyword, limit): Search for tokens by keyword. Use it when user asks to search for a token or find tokens by name/symbol.
- get_token(address, chain): Get detailed information about a specific token. Use when user asks for token details.
- get_price(token_ids): Get current price(s) for tokens. Use when user asks for token price.
- get_risk(address, chain): Get risk analysis for a token. Use when user asks about token safety or honeypot analysis.
- get_trending(chain, limit): Get trending tokens on a blockchain. Use when user asks what's trending, top tokens, or popular tokens.
- run_backtest(token_address, timeframe, start_date, end_date): Run a backtest on historical data. Returns performance metrics. Use when user asks to backtest or check historical performance.
- manage_simulation(action, token_address, kline_interval): Manage trading simulations. Actions: 'start' (begin new), 'stop' (stop running), 'status' (check if running), 'results' (get current/latest results).
When you want to use a tool, respond with:
{
"thinking": "...",
"response": "Running backtest...",
"tool_call": {"name": "run_backtest", "arguments": {"token_address": "0x...", "timeframe": "1d", "start_date": "2024-01-01", "end_date": "2024-12-01"}}
}
"""
)
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 = 2000,
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)
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

File diff suppressed because it is too large Load Diff

View 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"
"""

View 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

View 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()

View File

@@ -3,6 +3,35 @@
import type { ChatMessage } from '$lib/stores/chatStore'; import type { ChatMessage } from '$lib/stores/chatStore';
import { parseMarkdown, parseInlineElements, type InlineSegment } from '$lib/utils/markdown'; import { parseMarkdown, parseInlineElements, type InlineSegment } from '$lib/utils/markdown';
interface ToolItem {
name: string;
description: string;
command: string;
}
const TOOLS: { category: string; label: string; tools: ToolItem[] }[] = [
{
category: 'randebu',
label: '🤖 Randebu Built-in',
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',
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 { interface Props {
bot: Bot | null; bot: Bot | null;
messages: ChatMessage[]; messages: ChatMessage[];
@@ -26,9 +55,16 @@
let messageInput = $state(''); let messageInput = $state('');
let chatContainer: HTMLDivElement; let chatContainer: HTMLDivElement;
let expandedThinking: Record<string, boolean> = $state({}); 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
let filteredTools = $derived(messageInput.startsWith('/') ? TOOLS.flatMap(t => t.tools).filter(tool => tool.name.toLowerCase().startsWith(messageInput.slice(1).toLowerCase()) || tool.command.toLowerCase().startsWith(messageInput.slice(1).toLowerCase())) : []);
function handleSend() { function handleSend() {
if (!messageInput.trim()) return; if (!messageInput.trim()) return;
showSlashMenu = false;
onSendMessage(messageInput); onSendMessage(messageInput);
messageInput = ''; messageInput = '';
} }
@@ -36,8 +72,55 @@
function handleKeydown(e: KeyboardEvent) { function handleKeydown(e: KeyboardEvent) {
if (e.key === 'Enter' && !e.shiftKey) { if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault(); e.preventDefault();
if (showSlashMenu && filteredTools.length > 0) {
selectTool(filteredTools[selectedIndex]);
} else {
handleSend(); 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) { function handleBotChange(e: Event) {
@@ -74,8 +157,17 @@
} }
}).join(''); }).join('');
} }
function handleClickOutside(e: MouseEvent) {
const target = e.target as HTMLElement;
if (!target.closest('.slash-menu') && !target.closest('.input-container textarea')) {
showSlashMenu = false;
}
}
</script> </script>
<svelte:window on:click={handleClickOutside} />
<div class="chat-interface"> <div class="chat-interface">
{#if showBotSelector && availableBots.length > 0} {#if showBotSelector && availableBots.length > 0}
<div class="bot-selector"> <div class="bot-selector">
@@ -215,10 +307,32 @@
{#if bot} {#if bot}
<div class="input-container"> <div class="input-container">
{#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 TOOLS 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">Press Tab to select, Enter to send</div>
</div>
{/if}
<textarea <textarea
bind:value={messageInput} value={messageInput}
oninput={handleInput}
onkeydown={handleKeydown} onkeydown={handleKeydown}
placeholder="Describe your trading strategy..." placeholder="Describe your trading strategy... (or type / for commands)"
rows="1" rows="1"
></textarea> ></textarea>
<button onclick={handleSend}> <button onclick={handleSend}>
@@ -555,4 +669,76 @@
opacity: 0.5; opacity: 0.5;
cursor: not-allowed; 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> </style>