File size: 3,139 Bytes
7b710e8
 
 
 
 
 
 
 
 
 
 
ff7128b
7b710e8
ff7128b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();
        document.querySelector(this.getAttribute('href')).scrollIntoView({
            behavior: 'smooth'
        });
    });
});

document.addEventListener('DOMContentLoaded', function() {
// Check for saved API key
    const savedApiKey = localStorage.getItem('deepseek_api_key');
    if (!savedApiKey) {
        document.querySelector('custom-api-key-modal').setAttribute('open', 'true');
    }

    // Handle send request button
    document.getElementById('send-request').addEventListener('click', async function() {
        const apiKey = localStorage.getItem('deepseek_api_key');
        if (!apiKey) {
            document.querySelector('custom-api-key-modal').setAttribute('open', 'true');
            return;
        }

        const requestBody = document.getElementById('request-body').value;
        const responsePlaceholder = document.getElementById('response-placeholder');
        const responseContent = document.getElementById('response-content');
        
        try {
            // Show loading state
            responsePlaceholder.innerHTML = `
                <div class="flex flex-col items-center">
                    <i data-feather="loader" class="w-12 h-12 mx-auto mb-4 loading-spinner"></i>
                    <p>Processing your request...</p>
                </div>
            `;
            feather.replace();
            
            responsePlaceholder.classList.remove('hidden');
            responseContent.classList.add('hidden');
            
            // Parse JSON to validate
            const jsonBody = JSON.parse(requestBody);
            
            // Make API call
            const response = await fetch('https://api.deepseek.com/v1/chat/completions', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${apiKey}`
                },
                body: requestBody
            });
            
            const data = await response.json();
            
            // Display response
            responseContent.textContent = JSON.stringify(data, null, 2);
            responsePlaceholder.classList.add('hidden');
            responseContent.classList.remove('hidden');
            
        } catch (error) {
            responsePlaceholder.innerHTML = `
                <div class="flex flex-col items-center text-red-500">
                    <i data-feather="alert-circle" class="w-12 h-12 mx-auto mb-4"></i>
                    <p>Error: ${error.message}</p>
                </div>
            `;
            feather.replace();
        }
    });
});

// Theme toggle functionality
function toggleDarkMode() {
    const html = document.documentElement;
    if (html.classList.contains('dark')) {
        html.classList.remove('dark');
        localStorage.setItem('theme', 'light');
    } else {
        html.classList.add('dark');
        localStorage.setItem('theme', 'dark');
    }
}