Spaces:
Running
Running
// Error: Could not generate code. Please try again.
Browse files
script.js
CHANGED
|
@@ -34,8 +34,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 34 |
const prompt = `Create a ${tech} app that ${appDescription.value}`;
|
| 35 |
|
| 36 |
// Call Gemini API
|
| 37 |
-
const
|
| 38 |
-
|
|
|
|
| 39 |
headers: {
|
| 40 |
'Content-Type': 'application/json',
|
| 41 |
},
|
|
@@ -50,14 +51,25 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 50 |
|
| 51 |
const data = await response.json();
|
| 52 |
let generatedCode = "";
|
| 53 |
-
if (
|
| 54 |
-
|
| 55 |
-
} else {
|
| 56 |
-
generatedCode = "// Error: Could not generate code. Please try again.";
|
| 57 |
-
console.error('API response format unexpected:', data);
|
| 58 |
}
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
typeWriterEffect(codeOutput, generatedCode, 10, () => {
|
| 62 |
generateBtn.disabled = false;
|
| 63 |
generateBtn.innerHTML = '<i data-feather="code"></i> Generate My App';
|
|
@@ -73,7 +85,15 @@ typeWriterEffect(codeOutput, generatedCode, 10, () => {
|
|
| 73 |
});
|
| 74 |
} catch (error) {
|
| 75 |
console.error('Error generating code:', error);
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
generateBtn.disabled = false;
|
| 78 |
generateBtn.innerHTML = '<i data-feather="code"></i> Generate My App';
|
| 79 |
feather.replace();
|
|
|
|
| 34 |
const prompt = `Create a ${tech} app that ${appDescription.value}`;
|
| 35 |
|
| 36 |
// Call Gemini API
|
| 37 |
+
const apiUrl = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=AIzaSyB0HOw6eHYRrqMJ_ZwcEdynMk2NdDfPnP4';
|
| 38 |
+
const response = await fetch(apiUrl, {
|
| 39 |
+
method: 'POST',
|
| 40 |
headers: {
|
| 41 |
'Content-Type': 'application/json',
|
| 42 |
},
|
|
|
|
| 51 |
|
| 52 |
const data = await response.json();
|
| 53 |
let generatedCode = "";
|
| 54 |
+
if (!response.ok) {
|
| 55 |
+
throw new Error(`API request failed with status ${response.status}`);
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
+
|
| 58 |
+
const data = await response.json();
|
| 59 |
+
let generatedCode = "";
|
| 60 |
|
| 61 |
+
try {
|
| 62 |
+
if (data.candidates?.[0]?.content?.parts?.[0]?.text) {
|
| 63 |
+
generatedCode = data.candidates[0].content.parts[0].text;
|
| 64 |
+
} else {
|
| 65 |
+
console.warn('Unexpected API response format:', data);
|
| 66 |
+
throw new Error('Invalid API response format');
|
| 67 |
+
}
|
| 68 |
+
} catch (err) {
|
| 69 |
+
generatedCode = `// Error: ${err.message}\n// Raw response: ${JSON.stringify(data, null, 2)}`;
|
| 70 |
+
console.error('Error processing API response:', err);
|
| 71 |
+
}
|
| 72 |
+
// Display the code with typewriter effect
|
| 73 |
typeWriterEffect(codeOutput, generatedCode, 10, () => {
|
| 74 |
generateBtn.disabled = false;
|
| 75 |
generateBtn.innerHTML = '<i data-feather="code"></i> Generate My App';
|
|
|
|
| 85 |
});
|
| 86 |
} catch (error) {
|
| 87 |
console.error('Error generating code:', error);
|
| 88 |
+
|
| 89 |
+
// Create error message with more details
|
| 90 |
+
const errorMsg = `Failed to generate code:\n\n${error.message}\n\nPlease check your input and try again.`;
|
| 91 |
+
|
| 92 |
+
// Add error to chat
|
| 93 |
+
addMessage(errorMsg, 'ai');
|
| 94 |
+
|
| 95 |
+
// Show error in code output
|
| 96 |
+
codeOutput.textContent = `// Error: ${error.message}\n// Please try again or modify your request`;
|
| 97 |
generateBtn.disabled = false;
|
| 98 |
generateBtn.innerHTML = '<i data-feather="code"></i> Generate My App';
|
| 99 |
feather.replace();
|
style.css
CHANGED
|
@@ -35,13 +35,22 @@ body {
|
|
| 35 |
::-webkit-scrollbar-thumb:hover {
|
| 36 |
background: #555;
|
| 37 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
/* Tech choice buttons */
|
| 40 |
.tech-choice {
|
| 41 |
transition: all 0.2s ease;
|
| 42 |
cursor: pointer;
|
| 43 |
}
|
| 44 |
-
|
| 45 |
.tech-choice.active {
|
| 46 |
background-color: #6366f1;
|
| 47 |
color: white;
|
|
|
|
| 35 |
::-webkit-scrollbar-thumb:hover {
|
| 36 |
background: #555;
|
| 37 |
}
|
| 38 |
+
/* Error styles */
|
| 39 |
+
#code-output {
|
| 40 |
+
white-space: pre-wrap;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
#code-output:has(> :contains("Error:")) {
|
| 44 |
+
color: #ef4444;
|
| 45 |
+
background-color: #fee2e2;
|
| 46 |
+
border-left: 4px solid #ef4444;
|
| 47 |
+
}
|
| 48 |
|
| 49 |
/* Tech choice buttons */
|
| 50 |
.tech-choice {
|
| 51 |
transition: all 0.2s ease;
|
| 52 |
cursor: pointer;
|
| 53 |
}
|
|
|
|
| 54 |
.tech-choice.active {
|
| 55 |
background-color: #6366f1;
|
| 56 |
color: white;
|