MCPcopy Create free account
hub / github.com/explorerhq/sql-explorer / submitAssistantAsk

Function submitAssistantAsk

explorer/src/js/assistant.js:229–286  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

227
228
229function submitAssistantAsk() {
230
231 const data = {
232 sql: window.editor?.state.doc.toString() ?? null,
233 connection_id: document.getElementById("id_database_connection")?.value ?? null,
234 assistant_request: document.getElementById("id_assistant_input")?.value ?? null,
235 selected_tables: assistantChoices.getValue(true),
236 db_error: getErrorMessage()
237 };
238
239 document.getElementById("assistant_response").innerHTML = '';
240 document.getElementById("response_block").classList.remove('d-none');
241 document.getElementById("assistant_spinner").classList.remove('d-none');
242
243 fetch(`${window.baseUrlPath}assistant/`, {
244 method: 'POST',
245 headers: {
246 'Content-Type': 'application/json',
247 'X-CSRFToken': getCsrfToken()
248 },
249 body: JSON.stringify(data)
250 })
251 .then(response => {
252 if (!response.ok) {
253 throw new Error('Network response was not ok');
254 }
255 return response.json();
256 })
257 .then(data => {
258 const output = DOMPurify.sanitize(marked.parse(data.message));
259 document.getElementById("assistant_response").innerHTML = output;
260 document.getElementById("assistant_spinner").classList.add('d-none');
261
262 // If there is exactly one code block in the response and the SQL editor is empty
263 // then copy the code directly into the editor
264 const preElements = document.querySelectorAll('#assistant_response pre');
265 if (preElements.length === 1 && window.editor?.state.doc.toString().trim() === "") {
266 window.editor.dispatch({
267 changes: {
268 from: 0,
269 insert: preElements[0].textContent
270 }
271 });
272 }
273
274 // Similarly, if there is no description, copy the prompt into the description
275 const prompt = document.getElementById("id_assistant_input")?.value;
276 const description = document.getElementById("id_description");
277 if (description?.value === "") {
278 description.value = prompt;
279 }
280
281 setUpCopyButtons();
282 })
283 .catch(error => {
284 console.error('Error:', error);
285 });
286}

Callers 1

setUpAssistantFunction · 0.85

Calls 4

getCsrfTokenFunction · 0.90
getErrorMessageFunction · 0.85
setUpCopyButtonsFunction · 0.85
dispatchMethod · 0.45

Tested by

no test coverage detected