mirror of
https://github.com/thecyberlearn/quantum-ai-v3.git
synced 2026-08-18 12:52:58 +00:00
- Fix social ads generator updateWalletBalance function call - Fix weather reporter form submission URL and syntax errors - Add missing status endpoints for polling functionality - Remove debug logging from all agents - Clean up button styling consistency 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
525 lines
22 KiB
HTML
525 lines
22 KiB
HTML
{% extends 'base.html' %}
|
|
{% load static %}
|
|
|
|
{% block title %}Weather Reporter Agent - NetCop AI Hub{% endblock %}
|
|
|
|
{% block extra_css %}
|
|
<!-- Optimized Font Loading -->
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
|
|
|
|
<!-- External Stylesheets -->
|
|
<link rel="stylesheet" href="{% static 'css/themes.css' %}">
|
|
<link rel="stylesheet" href="{% static 'css/agent-base.css' %}">
|
|
|
|
<!-- Shared JavaScript Utilities -->
|
|
<script src="{% static 'js/agent-utils.js' %}"></script>
|
|
|
|
<style>
|
|
/* Custom styles for weather reporter radio grid */
|
|
.radio-grid {
|
|
display: grid;
|
|
grid-template-columns: 1fr;
|
|
gap: var(--space-md);
|
|
}
|
|
|
|
.radio-option {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: var(--space-md);
|
|
padding: var(--space-lg);
|
|
border: 1px solid var(--agent-border);
|
|
border-radius: var(--radius);
|
|
cursor: pointer;
|
|
background: white;
|
|
transition: all var(--transition);
|
|
min-height: 60px;
|
|
}
|
|
|
|
.radio-option.selected {
|
|
border-color: var(--agent-primary);
|
|
background: var(--agent-card-bg);
|
|
}
|
|
|
|
.radio-option:hover {
|
|
border-color: var(--agent-primary);
|
|
background: var(--hover-color);
|
|
}
|
|
|
|
.radio-option input[type="radio"] {
|
|
width: 18px;
|
|
height: 18px;
|
|
margin: 0;
|
|
}
|
|
|
|
.form-help {
|
|
font-size: var(--text-xs);
|
|
color: var(--accent-color);
|
|
margin-top: var(--space-xs);
|
|
opacity: 0.7;
|
|
}
|
|
</style>
|
|
{% endblock %}
|
|
|
|
{% block content %}
|
|
<div class="agent-page theme-professional">
|
|
<div class="agent-container">
|
|
<!-- Messages -->
|
|
{% if messages %}
|
|
{% for message in messages %}
|
|
<div class="{% if message.tags == 'error' %}error-message{% else %}success-message{% endif %}" style="grid-column: 1 / -1;">
|
|
{{ message }}
|
|
</div>
|
|
{% endfor %}
|
|
{% endif %}
|
|
|
|
<!-- Main Content -->
|
|
<div>
|
|
<div class="card">
|
|
<h3 class="section-title">🌤️ Weather Reporter</h3>
|
|
|
|
<form method="POST" id="weatherForm">
|
|
{% csrf_token %}
|
|
|
|
<!-- Location Input -->
|
|
<div class="form-group">
|
|
<label for="location" class="form-label">📍 Enter Location *</label>
|
|
<input type="text" name="location" id="location" class="form-input" placeholder="Enter city name, address, or coordinates..." required>
|
|
<div class="form-help">Examples: "New York", "London, UK", "Tokyo, Japan", "37.7749,-122.4194"</div>
|
|
</div>
|
|
|
|
<!-- Report Type Selection -->
|
|
<div>
|
|
<label class="form-label">📊 Report Type *</label>
|
|
<div class="radio-grid">
|
|
<label class="radio-option selected" onclick="selectReportType('current')">
|
|
<input type="radio" name="report_type" value="current" checked>
|
|
<div style="font-size: 20px;">🌡️</div>
|
|
<div>
|
|
<div style="font-weight: 600; margin-bottom: 4px; font-size: 16px;">Current Weather</div>
|
|
<div style="font-size: 14px; color: var(--text-secondary);">Real-time conditions</div>
|
|
</div>
|
|
</label>
|
|
|
|
<label class="radio-option" onclick="selectReportType('forecast')">
|
|
<input type="radio" name="report_type" value="forecast">
|
|
<div style="font-size: 20px;">📅</div>
|
|
<div>
|
|
<div style="font-weight: 600; margin-bottom: 4px; font-size: 16px;">5-Day Forecast</div>
|
|
<div style="font-size: 14px; color: var(--text-secondary);">Extended weather outlook</div>
|
|
</div>
|
|
</label>
|
|
|
|
<label class="radio-option" onclick="selectReportType('detailed')">
|
|
<input type="radio" name="report_type" value="detailed">
|
|
<div style="font-size: 20px;">📋</div>
|
|
<div>
|
|
<div style="font-weight: 600; margin-bottom: 4px; font-size: 16px;">Detailed Report</div>
|
|
<div style="font-size: 14px; color: var(--text-secondary);">Current + forecast combined</div>
|
|
</div>
|
|
</label>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<!-- Processing Status -->
|
|
<div id="processingStatus" class="processing-status">
|
|
<div class="status-icon">⏳</div>
|
|
<div style="font-weight: 600; color: var(--primary-dark);">Getting Weather Data...</div>
|
|
<div style="font-size: 14px; color: var(--primary-blue); margin-top: 8px;" id="statusText">Fetching latest weather information for your location</div>
|
|
</div>
|
|
|
|
<!-- Results -->
|
|
<div id="weatherResults" class="results-card">
|
|
<div class="results-header">
|
|
<div style="font-size: 24px;">✅</div>
|
|
<h3 style="font-size: 20px; font-weight: 600; color: var(--text-primary); margin: 0;">Weather Report</h3>
|
|
<div style="background: var(--primary-blue); color: white; padding: 6px 12px; border-radius: 6px; font-size: 14px; font-weight: 600; margin-left: auto;">✅ Complete</div>
|
|
</div>
|
|
|
|
<div class="results-content" id="weatherContent">
|
|
<!-- Weather data will be displayed here -->
|
|
</div>
|
|
|
|
<div class="action-buttons">
|
|
<button onclick="copyWeatherReport()" class="btn btn-primary">📋 Copy Report</button>
|
|
<button onclick="downloadWeatherReport()" class="btn btn-secondary">💾 Download Report</button>
|
|
<button onclick="resetForm()" class="btn" style="background: var(--primary-blue); color: white;">🔄 Get Another</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Wallet Sidebar -->
|
|
<div class="wallet-section">
|
|
<div class="card">
|
|
<h3 class="section-title">💳 Your Wallet</h3>
|
|
|
|
<div class="wallet-balance" data-wallet-balance>
|
|
{% if user.is_authenticated %}
|
|
{{ user.wallet_balance|floatformat:2 }} AED
|
|
{% else %}
|
|
0.00 AED
|
|
{% endif %}
|
|
</div>
|
|
<div class="balance-label">Available Balance</div>
|
|
|
|
{% if user.is_authenticated %}
|
|
{% if user.wallet_balance >= 2.00 %}
|
|
<button type="submit" form="weatherForm" class="btn btn-primary process-btn" id="processButton">
|
|
🌤️ Get Weather Report (2.00 AED)
|
|
</button>
|
|
{% else %}
|
|
<div class="insufficient-balance">
|
|
Insufficient balance! You need 2.00 AED.
|
|
</div>
|
|
<a href="{% url 'core:wallet' %}" class="btn btn-primary process-btn" style="text-decoration: none;">
|
|
💰 Top Up Wallet
|
|
</a>
|
|
{% endif %}
|
|
{% else %}
|
|
<a href="{% url 'authentication:login' %}" class="btn btn-primary process-btn" style="text-decoration: none;">
|
|
🔑 Login to Continue
|
|
</a>
|
|
{% endif %}
|
|
</div>
|
|
|
|
<div class="usage-info">
|
|
<h4>💡 How it works</h4>
|
|
<ul>
|
|
<li>Enter any city name worldwide</li>
|
|
<li>Choose your preferred report type</li>
|
|
<li>Get real-time weather data</li>
|
|
<li>Copy or download detailed reports</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{% endblock %}
|
|
|
|
{% block extra_js %}
|
|
<script>
|
|
// Report type selection
|
|
function selectReportType(type) {
|
|
document.querySelectorAll('.radio-option').forEach(option => {
|
|
option.classList.remove('selected');
|
|
});
|
|
|
|
event.currentTarget.classList.add('selected');
|
|
document.querySelector(`input[value="${type}"]`).checked = true;
|
|
}
|
|
|
|
// Form validation
|
|
function isFormValid() {
|
|
const location = document.getElementById('location').value.trim();
|
|
const reportType = document.querySelector('input[name="report_type"]:checked');
|
|
|
|
return location && reportType;
|
|
}
|
|
|
|
// Copy weather report to clipboard
|
|
function copyWeatherReport() {
|
|
const reportText = AgentUtils.generateTextForExport('weatherContent');
|
|
AgentUtils.copyToClipboard(reportText, 'Weather report copied to clipboard!');
|
|
}
|
|
|
|
// Download weather report as text file
|
|
function downloadWeatherReport() {
|
|
const reportText = AgentUtils.generateTextForExport('weatherContent');
|
|
AgentUtils.downloadAsFile(reportText, `weather-report-${Date.now()}.txt`, 'Weather report downloaded!');
|
|
}
|
|
|
|
// Track processing state to prevent duplicates
|
|
let isProcessing = false;
|
|
|
|
// Reset form for creating another report
|
|
function resetForm() {
|
|
// Reset processing state
|
|
isProcessing = false;
|
|
|
|
// Reset form and UI
|
|
document.getElementById('weatherForm').reset();
|
|
document.getElementById('weatherResults').style.display = 'none';
|
|
document.getElementById('processingStatus').style.display = 'none';
|
|
document.getElementById('processButton').disabled = false;
|
|
document.getElementById('processButton').innerHTML = '🌤️ Get Weather Report (2.00 AED)';
|
|
|
|
// Reset to default selection
|
|
document.querySelectorAll('.radio-option').forEach(option => {
|
|
option.classList.remove('selected');
|
|
});
|
|
document.querySelector('.radio-option').classList.add('selected');
|
|
document.querySelector('input[value="current"]').checked = true;
|
|
|
|
AgentUtils.showToast('Form reset! Ready for another weather report.', 'success');
|
|
}
|
|
|
|
// Simple toast notification
|
|
|
|
|
|
// Display weather results with clean weather data formatting
|
|
function displayResults(result) {
|
|
const resultsContainer = document.getElementById('weatherResults');
|
|
const contentElement = document.getElementById('weatherContent');
|
|
|
|
if (!resultsContainer || !contentElement) {
|
|
console.error('Weather results elements not found');
|
|
return;
|
|
}
|
|
|
|
// Update wallet balance if provided
|
|
if (result.wallet_balance !== undefined) {
|
|
AgentUtils.updateWalletBalance(result.wallet_balance);
|
|
}
|
|
|
|
// Handle errors
|
|
if (result.error) {
|
|
AgentUtils.showToast(`❌ ${result.error}`, 'error');
|
|
return;
|
|
}
|
|
|
|
// Handle failed status
|
|
if (result.status === 'failed') {
|
|
AgentUtils.showToast('❌ Failed to generate weather report - no charge applied', 'error');
|
|
return;
|
|
}
|
|
|
|
// Extract weather content with priority order
|
|
let content = '';
|
|
|
|
if (result.weather_data && typeof result.weather_data === 'string') {
|
|
content = result.weather_data;
|
|
} else if (result.formatted_report && typeof result.formatted_report === 'string') {
|
|
content = result.formatted_report;
|
|
} else if (result.content && typeof result.content === 'string') {
|
|
content = result.content;
|
|
} else if (result.output_text && typeof result.output_text === 'string') {
|
|
content = result.output_text;
|
|
} else {
|
|
content = 'Weather report generated successfully!';
|
|
}
|
|
|
|
// Format weather content with clean data presentation
|
|
const formattedContent = formatWeatherContent(content);
|
|
|
|
// Update content
|
|
contentElement.innerHTML = formattedContent;
|
|
|
|
// Show results
|
|
resultsContainer.style.display = 'block';
|
|
resultsContainer.scrollIntoView({ behavior: 'smooth' });
|
|
|
|
// Show success message
|
|
const successMessage = result.success ? '✅ Weather report completed and payment processed!' : '✅ Weather report generated!';
|
|
AgentUtils.showToast(successMessage, 'success');
|
|
}
|
|
|
|
// Clean weather data formatter
|
|
function formatWeatherContent(content) {
|
|
if (!content || typeof content !== 'string') {
|
|
return '<p>No weather data available.</p>';
|
|
}
|
|
|
|
let html = content;
|
|
|
|
// Enhanced header formatting for weather sections
|
|
html = html.replace(/^# (.*$)/gm, '<h1 style="font-size: 22px; font-weight: 700; color: var(--primary-blue); margin: 20px 0 14px 0; border-bottom: 3px solid var(--primary-blue); padding-bottom: 10px; text-align: center;">$1</h1>');
|
|
html = html.replace(/^## (.*$)/gm, '<h2 style="font-size: 18px; font-weight: 600; color: var(--primary-dark); margin: 16px 0 10px 0; padding: 8px 12px; background: rgba(59, 130, 246, 0.1); border-radius: 8px; border-left: 4px solid var(--primary-blue);">$1</h2>');
|
|
html = html.replace(/^### (.*$)/gm, '<h3 style="font-size: 16px; font-weight: 600; color: var(--text-primary); margin: 14px 0 8px 0; padding-bottom: 4px;">$1</h3>');
|
|
|
|
// Temperature and weather metric highlighting
|
|
html = html.replace(/\*\*(.*?)\*\*/g, '<strong style="font-weight: 600; color: var(--primary-dark); background: rgba(59, 130, 246, 0.1); padding: 2px 6px; border-radius: 4px;">$1</strong>');
|
|
|
|
// Weather condition formatting with clean presentation
|
|
const lines = html.split('\n');
|
|
let inList = false;
|
|
let processedLines = [];
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
const line = lines[i].trim();
|
|
|
|
if (line.startsWith('- ') || line.startsWith('• ')) {
|
|
if (!inList) {
|
|
processedLines.push('<ul style="margin: 12px 0; padding-left: 20px; list-style: none;">');
|
|
inList = true;
|
|
}
|
|
const listContent = line.substring(2).trim();
|
|
processedLines.push(`<li style="margin: 6px 0; line-height: 1.5; color: var(--text-primary); position: relative; padding-left: 20px;"><span style="position: absolute; left: 0; color: var(--primary-blue); font-weight: bold;">•</span>${listContent}</li>`);
|
|
} else {
|
|
if (inList) {
|
|
processedLines.push('</ul>');
|
|
inList = false;
|
|
}
|
|
processedLines.push(line);
|
|
}
|
|
}
|
|
|
|
if (inList) {
|
|
processedLines.push('</ul>');
|
|
}
|
|
|
|
html = processedLines.join('\n');
|
|
|
|
// Convert line breaks to clean paragraphs
|
|
const paragraphs = html.split(/\n\s*\n/);
|
|
html = paragraphs
|
|
.filter(p => p.trim() !== '')
|
|
.map(p => {
|
|
const trimmed = p.trim();
|
|
// Don't wrap headers or lists in paragraphs
|
|
if (trimmed.startsWith('<h') || trimmed.startsWith('<ul') || trimmed.startsWith('</ul>') || trimmed.includes('<li')) {
|
|
return trimmed;
|
|
}
|
|
return `<p style="margin: 12px 0; line-height: 1.6; color: var(--text-primary); font-size: 15px;">${trimmed}</p>`;
|
|
})
|
|
.join('');
|
|
|
|
// Add clean wrapper for weather data
|
|
html = `<div style="font-family: 'Inter', sans-serif; max-width: 100%; overflow-wrap: break-word; background: var(--background-subtle); border-radius: 8px; padding: 16px; border: 1px solid var(--border-light);">${html}</div>`;
|
|
|
|
return html;
|
|
}
|
|
|
|
// Poll for results
|
|
function pollForResults(requestId) {
|
|
let pollCount = 0;
|
|
const maxPolls = 30; // 30 seconds maximum
|
|
|
|
const pollInterval = setInterval(() => {
|
|
pollCount++;
|
|
|
|
fetch(`/agents/weather-reporter/status/${requestId}/`)
|
|
.then(response => response.json())
|
|
.then(result => {
|
|
if (result.status === 'completed' || result.status === 'failed') {
|
|
clearInterval(pollInterval);
|
|
document.getElementById('processingStatus').style.display = 'none';
|
|
document.getElementById('processButton').disabled = false;
|
|
document.getElementById('processButton').innerHTML = '🌤️ Get Weather Report (2.00 AED)';
|
|
|
|
displayResults(result);
|
|
} else if (pollCount >= maxPolls) {
|
|
clearInterval(pollInterval);
|
|
document.getElementById('processingStatus').style.display = 'none';
|
|
document.getElementById('processButton').disabled = false;
|
|
document.getElementById('processButton').innerHTML = '🌤️ Get Weather Report (2.00 AED)';
|
|
AgentUtils.showToast('❌ Processing timeout - please try again', 'error');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error polling results:', error);
|
|
if (pollCount >= maxPolls) {
|
|
clearInterval(pollInterval);
|
|
document.getElementById('processingStatus').style.display = 'none';
|
|
document.getElementById('processButton').disabled = false;
|
|
document.getElementById('processButton').innerHTML = '🌤️ Get Weather Report (2.00 AED)';
|
|
AgentUtils.showToast('❌ Network error - please try again', 'error');
|
|
}
|
|
});
|
|
}, 1000);
|
|
}
|
|
|
|
// Handle form submission
|
|
document.getElementById('weatherForm').addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
|
|
if (!isFormValid()) {
|
|
AgentUtils.showToast('Please fill in all required fields', 'error');
|
|
return;
|
|
}
|
|
|
|
// Prevent duplicate submissions
|
|
if (isProcessing) {
|
|
return;
|
|
}
|
|
|
|
// Check user authentication
|
|
{% if not user.is_authenticated %}
|
|
window.location.href = "{% url 'authentication:login' %}";
|
|
return;
|
|
{% endif %}
|
|
|
|
// Check wallet balance
|
|
const balance = {{ user.wallet_balance|default:0 }};
|
|
if (balance < 2.00) {
|
|
AgentUtils.showToast('Insufficient balance! You need 2.00 AED.', 'error');
|
|
setTimeout(() => {
|
|
window.location.href = "{% url 'core:wallet' %}";
|
|
}, 2000);
|
|
return;
|
|
}
|
|
|
|
// Set processing state
|
|
isProcessing = true;
|
|
|
|
// Show processing status with steps
|
|
document.getElementById('processingStatus').style.display = 'block';
|
|
document.getElementById('processButton').disabled = true;
|
|
document.getElementById('processButton').innerHTML = '⏳ Processing...';
|
|
document.getElementById('weatherResults').style.display = 'none';
|
|
|
|
const steps = [
|
|
'Connecting to weather service...',
|
|
'Fetching location data...',
|
|
'Getting current conditions...',
|
|
'Processing weather information...',
|
|
'Generating report...'
|
|
];
|
|
|
|
let currentStep = 0;
|
|
const stepInterval = setInterval(() => {
|
|
if (currentStep < steps.length) {
|
|
document.getElementById('statusText').textContent = steps[currentStep];
|
|
currentStep++;
|
|
} else {
|
|
clearInterval(stepInterval);
|
|
}
|
|
}, 800);
|
|
|
|
// Submit form via AJAX
|
|
const formData = new FormData(this);
|
|
|
|
fetch('/agents/weather-reporter/process/', {
|
|
method: 'POST',
|
|
body: formData,
|
|
headers: {
|
|
'X-Requested-With': 'XMLHttpRequest'
|
|
}
|
|
})
|
|
.then(response => response.json())
|
|
.then(result => {
|
|
clearInterval(stepInterval);
|
|
|
|
// Reset processing state
|
|
isProcessing = false;
|
|
|
|
// 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) {
|
|
AgentUtils.showToast(`❌ ${result.error}`, 'error');
|
|
} else if (result.success) {
|
|
displayResults(result);
|
|
} else {
|
|
AgentUtils.showToast('❌ Failed to generate weather report', 'error');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
clearInterval(stepInterval);
|
|
console.error('Error:', error);
|
|
|
|
// Reset processing state
|
|
isProcessing = false;
|
|
|
|
document.getElementById('processingStatus').style.display = 'none';
|
|
document.getElementById('processButton').disabled = false;
|
|
document.getElementById('processButton').innerHTML = '🌤️ Get Weather Report (2.00 AED)';
|
|
AgentUtils.showToast('❌ Network error - please try again', 'error');
|
|
});
|
|
});
|
|
</script>
|
|
{% endblock %} |