File size: 7,168 Bytes
7abe33d
 
e19913f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39138d2
 
a6f2ae8
 
 
 
39138d2
 
 
 
e19913f
 
 
 
 
 
 
 
 
 
 
 
954a8a4
 
 
 
 
 
 
 
 
 
 
 
 
 
e19913f
 
954a8a4
 
 
 
 
 
 
 
 
 
 
 
 
e19913f
 
78a2bad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7abe33d
 
78a2bad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7abe33d
78a2bad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
954a8a4
39138d2
e19913f
 
954a8a4
39138d2
e19913f
7abe33d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
let currentParameters = [];

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(', ');
            currentParameters = intent.parameters || [];
            populateParameterTable(currentParameters);

            $('#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());

    const intentData = {
        action: action,
        fallback_error_message: fallback,
        humanization_prompt: prompt,
        examples: examples,
        parameters: currentParameters
    };

    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.');
}

function populateParameterTable(parameters) {
    const body = document.getElementById('parameter-body');
    body.innerHTML = '';
    parameters.forEach((param, index) => {
        const row = document.createElement('tr');
        row.innerHTML = `
            <td><input type="text" class="form-control" value="${param.name || ''}" onchange="updateParam(${index}, 'name', this.value)"></td>
            <td>
                <select class="form-control" onchange="updateParam(${index}, 'type', this.value)">
                    <option value="string" ${param.type === 'string' ? 'selected' : ''}>string</option>
                    <option value="int" ${param.type === 'int' ? 'selected' : ''}>int</option>
                    <option value="float" ${param.type === 'float' ? 'selected' : ''}>float</option>
                </select>
            </td>
            <td><input type="text" class="form-control" value="${param.regex || ''}" onchange="updateParam(${index}, 'regex', this.value)"></td>
            <td><input type="text" class="form-control" value="${param.validation_message || ''}" onchange="updateParam(${index}, 'validation_message', this.value)"></td>
            <td><button class="btn btn-sm btn-danger" onclick="removeParameterRow(${index})">Delete</button></td>
        `;
        body.appendChild(row);
    });
}

function addParameterRow() {
    currentParameters.push({ name: '', type: 'string', regex: '', validation_message: '' });
    populateParameterTable(currentParameters);
}

function removeParameterRow(index) {
    currentParameters.splice(index, 1);
    populateParameterTable(currentParameters);
}

function updateParam(index, key, value) {
    currentParameters[index][key] = value;
}