File size: 10,778 Bytes
5b1ae16
23c4cde
5b1ae16
 
 
91e52d9
5b1ae16
23c4cde
91e52d9
23c4cde
91e52d9
 
 
 
 
 
5b1ae16
23c4cde
91e52d9
 
23c4cde
91e52d9
23c4cde
91e52d9
5b1ae16
23c4cde
 
 
91e52d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b1ae16
23c4cde
 
91e52d9
 
 
5b1ae16
23c4cde
91e52d9
5b1ae16
23c4cde
91e52d9
 
 
 
23c4cde
5b1ae16
91e52d9
23c4cde
 
 
91e52d9
23c4cde
 
91e52d9
23c4cde
91e52d9
 
 
5b1ae16
91e52d9
 
5b1ae16
91e52d9
 
23c4cde
5b1ae16
91e52d9
 
 
23c4cde
91e52d9
 
 
23c4cde
91e52d9
 
 
 
 
23c4cde
91e52d9
 
 
 
 
23c4cde
91e52d9
 
 
 
 
 
 
 
 
23c4cde
91e52d9
 
23c4cde
91e52d9
 
 
 
 
 
5b1ae16
 
 
23c4cde
 
91e52d9
5b1ae16
91e52d9
 
 
23c4cde
 
91e52d9
 
23c4cde
91e52d9
 
 
 
 
23c4cde
91e52d9
23c4cde
91e52d9
5b1ae16
91e52d9
 
 
23c4cde
91e52d9
23c4cde
91e52d9
 
 
 
23c4cde
 
91e52d9
 
 
23c4cde
91e52d9
 
23c4cde
91e52d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23c4cde
 
91e52d9
 
 
 
 
 
 
23c4cde
91e52d9
 
 
23c4cde
91e52d9
 
23c4cde
5b1ae16
23c4cde
91e52d9
 
 
 
23c4cde
 
91e52d9
 
 
 
 
 
23c4cde
 
91e52d9
 
 
 
 
 
 
5b1ae16
23c4cde
91e52d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b1ae16
23c4cde
91e52d9
 
 
23c4cde
91e52d9
 
23c4cde
91e52d9
 
 
 
 
 
 
 
 
 
 
 
23c4cde
 
91e52d9
 
23c4cde
91e52d9
23c4cde
 
 
91e52d9
 
