MediaWiki:CommonDashboard.js: differenze tra le versioni
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
Riga 1: | Riga 1: | ||
// ============================================ | // ============================================ | ||
// | // 🔧 CommonDashboard.js – versione aggiornata GPT-4o con fetch corretti | ||
// ============================================ | // ============================================ | ||
Riga 14: | Riga 11: | ||
}; | }; | ||
// ⚙️ CONNESSIONE API | // ⚙️ CONNESSIONE API | ||
window.testAPIConnection = async function () { | window.testAPIConnection = async function () { | ||
const key = document.getElementById('api-key').value.trim(); | const key = document.getElementById('api-key').value.trim(); | ||
Riga 49: | Riga 43: | ||
if (data.choices && data.choices.length > 0) { | if (data.choices && data.choices.length > 0) { | ||
output.innerText = "✅ Risposta: | output.innerText = "✅ Risposta:" | ||
+ data.choices[0].message.content; | |||
logActivity(`✅ Risposta ricevuta – Modello: <strong>${model}</strong>`); | logActivity(`✅ Risposta ricevuta – Modello: <strong>${model}</strong>`); | ||
} else if (data.error) { | } else if (data.error) { | ||
Riga 63: | Riga 58: | ||
}; | }; | ||
// 📊 STATO PROGETTI | // 📊 STATO PROGETTI | ||
window.addNewProject = function () { | window.addNewProject = function () { | ||
const title = document.getElementById("newProjectTitle").value.trim(); | const title = document.getElementById("newProjectTitle").value.trim(); | ||
Riga 94: | Riga 85: | ||
}; | }; | ||
// 📥 Carica prompt.txt | // 📥 Carica prompt.txt | ||
window.loadPrompt = function () { | window.loadPrompt = function () { | ||
fetch('/ | fetch('/dashboard/api/read_file.php?projectName=SSO_LinkedIn&subfolder=&filename=prompt.txt') | ||
.then(response => { | .then(response => { | ||
if (!response.ok) throw new Error('Errore nel caricamento del file'); | if (!response.ok) throw new Error('Errore nel caricamento del file'); | ||
Riga 114: | Riga 102: | ||
}; | }; | ||
// 💾 Salva prompt.txt | // 💾 Salva prompt.txt | ||
window.savePrompt = function () { | window.savePrompt = function () { | ||
const content = document.getElementById("promptArea").value; | const content = document.getElementById("promptArea").value; | ||
fetch('/ | const payload = { | ||
projectName: "SSO_LinkedIn", | |||
subfolder: "", | |||
filename: "prompt.txt", | |||
content: content | |||
}; | |||
fetch('/dashboard/api/write_file.php', { | |||
method: 'POST', | method: 'POST', | ||
headers: { | headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(payload) | |||
body: | |||
}) | }) | ||
.then(response => | .then(response => response.json()) | ||
.then(data => { | |||
.then( | |||
logActivity("💾 Salvato prompt.txt dal form."); | logActivity("💾 Salvato prompt.txt dal form."); | ||
alert("Prompt salvato con successo."); | alert("Prompt salvato con successo."); | ||
Riga 141: | Riga 128: | ||
}; | }; | ||
// 💾 Salva RISPOSTA GPT IN FILE | // 💾 Salva RISPOSTA GPT IN FILE | ||
window.salvaFileDaTextarea = function () { | window.salvaFileDaTextarea = function () { | ||
const content = document.getElementById("gpt-response-area").value; | const content = document.getElementById("gpt-response-area").value; | ||
Riga 158: | Riga 142: | ||
fetch('/dashboard/api/write_file.php', { | fetch('/dashboard/api/write_file.php', { | ||
method: 'POST', | method: 'POST', | ||
headers: { | headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ projectName: project, subfolder, filename, content }) | |||
body: JSON.stringify({ | |||
}) | }) | ||
.then(response => response.json()) | .then(response => response.json()) | ||
.then(data => { | .then(data => { | ||
logActivity(`💾 Salvato <strong>${filename}</strong> in progetto <strong>${project}</strong>.`); | logActivity(`💾 Salvato <strong>${filename}</strong> in progetto <strong>${project}</strong>.`); | ||
alert("File salvato con successo | alert("File salvato con successo."); | ||
}) | }) | ||
.catch(error => { | .catch(error => { | ||
alert("Errore durante il salvataggio del file."); | |||
logActivity("❌ Errore nel salvataggio."); | |||
}); | }); | ||
}; | }; | ||
// 📂 Carica File Salvato in Textarea GPT | // 📂 Carica File Salvato in Textarea GPT | ||
window.caricaFileGPT = function () { | |||
const filename = document.getElementById("gpt-filename").value.trim(); | const filename = document.getElementById("gpt-filename").value.trim(); | ||
const subfolder = document.getElementById("gpt-subfolder").value; | const subfolder = document.getElementById("gpt-subfolder").value; | ||
Riga 206: | Riga 179: | ||
}) | }) | ||
.catch(err => { | .catch(err => { | ||
alert("Errore durante il caricamento."); | |||
logActivity("❌ Errore durante il caricamento."); | |||
}); | }); | ||
}; | }; | ||
// 📝 LOG ATTIVITÀ | |||
// 📝 LOG ATTIVITÀ | |||
window.logActivity = function (message) { | window.logActivity = function (message) { | ||
const log = document.getElementById("activityLog"); | const log = document.getElementById("activityLog"); | ||
Riga 221: | Riga 191: | ||
entry.innerHTML = `<span style="color:gray;">[${time}]</span> ${message}`; | entry.innerHTML = `<span style="color:gray;">[${time}]</span> ${message}`; | ||
log.prepend(entry); | log.prepend(entry); | ||
}; | }; | ||
Riga 253: | Riga 198: | ||
logActivity("🧹 Log svuotato manualmente."); | logActivity("🧹 Log svuotato manualmente."); | ||
}; | }; | ||
Versione delle 11:19, 2 ago 2025
// ============================================
// 🔧 CommonDashboard.js – versione aggiornata GPT-4o con fetch corretti
// ============================================
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:"
+ 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('/dashboard/api/read_file.php?projectName=SSO_LinkedIn&subfolder=&filename=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;
const payload = {
projectName: "SSO_LinkedIn",
subfolder: "",
filename: "prompt.txt",
content: content
};
fetch('/dashboard/api/write_file.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
})
.then(response => response.json())
.then(data => {
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, filename, content })
})
.then(response => response.json())
.then(data => {
logActivity(`💾 Salvato <strong>${filename}</strong> in progetto <strong>${project}</strong>.`);
alert("File salvato con successo.");
})
.catch(error => {
alert("Errore durante il salvataggio del file.");
logActivity("❌ Errore nel salvataggio.");
});
};
// 📂 Carica File Salvato in Textarea GPT
window.caricaFileGPT = function () {
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) {
alert("Inserisci il nome del file da caricare.");
return;
}
fetch(`/dashboard/api/read_file.php?projectName=${encodeURIComponent(project)}&subfolder=${encodeURIComponent(subfolder)}&filename=${encodeURIComponent(filename)}`)
.then(res => res.json())
.then(data => {
if (data.status === "ok") {
document.getElementById("gpt-response-area").value = data.content;
logActivity(`📂 Caricato <strong>${filename}</strong> da progetto <strong>${project}</strong>.`);
} else {
alert("Errore: " + data.error);
logActivity(`❌ Errore nel caricamento di ${filename}.`);
}
})
.catch(err => {
alert("Errore durante il caricamento.");
logActivity("❌ Errore durante il caricamento.");
});
};
// 📝 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);
};
window.clearActivityLog = function () {
const contenitore = document.getElementById("activityLogContent");
contenitore.innerHTML = "<em>Registro svuotato.</em><br>";
logActivity("🧹 Log svuotato manualmente.");
};