From bdcb3e52ff272d5b7aa353ca73bda79cf9cd27b5 Mon Sep 17 00:00:00 2001 From: ouoertheo Date: Wed, 31 May 2023 08:01:07 -0500 Subject: [PATCH] Add some instructions. Code readability --- public/scripts/extensions/objective/index.js | 118 ++++++++---------- public/scripts/extensions/objective/style.css | 5 +- 2 files changed, 55 insertions(+), 68 deletions(-) diff --git a/public/scripts/extensions/objective/index.js b/public/scripts/extensions/objective/index.js index a9277fe07..5ab8bd83a 100644 --- a/public/scripts/extensions/objective/index.js +++ b/public/scripts/extensions/objective/index.js @@ -136,36 +136,33 @@ async function checkTaskCompleted() { } else { console.debug(`Checked task completion. taskResponse: ${taskResponse}`) } - } +} // Set a task in extensionPrompt context. Defaults to first incomplete -function setCurrentTask(index=null) { +function setCurrentTask(index = null) { const context = getContext(); - currentTask = {} - - // Default to selecting first incomplete task if not given index, else no task - if (index==null){ - currentTask = globalTasks.find(task=>{return true? !task.completed: false}) - if (currentTask == undefined){ - currentTask = {} - } - } else if (index < globalTasks.length && index > 0){ - currentTask = globalTasks[index] + let currentTask = {}; + + if (index === null) { + currentTask = globalTasks.find(task => !task.completed) || {}; + } else if (index >= 0 && index < globalTasks.length) { + currentTask = globalTasks[index]; } - - // Inject task into extension prompt context - if (Object.keys(currentTask).length > 0){ - context.setExtensionPrompt( - MODULE_NAME, injectPrompts["task"].replace(/{{task}}/gi, currentTask.description), 1, $('#objective-chat-depth').val() - ); - console.info(`Current task in context.extensionPrompts.Objective is ${JSON.stringify(context.extensionPrompts.Objective)}`) + + const { description } = currentTask; + const injectPromptsTask = injectPrompts["task"].replace(/{{task}}/gi, description); + + if (description) { + context.setExtensionPrompt(MODULE_NAME, injectPromptsTask, 1, $('#objective-chat-depth').val()); + console.info(`Current task in context.extensionPrompts.Objective is ${JSON.stringify(context.extensionPrompts.Objective)}`); } else { - context.setExtensionPrompt(MODULE_NAME,'') - console.info(`No current task`) + context.setExtensionPrompt(MODULE_NAME, ''); + console.info(`No current task`); } - saveState() -} + + saveState(); + } @@ -188,6 +185,9 @@ function resetState(){ // function saveState(){ + if (currentChatId == ""){ + currentChatId = getContext().chatId + } extension_settings.objective[currentChatId].objective = globalObjective extension_settings.objective[currentChatId].tasks = globalTasks extension_settings.objective[currentChatId].checkFrequency = $('#objective-check-frequency').val() @@ -211,42 +211,30 @@ window.debugObjectiveExtension = debugObjectiveExtension // Add a single task to the UI and attach event listeners for user edits -function addUiTask(taskIndex, taskComplete, taskDescription){ - let template = ` -
- {{task_index}} - - {{task_description}} -

- ` - - // Define id values - const taskLabelId = `objective-task-label-${taskIndex}` - const taskCompleteId = `objective-task-complete-${taskIndex}` - const taskDescriptionId = `objective-task-description-${taskIndex}` - - // Assign id values and populate current state - template = template.replace(/{{task_index}}/gi,taskIndex) - template = template.replace(/{{task_description}}/gi,taskDescription) - template = template.replace(/{{task_label_id}}/gi,taskLabelId) - template = template.replace(/{{task_complete_id}}/gi,taskCompleteId) - template = template.replace(/{{task_description_id}}/gi,taskDescriptionId) - +function addUiTask(taskIndex, taskComplete, taskDescription) { + const template = ` +
+ ${taskIndex} + + ${taskDescription} +

+ `; + // Add the filled out template - $('#objective-tasks').append(template) - + $('#objective-tasks').append(template); + // Add event listeners and set properties - $(`#${taskCompleteId}`).prop('checked',taskComplete) - $(`#${taskCompleteId}`).on('click', event => { - const index = Number(event.target.id[event.target.id.length - 1]) - globalTasks[index].completed = event.target.checked - setCurrentTask() + $(`#objective-task-complete-${taskIndex}`).prop('checked', taskComplete); + $(`#objective-task-complete-${taskIndex}`).on('click', event => { + const index = Number(event.target.id.split('-').pop()); + globalTasks[index].completed = event.target.checked; + setCurrentTask(); }); - $(`#${taskDescriptionId}`).on('keyup', event => { - const index = Number(event.target.id[event.target.id.length - 1]) - globalTasks[index].description = event.target.textContent + $(`#objective-task-description-${taskIndex}`).on('keyup', event => { + const index = Number(event.target.id.split('-').pop()); + globalTasks[index].description = event.target.textContent; }); -} + } // Populate UI task list function updateUiTaskList() { @@ -269,18 +257,12 @@ async function onGenerateObjectiveClick() { // Update extension prompts function onChatDepthInput() { - if (currentChatId == ""){ - currentChatId = getContext().chatId - } saveState() setCurrentTask() // Ensure extension prompt is updated } // Update how often we check for task completion function onCheckFrequencyInput() { - if (currentChatId == ""){ - currentChatId = getContext().chatId - } saveState() } @@ -320,15 +302,17 @@ jQuery(() => {
- + + Automatically generate tasks for an objective. Will take a moment and populate tasks below

-
- Messages until next task completion check 0 +
+ Messages until next AI task completion check 0 0 to disable auto completion checks +
`; @@ -346,8 +330,10 @@ jQuery(() => { if (currentChatId == undefined){ return } - checkTaskCompleted(); - checkCounter -= 1 + if ($("#objective-check-frequency").val() > 0) { + checkTaskCompleted(); + checkCounter -= 1 + } setCurrentTask(); $('#objective-counter').text(checkCounter) }); diff --git a/public/scripts/extensions/objective/style.css b/public/scripts/extensions/objective/style.css index c14ab4e10..05c4eed93 100644 --- a/public/scripts/extensions/objective/style.css +++ b/public/scripts/extensions/objective/style.css @@ -3,9 +3,10 @@ color: orange; } -[id^='objective-task-label-'] { +/* [id^='objective-task-label-'] { border: 1px solid var(--white30a); border-radius: 10px; padding: 7px; align-items: center; -} \ No newline at end of file +} */ +