function showMagicPanel() {
const panel = document.getElementById('magic-panel');
panel.classList.add('visible');
analyzeMagicSuggestions();
}
function hideMagicPanel() {
const panel = document.getElementById('magic-panel');
if (panel) {
panel.classList.remove('visible');
}
}
async function analyzeMagicSuggestions() {
const content = document.getElementById('magic-content');
content.innerHTML = '
';
const nodes = Array.from(state.nodes.values());
const dialogData = {
nodes: nodes.map(n => ({ type: n.type, fields: n.fields })),
connections: state.connections.length,
filename: document.getElementById('current-filename').value || 'untitled'
};
try {
const response = await fetch('/api/designer/magic', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(dialogData)
});
if (response.ok) {
const suggestions = await response.json();
renderMagicSuggestions(suggestions);
} else {
renderFallbackSuggestions(dialogData);
}
} catch (e) {
renderFallbackSuggestions(dialogData);
}
}
function renderFallbackSuggestions(dialogData) {
const suggestions = [];
const nodes = dialogData.nodes;
if (!nodes.some(n => n.type === 'HEAR')) {
suggestions.push({
type: 'ux',
title: 'Add User Input',
description: 'Your dialog has no HEAR nodes. Consider adding user input to make it interactive.'
});
}
if (nodes.filter(n => n.type === 'TALK').length > 5) {
suggestions.push({
type: 'ux',
title: 'Break Up Long Responses',
description: 'You have many TALK nodes. Consider grouping related messages or using a menu.'
});
}
if (!nodes.some(n => n.type === 'IF' || n.type === 'SWITCH')) {
suggestions.push({
type: 'feature',
title: 'Add Decision Logic',
description: 'Add IF or SWITCH nodes to handle different user responses dynamically.'
});
}
if (dialogData.connections < nodes.length - 1 && nodes.length > 1) {
suggestions.push({
type: 'perf',
title: 'Check Connections',
description: 'Some nodes may not be connected. Ensure all nodes flow properly.'
});
}
suggestions.push({
type: 'a11y',
title: 'Use Clear Language',
description: 'Keep messages short and clear. Avoid jargon for better accessibility.'
});
renderMagicSuggestions(suggestions);
}
function renderMagicSuggestions(suggestions) {
const content = document.getElementById('magic-content');
if (!suggestions || suggestions.length === 0) {
content.innerHTML = 'Your dialog looks great! No suggestions at this time.
';
return;
}
const icons = {
ux: '',
perf: '',
a11y: '',
feature: ''
};
content.innerHTML = suggestions.map(s => `
`).join('');
}
document.addEventListener('keydown', (e) => {
if (e.ctrlKey && e.key === 'm') {
e.preventDefault();
showMagicPanel();
}
});