23c4cde
5b1ae16
23c4cde
5b1ae16
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DeepSeek Chat Interface</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
            background-color: #f0f2f5;
            color: #1c1e21;
            display: flex;
            justify-content: center;
        }
        .container {
            max-width: 800px;
            width: 100%;
            background: #fff;
            padding: 25px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 25px;
        }
        .auth-section {
            text-align: right;
            margin-bottom: 15px;
            font-size: 0.9em;
        }
        .auth-section button {
            padding: 5px 10px;
            font-size: 0.9em;
            cursor: pointer;
            border: 1px solid #ddd;
            border-radius: 4px;
            background-color: #f5f5f5;
            margin-left: 5px;
        }
        .auth-section button:hover {
            background-color: #e9e9e9;
        }
        .auth-section span {
            color: #606770;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #4b4f56;
        }
        textarea {
            width: calc(100% - 22px); /* Full width minus padding */
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccd0d5;
            border-radius: 6px;
            font-size: 1rem;
            min-height: 80px;
            resize: vertical;
        }
        button#send-button {
            display: block;
            width: 100%;
            padding: 12px;
            background-color: #1877f2; /* Facebook Blue */
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 1rem;
            font-weight: 600;
            transition: background-color 0.2s ease;
        }
        button#send-button:hover:not(:disabled) {
            background-color: #166fe5;
        }
        button#send-button:disabled {
            background-color: #a0c3f0;
            cursor: not-allowed;
        }
        #status {
            margin-top: 15px;
            padding: 10px;
            border-radius: 4px;
            font-size: 0.95em;
            display: none; /* Hidden by default */
            text-align: center;
        }
        #status.info {
            background-color: #e7f3ff;
            border: 1px solid #b8d7f7;
            color: #1877f2;
            display: block;
        }
        #status.error {
            background-color: #fdecea;
            border: 1px solid #f8bbc1;
            color: #a94442;
            display: block;
        }
        #response-area {
            margin-top: 20px;
            border: 1px solid #e0e0e0;
            border-radius: 6px;
            padding: 15px;
            min-height: 150px;
            background-color: #f9f9f9;
            overflow-y: auto;
            max-height: 400px;
            white-space: pre-wrap; /* Preserve whitespace and line breaks */
            word-wrap: break-word; /* Wrap long words */
            font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; /* Monospace font for code */
            font-size: 0.9em;
            line-height: 1.5;
        }
        #response-area:empty::before {
            content: "Model's response will appear here...";
            color: #999;
            font-style: italic;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>DeepSeek Chat</h1>

        <div class="auth-section">
            <span id="user-info">Checking login status...</span>
            <button id="auth-button">Login</button>
        </div>

        <label for="user-prompt">Your Prompt:</label>
        <textarea id="user-prompt" placeholder="Enter your message or instructions for the AI..."></textarea>

        <!-- Optional: Add inputs/areas for 'html' and 'previousPrompt' if needed -->
        <!--
        <label for="context-html">Current HTML (Optional):</label>
        <textarea id="context-html" placeholder="Paste existing HTML if you want the AI to modify it..."></textarea>
        -->

        <button id="send-button">Send Prompt</button>

        <div id="status"></div>

        <label for="response-area" style="margin-top: 20px;">AI Response:</label>
        <div id="response-area"></div>
        <!-- Use a div with pre-wrap instead of <pre> for better control -->

    </div>

    <script>
        const userInfoSpan = document.getElementById('user-info');
        const authButton = document.getElementById('auth-button');
        const promptInput = document.getElementById('user-prompt');
        const sendButton = document.getElementById('send-button');
        const statusDiv = document.getElementById('status');
        const responseArea = document.getElementById('response-area');
        // Optional: Get context elements if you uncomment them
        // const contextHtmlInput = document.getElementById('context-html');

        let isLoggedIn = false;
        let currentUsername = 'Guest';

        // --- Authentication Handling ---
        async function checkLoginStatus() {
            try {
                const response = await fetch('/api/@me');
                if (response.ok) {
                    const user = await response.json();
                    currentUsername = user.preferred_username || user.name || 'Logged In User';
                    userInfoSpan.textContent = `Logged in as: ${currentUsername}`;
                    authButton.textContent = 'Logout';
                    authButton.onclick = () => window.location.href = '/auth/logout';
                    isLoggedIn = true;
                } else {
                    throw new Error('Not logged in');
                }
            } catch (error) {
                userInfoSpan.textContent = 'Not logged in.';
                authButton.textContent = 'Login with Hugging Face';
                authButton.onclick = () => window.location.href = '/api/login';
                isLoggedIn = false;
            }
        }

        // --- Display Status ---
        function showStatus(message, type = 'info') {
            statusDiv.textContent = message;
            statusDiv.className = type; // 'info' or 'error'
        }

        function clearStatus() {
            statusDiv.textContent = '';
            statusDiv.className = '';
            statusDiv.style.display = 'none'; // Hide it properly
        }

        // --- Handle Sending Prompt ---
        sendButton.addEventListener('click', async () => {
            const prompt = promptInput.value.trim();
            // Optional: Get context
            // const contextHtml = contextHtmlInput.value.trim();

            if (!prompt) {
                showStatus('Please enter a prompt.', 'error');
                return;
            }

            // Clear previous results and disable button
            responseArea.textContent = ''; // Clear previous response
            clearStatus();
            showStatus('Sending request and waiting for response...', 'info');
            sendButton.disabled = true;

            const requestBody = {
                prompt: prompt,
                // Optional: Include context if available
                // html: contextHtml || null, // Send null if empty
                // previousPrompt: null // Add logic if you track previous prompts
            };

            try {
                const response = await fetch('/api/ask-ai', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Accept': 'text/plain', // Expecting a stream
                    },
                    body: JSON.stringify(requestBody),
                });

                // --- Handle Non-OK HTTP Responses (e.g., 429, 500, 402) ---
                if (!response.ok) {
                     let errorMsg = `Error: ${response.status} ${response.statusText}`;
                     try {
                        // Try to parse JSON error message from backend
                        const errorData = await response.json();
                        errorMsg = errorData.message || errorMsg;
                        // Check for specific flags from backend
                        if (errorData.openLogin) {
                            errorMsg += " Please log in.";
                            // Maybe redirect or show login button prominently
                        }
                        if (errorData.openProModal) {
                             errorMsg += " (Credit limit likely reached)";
                        }
                    } catch (e) {
                        // If response is not JSON, use the status text
                        console.warn("Could not parse error response as JSON");
                    }
                    throw new Error(errorMsg); // Throw to be caught below
                }

                // --- Process the Stream ---
                const reader = response.body.getReader();
                const decoder = new TextDecoder(); // Defaults to utf-8

                responseArea.textContent = ''; // Ensure it's clear before streaming starts
                showStatus('Receiving response...', 'info'); // Update status

                while (true) {
                    const { value, done } = await reader.read();
                    if (done) {
                        console.log('Stream finished.');
                        break;
                    }
                    const chunk = decoder.decode(value, { stream: true });
                    responseArea.textContent += chunk; // Append chunk to display
                    // Auto-scroll to bottom (optional)
                    responseArea.scrollTop = responseArea.scrollHeight;
                }
                clearStatus(); // Clear status on successful completion

            } catch (error) {
                console.error('Error fetching or streaming response:', error);
                showStatus(`Failed: ${error.message}`, 'error');
            } finally {
                sendButton.disabled = false; // Re-enable button
            }
        });

        // --- Initial Setup ---
        checkLoginStatus(); // Check login status when the page loads

    </script>
</body>
</html>