fix: timeframe handling and progress tracking improvements
- Fix timeframe parameter handling in enhanced-screenshot API route - Support both 'timeframe' (singular) and 'timeframes' (array) parameters - Add proper sessionId propagation for real-time progress tracking - Enhance MACD analysis prompt with detailed crossover definitions - Add progress tracker service with Server-Sent Events support - Fix Next.js build errors in chart components (module variable conflicts) - Change dev environment port from 9000:3000 to 9001:3000 - Improve AI analysis layout detection logic - Add comprehensive progress tracking through all service layers
This commit is contained in:
@@ -1,19 +1,70 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { enhancedScreenshotService } from '../../../lib/enhanced-screenshot-simple'
|
||||
import { enhancedScreenshotService } from '../../../lib/enhanced-screenshot'
|
||||
import { aiAnalysisService } from '../../../lib/ai-analysis'
|
||||
import { progressTracker } from '../../../lib/progress-tracker'
|
||||
|
||||
export async function POST(request) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
const { symbol, layouts, timeframes, selectedLayouts, analyze = true } = body
|
||||
const { symbol, layouts, timeframe, timeframes, selectedLayouts, analyze = true } = body
|
||||
|
||||
console.log('📊 Enhanced screenshot request:', { symbol, layouts, timeframes, selectedLayouts })
|
||||
console.log('📊 Enhanced screenshot request:', { symbol, layouts, timeframe, timeframes, selectedLayouts })
|
||||
|
||||
// Generate unique session ID for progress tracking
|
||||
const sessionId = `analysis_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
|
||||
console.log('🔍 Created session ID:', sessionId)
|
||||
|
||||
// Create progress tracking session with initial steps
|
||||
const initialSteps = [
|
||||
{
|
||||
id: 'init',
|
||||
title: 'Initializing Analysis',
|
||||
description: 'Starting AI-powered trading analysis...',
|
||||
status: 'pending'
|
||||
},
|
||||
{
|
||||
id: 'auth',
|
||||
title: 'TradingView Authentication',
|
||||
description: 'Logging into TradingView accounts',
|
||||
status: 'pending'
|
||||
},
|
||||
{
|
||||
id: 'navigation',
|
||||
title: 'Chart Navigation',
|
||||
description: 'Navigating to chart layouts',
|
||||
status: 'pending'
|
||||
},
|
||||
{
|
||||
id: 'loading',
|
||||
title: 'Chart Data Loading',
|
||||
description: 'Waiting for chart data and indicators',
|
||||
status: 'pending'
|
||||
},
|
||||
{
|
||||
id: 'capture',
|
||||
title: 'Screenshot Capture',
|
||||
description: 'Capturing high-quality screenshots',
|
||||
status: 'pending'
|
||||
},
|
||||
{
|
||||
id: 'analysis',
|
||||
title: 'AI Analysis',
|
||||
description: 'Analyzing screenshots with AI',
|
||||
status: 'pending'
|
||||
}
|
||||
]
|
||||
|
||||
// Create the progress session
|
||||
console.log('🔍 Creating progress session with steps:', initialSteps.length)
|
||||
progressTracker.createSession(sessionId, initialSteps)
|
||||
console.log('🔍 Progress session created successfully')
|
||||
|
||||
// Prepare configuration for screenshot service
|
||||
const config = {
|
||||
symbol: symbol || 'BTCUSD',
|
||||
timeframe: timeframes?.[0] || '60', // Use first timeframe for now
|
||||
timeframe: timeframe || timeframes?.[0] || '60', // Use single timeframe, fallback to first of array, then default
|
||||
layouts: layouts || selectedLayouts || ['ai'],
|
||||
sessionId, // Pass session ID for progress tracking
|
||||
credentials: {
|
||||
email: process.env.TRADINGVIEW_EMAIL,
|
||||
password: process.env.TRADINGVIEW_PASSWORD
|
||||
@@ -22,35 +73,32 @@ export async function POST(request) {
|
||||
|
||||
console.log('🔧 Using config:', config)
|
||||
|
||||
// Capture screenshots using the working service
|
||||
const screenshots = await enhancedScreenshotService.captureWithLogin(config)
|
||||
console.log('📸 Screenshots captured:', screenshots)
|
||||
|
||||
let screenshots = []
|
||||
let analysis = null
|
||||
|
||||
// Perform AI analysis if requested and screenshots were captured
|
||||
if (analyze && screenshots.length > 0) {
|
||||
// Perform AI analysis if requested
|
||||
if (analyze) {
|
||||
try {
|
||||
console.log('🤖 Starting AI analysis...')
|
||||
|
||||
// Extract just the filenames from full paths
|
||||
const filenames = screenshots.map(path => path.split('/').pop())
|
||||
|
||||
if (filenames.length === 1) {
|
||||
analysis = await aiAnalysisService.analyzeScreenshot(filenames[0])
|
||||
} else {
|
||||
analysis = await aiAnalysisService.analyzeMultipleScreenshots(filenames)
|
||||
}
|
||||
|
||||
console.log('✅ AI analysis completed')
|
||||
console.log('🤖 Starting automated capture and analysis...')
|
||||
const result = await aiAnalysisService.captureAndAnalyzeWithConfig(config, sessionId)
|
||||
screenshots = result.screenshots
|
||||
analysis = result.analysis
|
||||
console.log('✅ Automated capture and analysis completed')
|
||||
} catch (analysisError) {
|
||||
console.error('❌ AI analysis failed:', analysisError)
|
||||
// Continue without analysis rather than failing the whole request
|
||||
console.error('❌ Automated capture and analysis failed:', analysisError)
|
||||
// Fall back to screenshot only
|
||||
screenshots = await enhancedScreenshotService.captureWithLogin(config, sessionId)
|
||||
}
|
||||
} else {
|
||||
// Capture screenshots only
|
||||
screenshots = await enhancedScreenshotService.captureWithLogin(config, sessionId)
|
||||
}
|
||||
|
||||
console.log('📸 Final screenshots:', screenshots)
|
||||
|
||||
const result = {
|
||||
success: true,
|
||||
sessionId, // Return session ID for progress tracking
|
||||
timestamp: Date.now(),
|
||||
symbol: config.symbol,
|
||||
layouts: config.layouts,
|
||||
|
||||
Reference in New Issue
Block a user