document.addEventListener('DOMContentLoaded', () => {
// Initialize UI
updateAIStatus();
// Handle prompt submission on Enter key
document.getElementById('promptInput').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendPrompt();
}
});
});
async function sendPrompt() {
const input = document.getElementById('promptInput');
const preview = document.getElementById('previewBox');
if (!input.value.trim()) {
preview.innerHTML = '
Veuillez entrer un prompt valide
';
return;
}
// Show loading state
preview.innerHTML = `
`;
try {
const result = await callAI(input.value);
const formattedText = result.text.replace(/\n/g, '
');
if (result.status === "offline") {
preview.innerHTML = `
Ollama non détecté
${formattedText}
`;
} else {
preview.innerHTML = `
${formattedText}
`;
}
// Clear input
input.value = '';
} catch (error) {
preview.innerHTML = `
Erreur: ${error.message}
`;
feather.replace();
}
}
function updateAIStatus() {
const statusElement = document.getElementById('aiStatus');
if (APP_CONFIG.aiMode === 'demo') {
statusElement.textContent = 'Mode démonstration';
statusElement.classList.add('text-yellow-400');
statusElement.classList.remove('text-green-400');
} else {
statusElement.textContent = `Connecté à Ollama (${APP_CONFIG.ollama.defaultModel})`;
statusElement.classList.add('text-green-400');
statusElement.classList.remove('text-yellow-400');
}
}
function showSettings() {
const modal = document.getElementById('settingsModal');
document.getElementById('ollamaUrl').value = APP_CONFIG.ollama.baseUrl;
document.getElementById('ollamaModel').value = APP_CONFIG.ollama.defaultModel;
modal.classList.remove('hidden');
feather.replace();
}
function hideSettings() {
document.getElementById('settingsModal').classList.add('hidden');
}
async function testOllamaConnection() {
const url = document.getElementById('ollamaUrl').value;
try {
const response = await fetch(`${url}/api/tags`, {
headers: {
"Authorization": `Bearer ${APP_CONFIG.ollama.apiKey}`
}
});
if (!response.ok) throw new Error('Connection failed');
alert('Connexion réussie à Ollama!');
} catch (error) {
alert(`Erreur de connexion: ${error.message}`);
}
}
function saveOllamaSettings() {
APP_CONFIG.ollama.baseUrl = document.getElementById('ollamaUrl').value;
APP_CONFIG.ollama.defaultModel = document.getElementById('ollamaModel').value;
APP_CONFIG.aiMode = 'ollama';
updateAIStatus();
hideSettings();
localStorage.setItem('ollamaConfig', JSON.stringify(APP_CONFIG.ollama));
}