File size: 15,947 Bytes
a450a2d
ea6c2a8
 
 
 
a450a2d
aa0e937
ea6c2a8
 
aa0e937
ea6c2a8
 
8761cb3
a450a2d
8761cb3
ea6c2a8
 
 
 
 
 
 
 
a450a2d
ea6c2a8
 
 
bcb0fad
a2c35f4
7017720
 
aa0e937
 
ea6c2a8
a450a2d
7017720
8761cb3
ea6c2a8
cf11907
a450a2d
ea6c2a8
7017720
 
8761cb3
 
a450a2d
8761cb3
7017720
ea6c2a8
 
 
 
 
 
 
a2c35f4
ea6c2a8
 
 
 
 
 
 
 
 
 
a582fa0
 
 
ea6c2a8
 
7017720
ea6c2a8
 
8761cb3
 
7017720
8761cb3
 
ea6c2a8
 
e2d8a68
 
 
7017720
8761cb3
e2d8a68
 
 
 
 
 
7017720
 
 
 
 
 
 
a450a2d
62b9a40
 
 
 
 
 
 
 
 
 
 
 
 
a450a2d
62b9a40
 
 
 
a450a2d
62b9a40
 
 
 
a450a2d
62b9a40
 
 
 
 
 
 
 
5dcafa7
62b9a40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5dcafa7
62b9a40
 
 
 
 
 
 
5dcafa7
 
 
 
 
 
 
 
 
 
62b9a40
 
 
 
 
5dcafa7
 
 
 
 
 
 
 
 
 
 
 
 
 
62b9a40
 
5dcafa7
 
8761cb3
62b9a40
 
 
 
 
 
 
 
ea6c2a8
62b9a40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf11907
62b9a40
 
 
 
 
 
 
 
 
 
8761cb3
62b9a40
 
 
 
 
 
 
 
 
 
 
 
 
 
cf11907
62b9a40
 
 
 
 
 
8761cb3
ea6c2a8
e2d8a68
 
 
 
8761cb3
 
 
ea6c2a8
7017720
ea6c2a8
7017720
 
 
 
 
 
 
 
 
 
 
 
 
ea6c2a8
 
 
 
 
 
7017720
ea6c2a8
7017720
 
62b9a40
 
 
 
 
 
 
 
 
7017720
 
ea6c2a8
7017720
ea6c2a8
 
 
a582fa0
bcb0fad
 
 
ea6c2a8
 
 
 
 
 
 
 
7017720
 
62b9a40
 
7017720
 
 
 
 
 
 
 
 
 
ea6c2a8
7017720
 
 
62b9a40
 
 
7017720
 
 
ea6c2a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18847f3
ea6c2a8
 
 
 
 
 
 
 
 
 
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import { useState } from "react";
import { RiSparkling2Fill } from "react-icons/ri";
import { GrSend } from "react-icons/gr";
import classNames from "classnames";
import { toast } from "react-toastify";
// Removed monaco editor import

import Login from "../login/login";
import { defaultHTML } from "../../utils/consts";
import SuccessSound from "./../../assets/success.mp3";

