diff --git a/components/TradeFollowUpPanel.tsx b/components/TradeFollowUpPanel.tsx index 0273938..d393f23 100644 --- a/components/TradeFollowUpPanel.tsx +++ b/components/TradeFollowUpPanel.tsx @@ -39,6 +39,7 @@ export default function TradeFollowUpPanel({ onClose }: TradeFollowUpPanelProps) const [currentMessage, setCurrentMessage] = useState('') const [isLoading, setIsLoading] = useState(false) const [isAnalyzing, setIsAnalyzing] = useState(false) + const [systemStatus, setSystemStatus] = useState(null) const chatEndRef = useRef(null) // Auto-scroll to bottom of chat @@ -48,9 +49,35 @@ export default function TradeFollowUpPanel({ onClose }: TradeFollowUpPanelProps) // Load active positions on mount useEffect(() => { - loadActivePositions() + const initializePanel = async () => { + await loadSystemStatus() + await loadActivePositions() + } + initializePanel() }, []) + const loadSystemStatus = async () => { + try { + // Test multiple endpoints to get status + const [statusRes, walletRes, healthRes] = await Promise.allSettled([ + fetch('/api/status'), + fetch('/api/wallet/balance'), + fetch('/api/trading/health') + ]) + + const systemInfo = { + api: statusRes.status === 'fulfilled', + wallet: walletRes.status === 'fulfilled', + trading: healthRes.status === 'fulfilled', + timestamp: new Date().toLocaleTimeString() + } + + setSystemStatus(systemInfo) + } catch (error) { + console.error('Error loading system status:', error) + } + } + const loadActivePositions = async () => { try { const response = await fetch('/api/trading/positions') @@ -61,18 +88,26 @@ export default function TradeFollowUpPanel({ onClose }: TradeFollowUpPanelProps) // TODO: Add position selector if multiple positions setActivePosition(data.positions[0]) - // Add welcome message + // Add welcome message with system status + const statusEmoji = systemStatus ? + `🟢 API ${systemStatus.api ? 'āœ“' : 'āœ—'} | šŸ’° Wallet ${systemStatus.wallet ? 'āœ“' : 'āœ—'} | šŸ“ˆ Trading ${systemStatus.trading ? 'āœ“' : 'āœ—'}` : + 'šŸ”„ Loading system status...' + setChatMessages([{ id: Date.now().toString(), type: 'system', - content: `šŸŽÆ **Trade Follow-up Assistant**\n\nI'm here to help you manage your active ${data.positions[0].symbol} ${data.positions[0].side} position.\n\n**Current Position:**\n• Entry: $${data.positions[0].entryPrice}\n• Size: ${data.positions[0].amount}\n• Current P&L: ${data.positions[0].unrealizedPnl > 0 ? '+' : ''}$${data.positions[0].unrealizedPnl.toFixed(2)}\n\nAsk me anything about your trade!`, + content: `šŸŽÆ **Trade Follow-up Assistant**\n\n**System Status (${systemStatus?.timestamp || 'Loading...'})**\n${statusEmoji}\n\nI'm here to help you manage your active ${data.positions[0].symbol} ${data.positions[0].side} position.\n\n**Current Position:**\n• Entry: $${data.positions[0].entryPrice}\n• Size: ${data.positions[0].amount}\n• Current P&L: ${data.positions[0].unrealizedPnl > 0 ? '+' : ''}$${data.positions[0].unrealizedPnl.toFixed(2)}\n\n**Available Commands:**\n• "status" - System and trading status\n• "analysis" - Fresh chart analysis\n• "risk" - Risk assessment\n• "exit" - Exit strategy\n\nAsk me anything about your trade!`, timestamp: new Date().toISOString() }]) } else { + const statusEmoji = systemStatus ? + `🟢 API ${systemStatus.api ? 'āœ“' : 'āœ—'} | šŸ’° Wallet ${systemStatus.wallet ? 'āœ“' : 'āœ—'} | šŸ“ˆ Trading ${systemStatus.trading ? 'āœ“' : 'āœ—'}` : + 'šŸ”„ Loading system status...' + setChatMessages([{ id: Date.now().toString(), type: 'system', - content: 'āš ļø **No Active Positions Found**\n\nI don\'t see any active positions to analyze. Please enter a trade first, then come back for follow-up analysis.', + content: `āš ļø **No Active Positions Found**\n\n**System Status (${systemStatus?.timestamp || 'Loading...'})**\n${statusEmoji}\n\nI don't see any active positions to analyze. Please:\n1. Enter a trade first\n2. Mark it as traded using the blue button\n3. Come back for follow-up analysis\n\nOr type "status" for detailed system information.`, timestamp: new Date().toISOString() }]) } @@ -81,7 +116,7 @@ export default function TradeFollowUpPanel({ onClose }: TradeFollowUpPanelProps) setChatMessages([{ id: Date.now().toString(), type: 'system', - content: 'āŒ **Error Loading Positions**\n\nCouldn\'t load your active positions. Please try again.', + content: `āŒ **Error Loading Positions**\n\n**System Status:** ${systemStatus ? `API ${systemStatus.api ? 'āœ“' : 'āœ—'} Wallet ${systemStatus.wallet ? 'āœ“' : 'āœ—'} Trading ${systemStatus.trading ? 'āœ“' : 'āœ—'}` : 'Unknown'}\n\nCouldn't load your active positions. Please check:\n• Docker container is running (port 9001)\n• API endpoints are responding\n• Database connection is working\n\nType "status" for detailed diagnostics.`, timestamp: new Date().toISOString() }]) } @@ -102,6 +137,27 @@ export default function TradeFollowUpPanel({ onClose }: TradeFollowUpPanelProps) setIsLoading(true) try { + // Check if user is asking for status or system information + const isStatusRequest = currentMessage.toLowerCase().includes('status') || + currentMessage.toLowerCase().includes('system') || + currentMessage.toLowerCase().includes('health') || + currentMessage.toLowerCase().includes('diagnostic') + + if (isStatusRequest) { + // Refresh system status and provide detailed information + await loadSystemStatus() + + const statusMessage: ChatMessage = { + id: (Date.now() + 1).toString(), + type: 'assistant', + content: `šŸ“Š **System Status Report** (${new Date().toLocaleTimeString()})\n\n**🐳 Docker Container:**\n• Status: ${systemStatus?.api ? '🟢 Running' : 'šŸ”“ Issues detected'}\n• Port: 9001 → 3000\n• Health: ${systemStatus?.api ? 'Healthy' : 'Unhealthy'}\n\n**🌐 API Services:**\n• Main API: ${systemStatus?.api ? '🟢 Online' : 'šŸ”“ Offline'}\n• Wallet API: ${systemStatus?.wallet ? '🟢 Connected' : 'šŸ”“ Disconnected'}\n• Trading API: ${systemStatus?.trading ? '🟢 Active' : 'šŸ”“ Inactive'}\n\n**šŸ“ˆ Trading Services:**\n• Drift Protocol: ${systemStatus?.trading ? '🟢 Connected' : 'šŸ”“ Not connected'}\n• Jupiter DEX: ${systemStatus?.trading ? '🟢 Available' : 'šŸ”“ Unavailable'}\n• Screenshot Service: ${systemStatus?.api ? '🟢 Ready' : 'šŸ”“ Not ready'}\n\n**🧠 AI Services:**\n• OpenAI GPT-4o mini: ${process.env.OPENAI_API_KEY ? '🟢 Configured' : 'šŸ”“ Not configured'}\n• Analysis Engine: ${systemStatus?.api ? '🟢 Ready' : 'šŸ”“ Not ready'}\n\n**šŸ’¾ Database:**\n• SQLite: 🟢 Connected\n• Position Tracking: ${activePosition ? '🟢 Active position found' : '🟔 No active positions'}\n\n${!activePosition ? '**šŸ’” Tip:** Execute a trade and mark it as traded to enable full follow-up features!' : '**āœ… Ready:** All systems operational for trade follow-up!'}`, + timestamp: new Date().toISOString() + } + setChatMessages(prev => [...prev, statusMessage]) + setIsLoading(false) + return + } + // Check if user is asking for updated analysis const needsScreenshot = currentMessage.toLowerCase().includes('analysis') || currentMessage.toLowerCase().includes('update') || @@ -206,9 +262,9 @@ export default function TradeFollowUpPanel({ onClose }: TradeFollowUpPanelProps) } const quickActions = [ + "status", "Should I exit now?", - "Update my stop loss", - "Move to break even", + "Update my stop loss", "Current market analysis", "Risk assessment", "Take profit strategy" @@ -225,19 +281,30 @@ export default function TradeFollowUpPanel({ onClose }: TradeFollowUpPanelProps)

Trade Follow-up Assistant

- {activePosition && ( + {activePosition ? (

{activePosition.symbol} {activePosition.side} • Entry: ${activePosition.entryPrice}

+ ) : ( +

No active positions

)}
- +
+ {/* System Status Indicator */} +
+
+ + {systemStatus?.api && systemStatus?.wallet && systemStatus?.trading ? 'All Systems' : 'System Issues'} + +
+ +
{/* Chat Messages */}