Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ AZURE_OPENAI_API_KEY=YOURKEY
AZURE_OPENAI_ENDPOINT=https://YOURENDPOINT.openai.azure.com
AZURE_OPENAI_API_VERSION=2024-02-01
MODEL=gpt-4o-mini
APP_ENV=development
EMBEDDING=text-embedding-3-small
EMBEDDING_MODEL_MAX_TOKENS=8192
GITHUB_TOKEN=YOURTOKEN
SUPABASE_URL=https://YOURLINK.supabase.co
SUPABASE_KEY=your_supabase_key
SUPABASE_KEY=your_supabase_key
19 changes: 19 additions & 0 deletions helpers/logging_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import logging
import os


DEBUG_ENVIRONMENTS = {"dev", "development", "local", "test", "debug"}


def get_log_level(environment=None):
app_env = environment
if app_env is None:
app_env = (
os.getenv("APP_ENV")
or os.getenv("ENVIRONMENT")
or os.getenv("PYTHON_ENV")
or os.getenv("ENV")
or ""
)

return logging.DEBUG if app_env.lower() in DEBUG_ENVIRONMENTS else logging.INFO
42 changes: 25 additions & 17 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
console.log("main.js loaded");
const isDebugLoggingEnabled = () => {
const host = window.location.hostname;
return window.DEVCONTAINER_DEBUG_LOGS === true || host === "localhost" || host === "127.0.0.1" || host === "";
};

const debugLog = (...args) => {
if (isDebugLoggingEnabled()) console.log(...args);
};

debugLog("main.js loaded");

document.addEventListener('htmx:beforeRequest', function(event) {
console.log('htmx:beforeRequest triggered');
debugLog('htmx:beforeRequest triggered');

if (event.detail.elt.id === 'generate-button') {
console.log('Disabling generate button');
debugLog('Disabling generate button');
event.detail.elt.disabled = true;
}
});

document.addEventListener('htmx:afterRequest', function(event) {
console.log('htmx:afterRequest triggered');
debugLog('htmx:afterRequest triggered');

if (event.detail.elt.id === 'generate-button') {
console.log('Enabling generate button');
debugLog('Enabling generate button');
event.detail.elt.disabled = false;
}
});

document.addEventListener('DOMContentLoaded', function() {
console.log('DOM fully loaded and parsed');
debugLog('DOM fully loaded and parsed');
const generateButton = document.getElementById('generate-button');
if (generateButton) {
console.log('Generate button found');
debugLog('Generate button found');
generateButton.addEventListener('click', function() {
console.log('Generate button clicked');
debugLog('Generate button clicked');
});
} else {
console.log('Generate button not found');
debugLog('Generate button not found');
}
});

Expand All @@ -37,7 +46,7 @@ function handleCopyClick(event) {
const button = event.target.closest('.icon-button.copy-button');
if (!button) return;

console.log("Copy button clicked");
debugLog("Copy button clicked");

const codeContainer = button.closest(".code-container");
if (!codeContainer) {
Expand All @@ -56,7 +65,7 @@ function handleCopyClick(event) {

navigator.clipboard.writeText(codeContents)
.then(() => {
console.log("Copy successful");
debugLog("Copy successful");
showActionText("Copied!");
})
.catch(error => {
Expand All @@ -68,7 +77,7 @@ function handleRegenerateClick(event) {
const button = event.target.closest('.icon-button.regenerate-button');
if (!button) return;

console.log("Regenerate button clicked");
debugLog("Regenerate button clicked");
showActionText("Regenerating...");
// The actual regeneration is handled by HTMX
}
Expand All @@ -90,17 +99,17 @@ function initializeButtons() {
const copyButtons = document.querySelectorAll(".icon-button.copy-button");
const regenerateButtons = document.querySelectorAll(".icon-button.regenerate-button");

console.log("Copy buttons found:", copyButtons.length);
console.log("Regenerate buttons found:", regenerateButtons.length);
debugLog("Copy buttons found:", copyButtons.length);
debugLog("Regenerate buttons found:", regenerateButtons.length);

copyButtons.forEach((button, index) => {
console.log(`Adding listener to copy button ${index}`);
debugLog(`Adding listener to copy button ${index}`);
button.removeEventListener("click", handleCopyClick);
button.addEventListener("click", handleCopyClick);
});

regenerateButtons.forEach((button, index) => {
console.log(`Adding listener to regenerate button ${index}`);
debugLog(`Adding listener to regenerate button ${index}`);
button.removeEventListener("click", handleRegenerateClick);
button.addEventListener("click", handleRegenerateClick);
});
Expand Down Expand Up @@ -164,4 +173,3 @@ document.addEventListener('DOMContentLoaded', function() {
initializeButtons();
setupObserver();
});

9 changes: 5 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
from helpers.openai_helpers import setup_azure_openai, setup_instructor
from helpers.github_helpers import fetch_repo_context, check_url_exists
from helpers.devcontainer_helpers import generate_devcontainer_json, validate_devcontainer_json
from helpers.logging_helpers import get_log_level
from helpers.token_helpers import count_tokens, truncate_to_token_limit
from models import DevContainer
from schemas import DevContainerModel
from content import *

# Set up logging
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")

# Load environment variables
load_dotenv()

# Set up logging
logging.basicConfig(level=get_log_level(), format="%(asctime)s - %(levelname)s - %(message)s")

def check_env_vars():
required_vars = [
"AZURE_OPENAI_ENDPOINT",
Expand Down Expand Up @@ -207,4 +208,4 @@ async def get(fname:str, ext:str):

if __name__ == "__main__":
logging.info("Starting FastHTML app...")
serve()
serve()
20 changes: 20 additions & 0 deletions tests/test_logging_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import logging
import unittest

from helpers.logging_helpers import get_log_level


class LoggingHelpersTest(unittest.TestCase):
def test_development_like_environments_use_debug_logging(self):
for environment in ("dev", "development", "local", "test", "debug"):
with self.subTest(environment=environment):
self.assertEqual(get_log_level(environment), logging.DEBUG)

def test_production_like_environments_use_info_logging(self):
for environment in ("", "prod", "production", "staging"):
with self.subTest(environment=environment):
self.assertEqual(get_log_level(environment), logging.INFO)


if __name__ == "__main__":
unittest.main()