Skip to content

Commit

Permalink
autopopulate the code if editor is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisclark committed Apr 26, 2024
1 parent 33c163a commit bfe4b32
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion explorer/assistant/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def run_assistant(request_data, user):
user_prompt += f"## User's Request to Assistant ##\n\n{request_data['assistant_request']}\n\n"

prompt = {
"system": ExplorerValue.objects.get_item(ExplorerValue.ASSISTANT_SYSTEM_PROMPT),
"system": ExplorerValue.objects.get_item(ExplorerValue.ASSISTANT_SYSTEM_PROMPT).value,
"user": user_prompt
}

Expand Down
2 changes: 1 addition & 1 deletion explorer/migrations/0016_alter_explorervalue_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def insert_assistant_prompt(apps, schema_editor):

ExplorerValue = apps.get_model('explorer', 'ExplorerValue')
ExplorerValue.objects.create(
key=ExplorerValue.ASSISTANT_SYSTEM_PROMPT,
key="ASP",
value="""You are a data analyst's assistant and will be asked write or modify a SQL query to assist a business
user with their analysis. The user will provide a prompt of what they are looking for help with, and may also
provide SQL they have written so far, relevant table schema, and sample rows from the tables they are querying.
Expand Down
15 changes: 15 additions & 0 deletions explorer/src/js/assistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ function submitAssistantAsk() {
const output = DOMPurify.sanitize(marked.parse(data.message));
document.getElementById("assistant_response").innerHTML = output;
document.getElementById("assistant_spinner").classList.add('d-none');

// If there is exactly one code block in the response and the SQL editor is empty
// then copy the code directly into the editor
const preElements = document.querySelectorAll('#assistant_response pre');
if (preElements.length === 1) {
if (window.editor?.state.doc.toString().trim() === "") {
window.editor.dispatch({
changes: {
from: 0,
insert: preElements[0].textContent
}
});
}
}

setUpCopyButtons();
})
.catch(error => {
Expand Down

0 comments on commit bfe4b32

Please sign in to comment.