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:
Claude 2025-07-11 22:37:14 +05:30
parent 07d61dbf63
commit 82fc051370
2 changed files with 38 additions and 23 deletions

View File

@ -634,7 +634,7 @@
// Submit form via AJAX
const formData = new FormData(this);
fetch(window.location.href, {
fetch('/agents/weather-reporter/process/', {
method: 'POST',
body: formData,
headers: {
@ -644,20 +644,17 @@
.then(response => response.json())
.then(result => {
clearInterval(stepInterval);
if (result.success && result.request_id) {
// Start polling for results
pollForResults(result.request_id);
} else {
// Handle immediate response
// Handle immediate response (API-based agent)
document.getElementById('processingStatus').style.display = 'none';
document.getElementById('processButton').disabled = false;
document.getElementById('processButton').innerHTML = '🌤️ Get Weather Report (2.00 AED)';
if (result.error) {
showToast(`❌ ${result.error}`, 'error');
} else {
} else if (result.success) {
displayResults(result);
}
} else {
showToast('❌ Failed to generate weather report', 'error');
}
})
.catch(error => {

View File

@ -41,7 +41,8 @@ class WeatherReporterProcessView(View):
return JsonResponse({'error': 'Authentication required'}, status=401)
try:
data = json.loads(request.body)
# Handle FormData from frontend
data = request.POST.dict()
# Get agent
agent = BaseAgent.objects.get(slug='weather-reporter')
@ -60,23 +61,40 @@ class WeatherReporterProcessView(View):
)
# Process request
# Process request immediately (API-based agent)
processor = WeatherReporterProcessor()
result = processor.process_request(
request_obj=agent_request,
user_id=request.user.id,
location=data.get('location'),
report_type=data.get('report_type'),
)
# Refresh user from database to get updated wallet balance
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({
'success': True,
'request_id': str(agent_request.id),
'message': 'Weather Reporter request processed successfully',
'status': 'completed',
'message': 'Weather report generated successfully',
'wallet_balance': float(request.user.wallet_balance)
})