Nota: dopo aver pubblicato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.
- Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (⌘-R su Mac)
- Google Chrome: premi Ctrl-Shift-R (⌘-Shift-R su un Mac)
- Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5.
// ============================================
// 🔧 CommonDashboard.js – versione GPT-4o + Lettura file reale
// ============================================
// ============================================
// 📘 INTRODUZIONE
// ============================================
window.toggleDashboardBox = function (id) {
const box = document.getElementById(id);
if (box) {
box.style.display = box.style.display === "block" ? "none" : "block";
}
};
// ============================================
// ⚙️ CONNESSIONE API
// ============================================
window.testAPIConnection = async function () {
const key = document.getElementById('api-key').value.trim();
const prompt = document.getElementById('test-prompt').value;
const output = document.getElementById('api-result');
const model = "gpt-4o-2024-05-13";
if (!key || !prompt) {
output.innerText = '⚠️ Inserisci una chiave valida e un prompt.';
return;
}
output.innerText = '⏳ Attendere risposta da OpenAI...';
logActivity(`🚀 Test API avviato – Modello: <strong>${model}</strong>`);
try {
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + key
},
body: JSON.stringify({
model: model,
messages: [{ role: "user", content: prompt }],
temperature: 0.7
})
});
const data = await response.json();
if (data.choices && data.choices.length > 0) {
output.innerText = "✅ Risposta:\n" + data.choices[0].message.content;
logActivity(`✅ Risposta ricevuta – Modello: <strong>${model}</strong>`);
} else if (data.error) {
output.innerText = "❌ Errore: " + data.error.message;
logActivity(`❌ Errore API: ${data.error.message}`);
} else {
output.innerText = "❌ Nessuna risposta ricevuta.";
}
} catch (e) {
output.innerText = "🚫 Errore di rete o sintassi: " + e.message;
logActivity(`❌ Errore di connessione: ${e.message}`);
}
};
// ============================================
// 📊 STATO PROGETTI
// ============================================
window.addNewProject = function () {
const title = document.getElementById("newProjectTitle").value.trim();
const notes = document.getElementById("newProjectNotes").value.trim();
if (!title) return;
const table = document.getElementById("projectRows");
const newRow = document.createElement("tr");
newRow.innerHTML = `
<td style="padding:0.5rem; border:1px solid #ccc;">${title}</td>
<td style="padding:0.5rem; border:1px solid #ccc; color:green; font-weight:bold;">✅ Completato</td>
<td style="padding:0.5rem; border:1px solid #ccc;">${new Date().toLocaleDateString("it-IT")}</td>
<td style="padding:0.5rem; border:1px solid #ccc;">${notes || '–'}</td>
`;
table.appendChild(newRow);
window.closeProjectDialog();
logActivity(`📌 Progetto aggiunto: <strong>${title}</strong> – ${notes}`);
};
window.openProjectDialog = function () {
document.getElementById("newProjectDialog").style.display = "block";
};
window.closeProjectDialog = function () {
document.getElementById("newProjectDialog").style.display = "none";
};
// ============================================
// 📥 Carica prompt.txt
// ============================================
window.loadPrompt = function () {
fetch('/mnt/data/masticationpedia-openai/prompt.txt')
.then(response => {
if (!response.ok) throw new Error('Errore nel caricamento del file');
return response.text();
})
.then(text => {
document.getElementById("promptArea").value = text;
logActivity("📥 Caricato prompt.txt nella textarea.");
})
.catch(error => {
alert("Errore nel caricamento: " + error);
logActivity("❌ Errore nel caricamento di prompt.txt.");
});
};
// ============================================
// 💾 Salva prompt.txt
// ============================================
window.savePrompt = function () {
const content = document.getElementById("promptArea").value;
fetch('/mnt/data/masticationpedia-openai/write_prompt.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'text=' + encodeURIComponent(content)
})
.then(response => {
if (!response.ok) throw new Error('Errore nel salvataggio');
return response.text();
})
.then(msg => {
logActivity("💾 Salvato prompt.txt dal form.");
alert("Prompt salvato con successo.");
})
.catch(error => {
alert("Errore nel salvataggio: " + error);
logActivity("❌ Errore nel salvataggio di prompt.txt.");
});
};
// ============================================
// 💾 Salva RISPOSTA GPT IN FILE
// ============================================
window.salvaFileDaTextarea = function () {
const content = document.getElementById("gpt-response-area").value;
const filename = document.getElementById("gpt-filename").value.trim();
const subfolder = document.getElementById("gpt-subfolder").value;
const project = document.getElementById("newProjectTitle").value.trim() || "SSO_LinkedIn";
if (!filename || !content) {
alert("Inserisci un nome file e assicurati che il contenuto non sia vuoto.");
return;
}
fetch('/dashboard/api/write_file.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
projectName: project,
subfolder: subfolder,
filename: filename,
content: content
})
})
.then(response => response.json())
.then(data => {
console.log("✅ File salvato:", data);
logActivity(`💾 Salvato <strong>${filename}</strong> in progetto <strong>${project}</strong>.`);
alert("File salvato con successo in:\n" + data.path);
})
.catch(error => {
console.error("❌ Errore:", error);
alert("Errore durante il salvataggio.");
});
};
// ============================================
// 📝 LOG ATTIVITÀ?
// ============================================
window.logActivity = function (message) {
const log = document.getElementById("activityLog");
const time = new Date().toLocaleTimeString("it-IT");
const entry = document.createElement("div");
entry.innerHTML = `<span style="color:gray;">[${time}]</span> ${message}`;
log.prepend(entry);
};
// ============================================
// 🧪 STRUMENTI DI TEST
// ============================================
// (Da completare nel prossimo step)
// ============================================
// 🧾 REGISTRO ATTIVITÀ
// ============================================
window.logActivity = function (messaggio) {
const contenitore = document.getElementById("activityLogContent");
if (!contenitore) return;
const ora = new Date().toLocaleTimeString("it-IT");
const paragrafo = document.createElement("p");
paragrafo.innerHTML = `<strong>[${ora}]</strong> ${messaggio}`;
contenitore.appendChild(paragrafo);
contenitore.scrollTop = contenitore.scrollHeight;
};
window.clearActivityLog = function () {
const contenitore = document.getElementById("activityLogContent");
contenitore.innerHTML = "<em>Registro svuotato.</em><br>";
logActivity("🧹 Log svuotato manualmente.");
};
// ============================================
// 🧠 CARICA IN OPENAI
// ============================================
// (Da completare nel prossimo step)