Spaces:
Running
Running
File size: 5,618 Bytes
e19913f 39138d2 a6f2ae8 39138d2 e19913f 954a8a4 e19913f 954a8a4 e19913f 78a2bad 954a8a4 39138d2 e19913f 954a8a4 39138d2 e19913f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
function listProjects() {
apiGet('/project/list')
.then(data => {
const select = document.getElementById('project-select');
select.innerHTML = '<option value="">-- Select a project --</option>';
Object.keys(data).forEach(project => {
const option = document.createElement('option');
option.value = project;
option.text = project;
select.appendChild(option);
});
})
.catch(err => console.error(err));
}
function loadProjectDetails() {
const project = document.getElementById('project-select').value;
if (!project) return;
apiGet(`/project/${project}/latest`)
.then(data => {
const details = document.getElementById('project-details');
const info = document.getElementById('project-info');
details.classList.remove('d-none');
let html = `<p><strong>Version:</strong> ${data.version_number}</p>`;
html += `<p><strong>Published:</strong> ${data.published ? 'β
Yes' : 'β No'}</p>`;
html += `<div><strong>Intents:</strong><ul>`;
if (Array.isArray(data.intents) && data.intents.length > 0) {
data.intents.forEach(intent => {
html += `<li>${intent.name}
<button class="btn btn-sm btn-primary ml-2" onclick="editIntent('${intent.name}')">Edit</button>
<button class="btn btn-sm btn-danger ml-2" onclick="removeIntent('${intent.name}')">β</button>
</li>`;
});
} else {
html += `<li>No intents defined.</li>`;
}
html += `</ul><button class="btn btn-sm btn-success" onclick="addIntent()">+ Add Intent</button></div>`;
info.innerHTML = html;
if (data.published) {
document.querySelectorAll('#project-info input, #project-info button').forEach(el => {
el.disabled = true;
});
}
})
.catch(err => console.error(err));
}
function addIntent() {
const project = document.getElementById('project-select').value;
const intentName = prompt('Enter new intent name:');
if (!intentName) return;
apiPost('/project/add_intent', { project_name: project, intent_name: intentName })
.then(data => {
showResult('project-result', data);
loadProjectDetails();
})
.catch(err => {
console.error(err);
alert('Failed to add intent.');
});
}
function removeIntent(name) {
const project = document.getElementById('project-select').value;
if (!confirm(`Are you sure you want to delete intent '${name}'?`)) return;
apiPost('/project/delete_intent', { project_name: project, intent_name: name })
.then(data => {
showResult('project-result', data);
loadProjectDetails();
})
.catch(err => {
console.error(err);
alert('Failed to delete intent.');
});
}
function editIntent(name) {
const project = document.getElementById('project-select').value;
apiGet(`/project/${project}/latest`)
.then(data => {
const intent = data.intents.find(i => i.name === name);
if (!intent) {
alert('Intent not found.');
return;
}
document.getElementById('intent-project-name').value = project;
document.getElementById('intent-name').value = intent.name;
document.getElementById('intent-action').value = intent.action || '';
document.getElementById('intent-fallback').value = intent.fallback_error_message || '';
document.getElementById('intent-prompt').value = intent.humanization_prompt || '';
document.getElementById('intent-examples').value = (intent.examples || []).join(', ');
document.getElementById('intent-parameters').value = JSON.stringify(intent.parameters || [], null, 2);
$('#intentModal').modal('show');
})
.catch(err => console.error(err));
}
function saveIntent() {
const project = document.getElementById('intent-project-name').value;
const name = document.getElementById('intent-name').value;
const action = document.getElementById('intent-action').value;
const fallback = document.getElementById('intent-fallback').value;
const prompt = document.getElementById('intent-prompt').value;
const examples = document.getElementById('intent-examples').value.split(',').map(e => e.trim());
let parameters = [];
try {
parameters = JSON.parse(document.getElementById('intent-parameters').value);
} catch (e) {
alert('Invalid JSON in parameters.');
return;
}
const intentData = {
action: action,
fallback_error_message: fallback,
humanization_prompt: prompt,
examples: examples,
parameters: parameters
};
apiPost('/project/update_intent', {
project_name: project,
intent_name: name,
intent_data: intentData
})
.then(data => {
showResult('project-result', data);
$('#intentModal').modal('hide');
loadProjectDetails();
})
.catch(err => {
console.error(err);
alert('Failed to save intent.');
});
}
function saveProject() {
alert('Save logic will be implemented here.');
}
function publishProject() {
alert('Publish logic will be implemented here.');
}
|