mirror of
https://github.com/thecyberlearn/quantum-ai-v2.git
synced 2026-08-18 21:32:59 +00:00
Fix Weather Reporter network errors for API-based agent
Fix data format and processing pattern for Weather Reporter: - Change backend from json.loads() to request.POST.dict() for FormData handling - Update frontend URL from window.location.href to /process/ endpoint - Implement immediate response pattern for API-based agent (no polling needed) - Return complete weather data directly in POST response - Weather Reporter uses OpenWeatherMap API directly, unlike webhook agents Key difference: API agents process immediately, webhook agents use async polling 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
07d61dbf63
commit
82fc051370
@ -634,7 +634,7 @@
|
|||||||
// Submit form via AJAX
|
// Submit form via AJAX
|
||||||
const formData = new FormData(this);
|
const formData = new FormData(this);
|
||||||
|
|
||||||
fetch(window.location.href, {
|
fetch('/agents/weather-reporter/process/', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: formData,
|
body: formData,
|
||||||
headers: {
|
headers: {
|
||||||
@ -644,20 +644,17 @@
|
|||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(result => {
|
.then(result => {
|
||||||
clearInterval(stepInterval);
|
clearInterval(stepInterval);
|
||||||
if (result.success && result.request_id) {
|
// Handle immediate response (API-based agent)
|
||||||
// Start polling for results
|
|
||||||
pollForResults(result.request_id);
|
|
||||||
} else {
|
|
||||||
// Handle immediate response
|
|
||||||
document.getElementById('processingStatus').style.display = 'none';
|
document.getElementById('processingStatus').style.display = 'none';
|
||||||
document.getElementById('processButton').disabled = false;
|
document.getElementById('processButton').disabled = false;
|
||||||
document.getElementById('processButton').innerHTML = '🌤️ Get Weather Report (2.00 AED)';
|
document.getElementById('processButton').innerHTML = '🌤️ Get Weather Report (2.00 AED)';
|
||||||
|
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
showToast(`❌ ${result.error}`, 'error');
|
showToast(`❌ ${result.error}`, 'error');
|
||||||
} else {
|
} else if (result.success) {
|
||||||
displayResults(result);
|
displayResults(result);
|
||||||
}
|
} else {
|
||||||
|
showToast('❌ Failed to generate weather report', 'error');
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
|
|||||||
@ -41,7 +41,8 @@ class WeatherReporterProcessView(View):
|
|||||||
return JsonResponse({'error': 'Authentication required'}, status=401)
|
return JsonResponse({'error': 'Authentication required'}, status=401)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = json.loads(request.body)
|
# Handle FormData from frontend
|
||||||
|
data = request.POST.dict()
|
||||||
|
|
||||||
# Get agent
|
# Get agent
|
||||||
agent = BaseAgent.objects.get(slug='weather-reporter')
|
agent = BaseAgent.objects.get(slug='weather-reporter')
|
||||||
@ -60,23 +61,40 @@ class WeatherReporterProcessView(View):
|
|||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Process request
|
# Process request immediately (API-based agent)
|
||||||
processor = WeatherReporterProcessor()
|
processor = WeatherReporterProcessor()
|
||||||
result = processor.process_request(
|
result = processor.process_request(
|
||||||
request_obj=agent_request,
|
request_obj=agent_request,
|
||||||
user_id=request.user.id,
|
user_id=request.user.id,
|
||||||
location=data.get('location'),
|
location=data.get('location'),
|
||||||
report_type=data.get('report_type'),
|
report_type=data.get('report_type'),
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Refresh user from database to get updated wallet balance
|
# Refresh user from database to get updated wallet balance
|
||||||
request.user.refresh_from_db()
|
request.user.refresh_from_db()
|
||||||
|
|
||||||
|
# Check if we have a response object
|
||||||
|
if hasattr(agent_request, 'response'):
|
||||||
|
response_obj = agent_request.response
|
||||||
|
return JsonResponse({
|
||||||
|
'success': response_obj.success,
|
||||||
|
'status': 'completed',
|
||||||
|
'content': response_obj.formatted_report,
|
||||||
|
'weather_data': response_obj.weather_data,
|
||||||
|
'temperature': response_obj.temperature,
|
||||||
|
'description': response_obj.description,
|
||||||
|
'humidity': response_obj.humidity,
|
||||||
|
'wind_speed': response_obj.wind_speed,
|
||||||
|
'formatted_report': response_obj.formatted_report,
|
||||||
|
'processing_time': float(response_obj.processing_time) if response_obj.processing_time else None,
|
||||||
|
'wallet_balance': float(request.user.wallet_balance)
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
# Fallback if no response object
|
||||||
return JsonResponse({
|
return JsonResponse({
|
||||||
'success': True,
|
'success': True,
|
||||||
'request_id': str(agent_request.id),
|
'status': 'completed',
|
||||||
'message': 'Weather Reporter request processed successfully',
|
'message': 'Weather report generated successfully',
|
||||||
'wallet_balance': float(request.user.wallet_balance)
|
'wallet_balance': float(request.user.wallet_balance)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user