Files
trading_bot_v3/app/api/analyze/route.ts
root b2d02cd716 Improve TradingView layout loading with better debugging and selection logic
- Added comprehensive layout menu item detection with multiple selectors
- Implemented debug screenshots for layout menu and after layout changes
- Added better error handling and logging for layout selection
- Improved text matching with exact and partial match strategies
- Added fallback comprehensive search with direct click functionality
- Fixed TypeScript issues with element handle clicking
2025-07-09 14:45:04 +02:00

55 lines
1.9 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { aiAnalysisService } from '../../../lib/ai-analysis'
import { tradingViewCapture } from '../../../lib/tradingview'
import { settingsManager } from '../../../lib/settings'
import path from 'path'
export async function POST(req: NextRequest) {
try {
const { symbol, layouts, timeframe } = await req.json()
// Load current settings
const settings = await settingsManager.loadSettings()
// Use provided values or fall back to saved settings
const finalSymbol = symbol || settings.symbol
const finalTimeframe = timeframe || settings.timeframe
const finalLayouts = layouts || settings.layouts
if (!finalSymbol) {
return NextResponse.json({ error: 'Missing symbol' }, { status: 400 })
}
const baseFilename = `${finalSymbol}_${finalTimeframe}_${Date.now()}`
const screenshots = await tradingViewCapture.capture(finalSymbol, `${baseFilename}.png`, finalLayouts, finalTimeframe)
let result
if (screenshots.length === 1) {
// Single screenshot analysis
const filename = path.basename(screenshots[0])
result = await aiAnalysisService.analyzeScreenshot(filename)
} else {
// Multiple screenshots analysis
const filenames = screenshots.map(screenshot => path.basename(screenshot))
result = await aiAnalysisService.analyzeMultipleScreenshots(filenames)
}
if (!result) {
return NextResponse.json({ error: 'Analysis failed' }, { status: 500 })
}
return NextResponse.json({
...result,
layoutsAnalyzed: finalLayouts,
settings: {
symbol: finalSymbol,
timeframe: finalTimeframe,
layouts: finalLayouts
},
screenshots: screenshots.map(s => path.basename(s))
})
} catch (e: any) {
return NextResponse.json({ error: e.message }, { status: 500 })
}
}