Dashboard Masticationpedia: differenze tra le versioni
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
| Riga 336: | Riga 336: | ||
// ======== Invio prompt via proxy sicuro ======== | // ======== Invio prompt via proxy sicuro ======== | ||
async function sendPrompt(){ | <script> | ||
// --- PATCH: sendPrompt con errori visibili --- | |||
async function sendPrompt(){ | |||
const ta = document.querySelector('#mpai-input'); | |||
const model = (document.querySelector('#mpai-model')?.value || 'gpt-4o-2024-05-13').trim(); | |||
const temp = parseFloat(document.querySelector('#mpai-temp')?.value || '0.7') || 0.7; | |||
const sanitizedOnly = !!(document.querySelector('#mpai-sanitized-only')?.checked); | |||
const content = (ta.value || '').trim(); | |||
if (!content){ ta.focus(); return; } | |||
// Se non c'è un progetto, usa la bozza locale | |||
if (!window.currentProject && !window.sessionId){ | |||
ensureSession(); | |||
loadSession(sessionId); | |||
} | |||
// Messaggio utente | |||
history.push({role:'user', content}); | |||
appendMsg('user', content); | |||
ta.value = ''; | |||
// Contesto | |||
const contextName = (window.currentProject) | |||
? `Progetto: ${window.currentProject}` | |||
: `Sessione: ${window.sessionMeta?.title || 'Nuova conversazione'}`; | |||
const fileNames = attachments.map(a => a.name).join(', '); | |||
const preface = `${contextName} | |||
File allegati${sanitizedOnly ? ' (sanificati)' : ''}: ${fileNames || 'nessuno'} | File allegati${sanitizedOnly ? ' (sanificati)' : ''}: ${fileNames || 'nessuno'} | ||
| Riga 371: | Riga 372: | ||
${content}`; | ${content}`; | ||
// Placeholder | |||
const pending = document.createElement('div'); | |||
pending.className = 'mpai-msg'; | |||
pending.innerHTML = '<em class="muted">Elaboro…</em>'; | |||
document.querySelector('#mpai-chat').appendChild(pending); | |||
try{ | |||
const r = await fetch('/dashboard/api/openai_project_gpt.php', { | |||
method: 'POST', | |||
headers: { 'Content-Type': 'application/json' }, | |||
credentials: 'include', // manda i cookie/credenziali per Basic Auth | |||
mode: 'same-origin', | |||
cache: 'no-store', | |||
body: JSON.stringify({ model, prompt: preface }) | |||
}); | |||
const raw = await r.text(); | |||
// se non è 2xx, mostra status + body grezzo | |||
if (!r.ok){ | |||
pending.remove(); | |||
console.error('GPT proxy error', r.status, r.statusText, raw); | |||
appendMsg('error', `HTTP ${r.status} ${r.statusText}\n\n${raw || '(nessun body)'}`); | |||
return; | |||
} | |||
// prova a parse-are il JSON | |||
let j; | |||
try { j = JSON.parse(raw); } | |||
catch(parseErr){ | |||
pending.remove(); | |||
console.error('JSON parse error', parseErr, raw); | |||
appendMsg('error', `Risposta non in JSON:\n\n${raw.slice(0,1500)}`); | |||
return; | |||
} | |||
pending.remove(); | |||
if (j.status === 'ok' && j.result){ | |||
// aggiorna titolo bozza alla prima risposta | |||
if (!window.currentProject && window.sessionMeta && window.sessionMeta.title === 'Nuova conversazione'){ | |||
window.sessionMeta.title = (content.slice(0,48) || 'Nuova conversazione'); | |||
saveSession && saveSession(); | |||
} | } | ||
history.push({role:'assistant', content: j.result}); | |||
appendMsg('assistant', j.result); | |||
saveLocal && saveLocal(); | |||
} else { | |||
console.error('API said error', j); | |||
const err = j.error || 'Errore sconosciuto'; | |||
appendMsg('error', `API error: ${err}\n\nRAW:\n${(j.raw||'').slice(0,1500)}`); | |||
} | |||
} catch(e){ | |||
pending.remove(); | |||
console.error('Network/JS error', e); | |||
appendMsg('error', `Errore di rete/JS: ${e.message}`); | |||
} | |||
} | |||
</script> | |||
<!-- ========== SEZIONI LEGACY (nascoste di default) ========== --> | <!-- ========== SEZIONI LEGACY (nascoste di default) ========== --> | ||
Versione delle 16:29, 27 set 2025
🔧 Dashboard Operativa – Masticationpedia
Centro di comando per progetti, API, file e backup