function AskAI({
  html, // Current full HTML content (used for initial request and context)
  setHtml, // Used for updates (both full and diff-based)
  onScrollToBottom, // Used for full updates
  isAiWorking,
  setisAiWorking,
}: {
  html: string;
  setHtml: (html: string) => void;
  onScrollToBottom: () => void;
  isAiWorking: boolean;
  setisAiWorking: React.Dispatch<React.SetStateAction<boolean>>;
  // Removed editorRef prop
}) {
  const [open, setOpen] = useState(false);
  const [prompt, setPrompt] = useState("");
  const [hasAsked, setHasAsked] = useState(false);
  const [previousPrompt, setPreviousPrompt] = useState("");
  const [streamProgress, setStreamProgress] = useState(0); // Track streaming progress
  const [responseMode, setResponseMode] = useState<"full" | "diff" | null>(null); // Track response type
  const audio = new Audio(SuccessSound);
  audio.volume = 0.5;

  // Removed client-side diff parsing/applying logic
  
  // --- Main AI Call Logic ---
  const callAi = async () => {
    if (isAiWorking || !prompt.trim()) return;
    const originalHtml = html; // Store the HTML state at the start of the request
    setisAiWorking(true);
    setStreamProgress(0); // Reset progress indicator
    setResponseMode(null); // Reset response mode

    let fullContentResponse = ""; // Used for full HTML mode
    let accumulatedDiffResponse = ""; // Used for diff mode
    let lastRenderTime = 0; // For throttling full HTML updates
    let totalChunksReceived = 0;

    try {
      const request = await fetch("/api/ask-ai", {
        method: "POST",
        body: JSON.stringify({
          prompt,
          ...(html === defaultHTML ? {} : { html }),
          ...(previousPrompt ? { previousPrompt } : {}),
        }),
        headers: {
          "Content-Type": "application/json",
        },
      });
      if (request && request.body) {
        if (!request.ok) {
          const res = await request.json();
          if (res.openLogin) {
            setOpen(true);
          } else {
            // don't show toast if it's a login error
            toast.error(res.message);
          }
          setisAiWorking(false);
          setStreamProgress(0);
          return;
        }

        const responseType = request.headers.get("X-Response-Type") || "full"; // Default to full if header missing
        setResponseMode(responseType as "full" | "diff");
        console.log(`[AI Response] Type: ${responseType}`);

        const reader = request.body.getReader();
        const decoder = new TextDecoder("utf-8");
        
        console.log("[AI Request] Starting to read response stream");
        
        // Process all chunks until done
        while (true) {
          try {
            const { done, value } = await reader.read();
            if (done) {
              console.log("[AI Response] Stream finished successfully.");
              console.log("[AI Response] Accumulated full content length:", fullContentResponse.length);
              console.log("[AI Response] Accumulated diff content length:", accumulatedDiffResponse.length);
              setStreamProgress(100); // Set progress to 100% when done
              
              // Play success animation
              document.body.classList.add('ai-success');
              setTimeout(() => {
                document.body.classList.remove('ai-success');
              }, 1000);

              // --- Post-stream processing ---
              if (responseType === 'diff') {
                // Apply diffs server-side
                try {
                  console.log("[Diff Apply] Sending original HTML and AI diff response to server...");
                  const applyRequest = await fetch("/api/apply-diffs", {
                    method: "POST",
                    headers: { "Content-Type": "application/json" },
                    body: JSON.stringify({
                      originalHtml: originalHtml, // Send the HTML from the start of the request
                      aiResponseContent: accumulatedDiffResponse,
                    }),
                  });

                  if (!applyRequest.ok) {
                    const errorData = await applyRequest.json();
                    throw new Error(errorData.message || `Server failed to apply diffs (status ${applyRequest.status})`);
                  }

                  const patchedHtml = await applyRequest.text();
                  console.log("[Diff Apply] Received patched HTML from server.");
                  setHtml(patchedHtml); // Update editor with the final result
                  toast.success("AI changes applied");

                } catch (applyError: unknown) {
                  console.error("Error applying diffs server-side:", applyError);
                  const errorMessage = applyError instanceof Error 
                    ? applyError.message 
                    : 'Unknown error applying changes';
                  toast.error(`Failed to apply AI changes: ${errorMessage}`);
                  // Optionally revert to originalHtml? Or leave the editor as is?
                  // setHtml(originalHtml); // Uncomment to revert on failure
                }

              } else {
                // Final update for full HTML mode
                const finalDoc = fullContentResponse.match(/<!DOCTYPE html>[\s\S]*<\/html>/)?.[0];
                if (finalDoc) {
                  console.log("[AI Response] Found complete HTML document");
                  setHtml(finalDoc); // Ensure final complete HTML is set
                } else if (fullContentResponse.includes("<html") && fullContentResponse.includes("</html>")) {
                  // Try to match HTML without DOCTYPE
                  const htmlMatch = fullContentResponse.match(/<html[\s\S]*<\/html>/);
                  if (htmlMatch) {
                    console.log("[AI Response] Found HTML without DOCTYPE, adding DOCTYPE");
                    setHtml(`<!DOCTYPE html>\n${htmlMatch[0]}`);
                  }
                } else if (fullContentResponse.trim()) {
                  console.warn("[AI Response] Final response doesn't contain proper HTML");
                  
                  // Search for any HTML-like content
                  if (fullContentResponse.includes("<body") || 
                      (fullContentResponse.includes("<div") && fullContentResponse.includes("</div>"))) {
                    
                    console.log("[AI Response] Found partial HTML content, wrapping it");
                    // Wrap the content in a basic HTML structure
                    const wrappedContent = `<!DOCTYPE html>
<html>
<head>
  <title>AI Generated Content</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
  ${fullContentResponse}
</body>
</html>`;
                    setHtml(wrappedContent);
                  } else {
                    // If it's just text, wrap it in pre tag
                    console.log("[AI Response] No HTML found, creating preview with text content");
                    const textContent = `<!DOCTYPE html>
<html>
<head>
  <title>AI Generated Text</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    body { font-family: Arial, sans-serif; padding: 20px; }
    pre { white-space: pre-wrap; word-break: break-word; }
  </style>
</head>
<body>
  <pre>${fullContentResponse}</pre>
</body>
</html>`;
                    setHtml(textContent);
                  }
                }
              }

              toast.success("AI processing complete");
              setPrompt("");
              setPreviousPrompt(prompt);
              setisAiWorking(false);
              setHasAsked(true);
              audio.play();
              break; // Exit the loop
            }

            const chunk = decoder.decode(value, { stream: true });
            totalChunksReceived++;
            
            // Update progress indicator with better estimation
            // First 5 chunks are typically slower (setup)
            let estimatedProgress;
            if (totalChunksReceived <= 5) {
              estimatedProgress = Math.floor((totalChunksReceived / 5) * 25); // First 25%
            } else if (totalChunksReceived <= 15) {
              estimatedProgress = 25 + Math.floor(((totalChunksReceived - 5) / 10) * 50); // Next 50%
            } else {
              estimatedProgress = 75 + Math.floor(((totalChunksReceived - 15) / 10) * 20); // Final 25%
            }
            estimatedProgress = Math.min(95, estimatedProgress);
            setStreamProgress(estimatedProgress);
            
            console.log("[AI Stream] Received chunk of length:", chunk.length);
            if (chunk.length > 0) {
              console.log("[AI Stream] Sample:", chunk.substring(0, 50) + (chunk.length > 50 ? "..." : ""));
            }

            if (responseType === 'diff') {
              // --- Diff Mode ---
              accumulatedDiffResponse += chunk; // Just accumulate the raw response
              console.log("[AI Diff] Accumulated diff length now:", accumulatedDiffResponse.length);
            } else {
              // --- Full HTML Mode ---
              fullContentResponse += chunk; // Accumulate for preview
              // Use regex to find the start of the HTML doc
              const newHtmlMatch = fullContentResponse.match(/<!DOCTYPE html>[\s\S]*/);
              const newHtml = newHtmlMatch ? newHtmlMatch[0] : null;

              if (newHtml) {
                console.log("[AI Full] Found HTML content, length:", newHtml.length);
                // Throttle the re-renders to avoid flashing/flicker - reduced from 300ms to 150ms
                const now = Date.now();
                if (now - lastRenderTime > 150) {
                   // Force-close the HTML tag for preview if needed
                   let partialDoc = newHtml;
                   if (!partialDoc.trim().endsWith("</html>")) {
                       partialDoc += "\n</html>";
                   }
                  console.log("[AI Full] Updating preview with partial HTML");
                  setHtml(partialDoc); // Update the preview iframe content
                  lastRenderTime = now;
                }

                // Scroll editor down if content is long (heuristic)
                if (newHtml.length > 200 && now - lastRenderTime < 50) { // Only scroll if recently rendered
                  onScrollToBottom();
                }
              } else {
                console.log("[AI Full] No HTML content found yet in stream");
              }
            }
          } catch (streamError) {
            console.error("[AI Response] Error processing stream chunk:", streamError);
            // Continue trying to read the stream despite error
          }
        } // end while loop
      } else {
         throw new Error("Response body is null");
      }
    } catch (error: unknown) {
      setisAiWorking(false);
      setStreamProgress(0);
      
      // Handle the error with proper type checking
      if (error instanceof Error) {
        toast.error(error.message);
      } else if (typeof error === 'object' && error !== null && 'openLogin' in error) {
        const loginError = error as { openLogin: boolean, message?: string };
        toast.error(loginError.message || 'Authentication error');
        if (loginError.openLogin) {
          setOpen(true);
        }
      } else {
        toast.error('An unexpected error occurred');
      }
    }
  };

  return (
    <div
      className={`bg-gray-950 rounded-xl py-2 lg:py-2.5 pl-3.5 lg:pl-4 pr-2 lg:pr-2.5 absolute lg:sticky bottom-3 left-3 lg:bottom-4 lg:left-4 w-[calc(100%-1.5rem)] lg:w-[calc(100%-2rem)] z-10 group`}
    >
      {/* Progress indicator */}
      {isAiWorking && (
        <div className="absolute top-0 left-0 h-2 bg-gradient-to-r from-purple-500 via-pink-500 to-blue-500 transition-all duration-200 ease-in-out shadow-lg animate-pulse"
             style={{ width: `${streamProgress}%`, borderTopLeftRadius: '0.75rem', zIndex: 20 }}>
          <div className="absolute right-2 top-1/2 -translate-y-1/2 flex items-center gap-2">
            <span className="text-xs font-bold text-white drop-shadow-lg">
              {streamProgress}%
            </span>
            <span className="inline-block w-3 h-3 border-2 border-white border-t-transparent rounded-full animate-spin"></span>
          </div>
        </div>
      )}
      
      <div className="w-full relative flex items-center justify-between">
        <RiSparkling2Fill className={`text-lg lg:text-xl ${isAiWorking ? 'text-pink-500 animate-pulse' : 'text-gray-500'} group-focus-within:text-pink-500`} />
        <input
          type="text"
          disabled={isAiWorking}
          className="w-full bg-transparent max-lg:text-sm outline-none pl-3 text-white placeholder:text-gray-500 font-code"
          placeholder={
            hasAsked ? "What do you want to ask AI next?" : "Ask AI anything..."
          }
          value={prompt}
          onChange={(e) => setPrompt(e.target.value)}
          onKeyDown={(e) => {
            if (e.key === "Enter") {
              callAi();
            }
          }}
        />
        {isAiWorking ? (
          <div className="flex-none flex items-center justify-center rounded-full text-sm font-semibold size-8 text-center bg-pink-500 text-white shadow-sm dark:shadow-highlight/20">
            <div className="animate-spin h-4 w-4 border-2 border-white border-t-transparent rounded-full mr-1"></div>
            <span className="ml-1 text-xs font-bold animate-ellipsis">{streamProgress}%</span>
          </div>
        ) : (
          <button
            disabled={isAiWorking}
            className="relative overflow-hidden cursor-pointer flex-none flex items-center justify-center rounded-full text-sm font-semibold size-8 text-center bg-pink-500 hover:bg-pink-400 text-white shadow-sm dark:shadow-highlight/20 disabled:bg-gray-300 disabled:text-gray-500 disabled:cursor-not-allowed disabled:hover:bg-gray-300"
            onClick={callAi}
          >
            <GrSend className="-translate-x-[1px]" />
          </button>
        )}
      </div>
      
      {/* Response mode indicator */}
      {isAiWorking && responseMode && (
        <div className="absolute -top-8 right-0 text-xs font-medium text-white bg-gray-800/90 backdrop-blur-sm px-3 py-1.5 rounded-full flex items-center gap-1.5 animate-pulse border border-gray-700 shadow-lg z-20">
          <div className="size-2 rounded-full bg-pink-500 animate-pulse"></div>
          {responseMode === 'diff' ? 'Applying changes to code...' : 'Generating new HTML...'}
        </div>
      )}
      
      <div
        className={classNames(
          "h-screen w-screen bg-black/20 fixed left-0 top-0 z-10",
          {
            "opacity-0 pointer-events-none": !open,
          }
        )}
        onClick={() => setOpen(false)}
      ></div>
      <div
        className={classNames(
          "absolute top-0 -translate-y-[calc(100%+8px)] right-0 z-10 w-80 bg-white border border-gray-200 rounded-lg shadow-lg transition-all duration-75 overflow-hidden",
          {
            "opacity-0 pointer-events-none": !open,
          }
        )}
      >
        <Login html={html}>
          <p className="text-gray-500 text-sm mb-3">
            You reached the limit of free AI usage. Please login to continue.
          </p>
        </Login>
      </div>
    </div>
  );
}

export default AskAI;