diff --git a/app/api/automation/learning-status/route.js b/app/api/automation/learning-status/route.js index c5358ac..e28b8a1 100644 --- a/app/api/automation/learning-status/route.js +++ b/app/api/automation/learning-status/route.js @@ -5,7 +5,7 @@ import { NextResponse } from 'next/server'; async function getAutomationInstance() { try { // Import the singleton automation instance - const { getAutomationInstance } = await import('../../../lib/automation-singleton.js'); + const { getAutomationInstance } = await import('../../../../lib/automation-singleton.js'); return getAutomationInstance(); } catch (error) { console.error('❌ Could not get automation instance:', error); @@ -44,9 +44,9 @@ export async function GET() { success: true, learningSystem: { ...learningStatus, - automationRunning: automationStatus.isRunning, - totalCycles: automationStatus.stats.totalCycles, - totalTrades: automationStatus.stats.totalTrades + automationRunning: automationStatus.isActive || automationStatus.isRunning, + totalCycles: automationStatus.totalCycles || automationStatus.stats?.totalCycles || 0, + totalTrades: automationStatus.totalTrades || automationStatus.stats?.totalTrades || 0 }, visibility: { decisionTrackingActive: learningStatus.activeDecisions > 0, diff --git a/app/api/automation/start/route.js b/app/api/automation/start/route.js index 611829d..3337434 100644 --- a/app/api/automation/start/route.js +++ b/app/api/automation/start/route.js @@ -3,7 +3,7 @@ import { NextResponse } from 'next/server'; // Import singleton automation manager async function getAutomationInstance() { try { - const { getAutomationInstance } = await import('../../../lib/automation-singleton.js'); + const { getAutomationInstance } = await import('../../../../lib/automation-singleton.js'); return await getAutomationInstance(); } catch (error) { console.error('❌ Could not get automation instance:', error); diff --git a/app/api/automation/status/route.js b/app/api/automation/status/route.js index fa62b68..a3f2d72 100644 --- a/app/api/automation/status/route.js +++ b/app/api/automation/status/route.js @@ -3,7 +3,7 @@ import { NextResponse } from 'next/server'; // Import singleton automation manager async function getAutomationInstance() { try { - const { getAutomationInstance } = await import('../../../lib/automation-singleton.js'); + const { getAutomationInstance } = await import('../../../../lib/automation-singleton.js'); return await getAutomationInstance(); } catch (error) { console.error('❌ Could not get automation instance:', error); diff --git a/app/api/automation/stop/route.js b/app/api/automation/stop/route.js index 4288055..ef07965 100644 --- a/app/api/automation/stop/route.js +++ b/app/api/automation/stop/route.js @@ -1,7 +1,7 @@ // Import singleton automation manager async function getAutomationInstance() { try { - const { getAutomationInstance } = await import('../../../lib/automation-singleton.js'); + const { getAutomationInstance } = await import('../../../../lib/automation-singleton.js'); return await getAutomationInstance(); } catch (error) { console.error('❌ Could not get automation instance:', error); diff --git a/components/EnhancedAILearningPanel.tsx b/components/EnhancedAILearningPanel.tsx index 108ff75..ff03cdd 100644 --- a/components/EnhancedAILearningPanel.tsx +++ b/components/EnhancedAILearningPanel.tsx @@ -17,7 +17,11 @@ interface LearningData { thresholds?: any; confidenceLevel?: number; }; - recommendations?: string[]; + recommendations?: Array<{ + type: string; + message: string; + priority: string; + } | string>; }; }; visibility?: { @@ -47,15 +51,43 @@ const EnhancedAILearningPanel = () => { const learningData = await learningResponse.json(); const statusData = await statusResponse.json(); - setLearningData({ - ...learningData, + // Ensure we have a proper data structure even if APIs return errors + const safeData = { + learningSystem: learningData.learningSystem || { + enabled: false, + message: learningData.message || 'Learning system not available', + activeDecisions: 0 + }, + visibility: learningData.visibility || { + decisionTrackingActive: false, + learningDatabaseConnected: false, + aiEnhancementsActive: false, + lastUpdateTime: new Date().toISOString() + }, automationStatus: statusData - }); + }; + setLearningData(safeData); setError(null); } catch (err) { console.error('Error fetching learning status:', err); setError(err instanceof Error ? err.message : 'Unknown error'); + + // Set default data structure on error + setLearningData({ + learningSystem: { + enabled: false, + message: 'Failed to fetch learning status', + activeDecisions: 0 + }, + visibility: { + decisionTrackingActive: false, + learningDatabaseConnected: false, + aiEnhancementsActive: false, + lastUpdateTime: new Date().toISOString() + }, + automationStatus: null + }); } finally { setLoading(false); } @@ -109,8 +141,23 @@ const EnhancedAILearningPanel = () => { const { learningSystem, visibility } = learningData; + // Safety check for learningSystem + if (!learningSystem) { + return ( +
+
+
+

+ 🧠 AI Learning System +

+
+
Loading learning system data...
+
+ ); + } + const renderLearningStatus = () => { - if (!learningSystem.enabled) { + if (!learningSystem || !learningSystem.enabled) { return (
@@ -120,9 +167,9 @@ const EnhancedAILearningPanel = () => {
- {learningSystem.message || 'The AI learning system is not currently integrated with the automation.'} + {learningSystem?.message || 'The AI learning system is not currently integrated with the automation.'}
- {learningSystem.recommendation && ( + {learningSystem?.recommendation && (
💡 {learningSystem.recommendation}
@@ -145,7 +192,7 @@ const EnhancedAILearningPanel = () => { AI Learning Active
- {learningSystem.report && ( + {learningSystem?.report && (
Total Decisions
@@ -192,15 +239,15 @@ const EnhancedAILearningPanel = () => {
-
0 ? 'bg-blue-500' : 'bg-gray-500'}`}>
- 0 ? 'text-blue-400' : 'text-gray-400'}> - Active Decisions ({learningSystem.activeDecisions || 0}) +
0 ? 'bg-blue-500' : 'bg-gray-500'}`}>
+ 0 ? 'text-blue-400' : 'text-gray-400'}> + Active Decisions ({learningSystem?.activeDecisions || 0})
- {learningSystem.report?.insights && ( + {learningSystem?.report?.insights && (
🎯 Learning Insights
@@ -214,13 +261,13 @@ const EnhancedAILearningPanel = () => {
)} - {learningSystem.report?.recommendations && learningSystem.report.recommendations.length > 0 && ( + {learningSystem?.report?.recommendations && learningSystem.report.recommendations.length > 0 && (
💡 AI Recommendations
- {learningSystem.report.recommendations.map((rec: string, index: number) => ( + {learningSystem.report.recommendations.map((rec: any, index: number) => (
- • {rec} + • {typeof rec === 'string' ? rec : rec.message || rec.type || 'No message'}
))}
@@ -234,7 +281,7 @@ const EnhancedAILearningPanel = () => {
-
+

🧠 AI Learning System

diff --git a/lib/automation-singleton.js b/lib/automation-singleton.js index 1c6f605..7e208c5 100644 --- a/lib/automation-singleton.js +++ b/lib/automation-singleton.js @@ -4,7 +4,7 @@ let automationInstance = null; async function createAutomationInstance() { try { // Try to import the learning-enhanced automation first - const AutomationWithLearning = require('./automation-with-learning-v2.js'); + const AutomationWithLearning = (await import('./automation-with-learning-v2.js')).default; console.log('✅ Creating automation instance with AI learning system'); return new AutomationWithLearning(); } catch (error) { @@ -23,24 +23,24 @@ async function createAutomationInstance() { } } -function getAutomationInstance() { +async function getAutomationInstance() { if (!automationInstance) { - automationInstance = createAutomationInstance(); + automationInstance = await createAutomationInstance(); } return automationInstance; } -function resetAutomationInstance() { +async function resetAutomationInstance() { if (automationInstance) { console.log('🔄 Resetting automation instance'); if (typeof automationInstance.stop === 'function') { - automationInstance.stop(); + await automationInstance.stop(); } } automationInstance = null; } -module.exports = { +export { getAutomationInstance, resetAutomationInstance }; diff --git a/lib/automation-with-learning-v2.js b/lib/automation-with-learning-v2.js index a20e622..385ecef 100644 --- a/lib/automation-with-learning-v2.js +++ b/lib/automation-with-learning-v2.js @@ -445,4 +445,4 @@ class AutomationWithLearning { } } -module.exports = AutomationWithLearning; +export default AutomationWithLearning; diff --git a/lib/db.js b/lib/db.js index d44d2e4..5be8f47 100644 --- a/lib/db.js +++ b/lib/db.js @@ -1,46 +1,12 @@ -/** - * Database utility for Prisma client - * - * Provides a global Prisma instance to avoid connection issues - */ +import { PrismaClient } from '@prisma/client'; -const { PrismaClient } = require('@prisma/client'); +let prisma; -// Global Prisma instance -let prisma = null; - -/** - * Get the global Prisma database instance - */ -async function getDB() { +function getDB() { if (!prisma) { prisma = new PrismaClient(); - - // Test the connection - try { - await prisma.$connect(); - console.log('✅ Database connection established'); - } catch (error) { - console.error('❌ Database connection failed:', error.message); - throw error; - } } - return prisma; } -/** - * Close the database connection - */ -async function closeDB() { - if (prisma) { - await prisma.$disconnect(); - prisma = null; - console.log('✅ Database connection closed'); - } -} - -module.exports = { - getDB, - closeDB -}; +export { getDB }; \ No newline at end of file diff --git a/lib/simplified-stop-loss-learner-fixed.js b/lib/simplified-stop-loss-learner-fixed.js index 2072655..5d1edde 100644 --- a/lib/simplified-stop-loss-learner-fixed.js +++ b/lib/simplified-stop-loss-learner-fixed.js @@ -5,8 +5,8 @@ * without complex statistical analysis. */ -const { PrismaClient } = require('@prisma/client'); -const getDB = require('./database-util'); +import { PrismaClient } from '@prisma/client'; +import { getDB } from './db.js'; class SimplifiedStopLossLearner { constructor() { @@ -527,4 +527,4 @@ class SimplifiedStopLossLearner { } } -module.exports = SimplifiedStopLossLearner; +export { SimplifiedStopLossLearner }; diff --git a/prisma/prisma/dev.db b/prisma/prisma/dev.db index a12c0ec..7a43a44 100644 Binary files a/prisma/prisma/dev.db and b/prisma/prisma/dev.db differ