mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Migrate objective extension settings to chat_metadata
This commit is contained in:
@ -1,10 +1,9 @@
|
||||
import { callPopup, extension_prompt_types } from "../../../script.js";
|
||||
import { chat_metadata } from "../../../script.js";
|
||||
import { getContext, extension_settings } from "../../extensions.js";
|
||||
import {
|
||||
substituteParams,
|
||||
eventSource,
|
||||
event_types,
|
||||
saveSettingsDebounced
|
||||
} from "../../../script.js";
|
||||
|
||||
const MODULE_NAME = "Objective"
|
||||
@ -31,7 +30,7 @@ const objectivePrompts = {
|
||||
6. Serve the food
|
||||
7. Enjoy eating the meal with {{user}}
|
||||
`,
|
||||
"checkTaskCompleted": `Pause your roleplay. Determine if this task is completed: [{{task}}].
|
||||
"checkTaskCompleted": `Pause your roleplay. Determine if this task is completed: [{{task}}].
|
||||
To do this, examine the most recent messages. Your response must only contain either true or false, nothing other words.
|
||||
Example output:
|
||||
true
|
||||
@ -60,7 +59,7 @@ async function generateQuietPrompt(quiet_prompt) {
|
||||
//###############################//
|
||||
|
||||
// Accepts optional position. Defaults to adding to end of list.
|
||||
function addTask(description, position=null) {
|
||||
function addTask(description, position = null) {
|
||||
position = position ? position != null : position = globalTasks.length
|
||||
globalTasks.splice(position, 0, {
|
||||
"description": description,
|
||||
@ -70,13 +69,13 @@ function addTask(description, position=null) {
|
||||
}
|
||||
|
||||
// Get a task either by index or task description. Return current task if none specified
|
||||
function getTask(index=null, taskDescription=null){
|
||||
function getTask(index = null, taskDescription = null) {
|
||||
let task = {}
|
||||
if (index == null && taskDescription==null) {
|
||||
if (index == null && taskDescription == null) {
|
||||
task = currentTask
|
||||
} else if (index != null){
|
||||
} else if (index != null) {
|
||||
task = globalObjective[index]
|
||||
} else if (taskDescription != null){
|
||||
} else if (taskDescription != null) {
|
||||
task = globalTasks.find(task => {
|
||||
return true ? task.description == description : false
|
||||
})
|
||||
@ -97,6 +96,7 @@ function completeTask(task) {
|
||||
async function generateTasks() {
|
||||
const prompt = substituteParams(objectivePrompts["createTask"].replace(/{{objective}}/gi, globalObjective));
|
||||
console.log(`Generating tasks for objective with prompt`)
|
||||
toastr.info('Generating tasks for objective', 'Please wait...');
|
||||
const taskResponse = await generateQuietPrompt(prompt)
|
||||
globalTasks = []
|
||||
const numberedListPattern = /^\d+\./
|
||||
@ -104,22 +104,23 @@ async function generateTasks() {
|
||||
// Add numbered tasks, store them without the numbers.
|
||||
for (const task of taskResponse.split('\n')) {
|
||||
if (task.match(numberedListPattern) != null) {
|
||||
addTask(task.replace(numberedListPattern,'').trim())
|
||||
addTask(task.replace(numberedListPattern, '').trim())
|
||||
}
|
||||
}
|
||||
updateUiTaskList()
|
||||
console.info(`Response for Objective: '${globalObjective}' was \n'${taskResponse}', \nwhich created tasks \n${JSON.stringify(globalTasks, null, 2)} `)
|
||||
toastr.success(`Generated ${globalTasks.length} tasks`, 'Done!');
|
||||
}
|
||||
|
||||
// Call Quiet Generate to check if a task is completed
|
||||
// Call Quiet Generate to check if a task is completed
|
||||
async function checkTaskCompleted() {
|
||||
// Make sure there are tasks and check is enabled
|
||||
if (currentTask == {} || $('#objective-check-frequency').val() == 0){
|
||||
if (currentTask == {} || $('#objective-check-frequency').val() == 0) {
|
||||
return
|
||||
}
|
||||
|
||||
// Check only at specified interval
|
||||
if (checkCounter > 0){
|
||||
if (checkCounter > 0) {
|
||||
return
|
||||
}
|
||||
checkCounter = $('#objective-check-frequency').val()
|
||||
@ -128,7 +129,7 @@ async function checkTaskCompleted() {
|
||||
const taskResponse = (await generateQuietPrompt(prompt)).toLowerCase()
|
||||
|
||||
// Check response if task complete
|
||||
if (taskResponse.includes("true")){
|
||||
if (taskResponse.includes("true")) {
|
||||
console.info(`Character determined task '${JSON.stringify(currentTask)} is completed.`)
|
||||
completeTask(getTask())
|
||||
} else if (!(taskResponse.includes("false"))) {
|
||||
@ -143,26 +144,26 @@ async function checkTaskCompleted() {
|
||||
function setCurrentTask(index = null) {
|
||||
const context = getContext();
|
||||
let currentTask = {};
|
||||
|
||||
|
||||
if (index === null) {
|
||||
currentTask = globalTasks.find(task => !task.completed) || {};
|
||||
currentTask = globalTasks.find(task => !task.completed) || {};
|
||||
} else if (index >= 0 && index < globalTasks.length) {
|
||||
currentTask = globalTasks[index];
|
||||
currentTask = globalTasks[index];
|
||||
}
|
||||
|
||||
|
||||
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)}`);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -175,37 +176,43 @@ const defaultSettings = {
|
||||
objective: "",
|
||||
tasks: [],
|
||||
chatDepth: 2,
|
||||
checkFrequency:3,
|
||||
checkFrequency: 3,
|
||||
hideTasks: false
|
||||
}
|
||||
|
||||
// Convenient single call. Not much at the moment.
|
||||
function resetState(){
|
||||
function resetState() {
|
||||
loadSettings();
|
||||
}
|
||||
|
||||
//
|
||||
function saveState(){
|
||||
if (currentChatId == ""){
|
||||
currentChatId = getContext().chatId
|
||||
//
|
||||
function saveState() {
|
||||
const context = getContext();
|
||||
|
||||
if (currentChatId == "") {
|
||||
currentChatId = context.chatId
|
||||
}
|
||||
extension_settings.objective[currentChatId].objective = globalObjective
|
||||
extension_settings.objective[currentChatId].tasks = globalTasks
|
||||
extension_settings.objective[currentChatId].checkFrequency = $('#objective-check-frequency').val()
|
||||
extension_settings.objective[currentChatId].chatDepth = $('#objective-chat-depth').val()
|
||||
extension_settings.objective[currentChatId].hideTasks = $('#objective-hide-tasks').prop('checked')
|
||||
saveSettingsDebounced()
|
||||
|
||||
chat_metadata['objective'] = {
|
||||
objective: globalObjective,
|
||||
tasks: globalTasks,
|
||||
checkFrequency: $('#objective-check-frequency').val(),
|
||||
chatDepth: $('#objective-chat-depth').val(),
|
||||
hideTasks: $('#objective-hide-tasks').prop('checked'),
|
||||
}
|
||||
|
||||
context.saveMetadata();
|
||||
}
|
||||
|
||||
// Dump core state
|
||||
function debugObjectiveExtension(){
|
||||
function debugObjectiveExtension() {
|
||||
console.log(JSON.stringify({
|
||||
"currentTask": currentTask,
|
||||
"currentChatId": currentChatId,
|
||||
"checkCounter": checkCounter,
|
||||
"globalObjective": globalObjective,
|
||||
"globalTasks": globalTasks,
|
||||
"extension_settings": extension_settings.objective[currentChatId],
|
||||
"extension_settings": chat_metadata['objective'],
|
||||
}, null, 2))
|
||||
}
|
||||
|
||||
@ -221,22 +228,22 @@ function addUiTask(taskIndex, taskComplete, taskDescription) {
|
||||
<span class="text_pole" style="display: block" id="objective-task-description-${taskIndex}" contenteditable>${taskDescription}</span>
|
||||
</div><br>
|
||||
`;
|
||||
|
||||
|
||||
// Add the filled out template
|
||||
$('#objective-tasks').append(template);
|
||||
|
||||
|
||||
// Add event listeners and set properties
|
||||
$(`#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();
|
||||
const index = Number(event.target.id.split('-').pop());
|
||||
globalTasks[index].completed = event.target.checked;
|
||||
setCurrentTask();
|
||||
});
|
||||
$(`#objective-task-description-${taskIndex}`).on('keyup', event => {
|
||||
const index = Number(event.target.id.split('-').pop());
|
||||
globalTasks[index].description = event.target.textContent;
|
||||
const index = Number(event.target.id.split('-').pop());
|
||||
globalTasks[index].description = event.target.textContent;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Populate UI task list
|
||||
function updateUiTaskList() {
|
||||
@ -268,8 +275,8 @@ function onCheckFrequencyInput() {
|
||||
saveState()
|
||||
}
|
||||
|
||||
function onHideTasksInput(){
|
||||
$('#objective-tasks').prop('hidden',$('#objective-hide-tasks').prop('checked'))
|
||||
function onHideTasksInput() {
|
||||
$('#objective-tasks').prop('hidden', $('#objective-hide-tasks').prop('checked'))
|
||||
saveState()
|
||||
}
|
||||
|
||||
@ -281,23 +288,29 @@ function loadSettings() {
|
||||
if (currentChatId == undefined) {
|
||||
return
|
||||
}
|
||||
if (!(currentChatId in extension_settings.objective)) {
|
||||
extension_settings.objective[currentChatId] = {}
|
||||
Object.assign(extension_settings.objective[currentChatId], defaultSettings)
|
||||
|
||||
// Migrate existing settings
|
||||
if (currentChatId in extension_settings.objective) {
|
||||
chat_metadata['objective'] = extension_settings.objective[currentChatId];
|
||||
delete extension_settings.objective[currentChatId];
|
||||
}
|
||||
|
||||
if (!('objective' in chat_metadata)) {
|
||||
Object.assign(chat_metadata, { objective: defaultSettings });
|
||||
}
|
||||
|
||||
// Update globals
|
||||
globalObjective = extension_settings.objective[currentChatId].objective
|
||||
globalTasks = extension_settings.objective[currentChatId].tasks
|
||||
checkCounter = extension_settings.objective[currentChatId].checkFrequency
|
||||
globalObjective = chat_metadata['objective'].objective
|
||||
globalTasks = chat_metadata['objective'].tasks
|
||||
checkCounter = chat_metadata['objective'].checkFrequency
|
||||
|
||||
// Update UI elements
|
||||
$('#objective-counter').text(checkCounter)
|
||||
$("#objective-text").text(globalObjective)
|
||||
updateUiTaskList()
|
||||
$('#objective-chat-depth').val(extension_settings.objective[currentChatId].chatDepth)
|
||||
$('#objective-check-frequency').val(extension_settings.objective[currentChatId].checkFrequency)
|
||||
$('#objective-hide-tasks').prop('checked',extension_settings.objective[currentChatId].hideTasks)
|
||||
$('#objective-chat-depth').val(chat_metadata['objective'].chatDepth)
|
||||
$('#objective-check-frequency').val(chat_metadata['objective'].checkFrequency)
|
||||
$('#objective-hide-tasks').prop('checked', chat_metadata['objective'].hideTasks)
|
||||
onHideTasksInput()
|
||||
setCurrentTask()
|
||||
}
|
||||
@ -313,32 +326,42 @@ jQuery(() => {
|
||||
<div class="inline-drawer-content">
|
||||
<label for="objective-text"><small>Enter an objective and generate tasks. The AI will attempt to complete tasks autonomously</small></label>
|
||||
<textarea id="objective-text" type="text" class="text_pole textarea_compact" rows="4"></textarea>
|
||||
<label class="checkbox_label"><input id="objective-generate" class="menu_button" type="submit" value="Generate Tasks" />
|
||||
<small>Automatically generate tasks for Objective. Takes a moment.</small></label></br>
|
||||
<div class="objective_block">
|
||||
<input id="objective-generate" class="menu_button" type="submit" value="Generate Tasks" />
|
||||
<small>Automatically generate tasks for Objective. Takes a moment.</small>
|
||||
</div>
|
||||
</br>
|
||||
<label class="checkbox_label"><input id="objective-hide-tasks" type="checkbox"> Hide Tasks</label><br>
|
||||
<div id="objective-tasks"> </div>
|
||||
<label for="objective-chat-depth">In-chat @ Depth</label>
|
||||
<input id="objective-chat-depth" class="text_pole widthUnset" type="number" min="0" max="99" /><br>
|
||||
<label for="objective-check-frequency">Task Check Frequency</label>
|
||||
<input id="objective-check-frequency" class="text_pole widthUnset" type="number" min="" max="99" /><small> (0 = disabled) </small><br>
|
||||
<span> Messages until next AI task completion check <span id="objective-counter">0</span></span>
|
||||
<div class="objective_block">
|
||||
<div class="objective_block objective_block_control flex1">
|
||||
<label for="objective-chat-depth">In-chat @ Depth</label>
|
||||
<input id="objective-chat-depth" class="text_pole widthUnset" type="number" min="0" max="99" />
|
||||
</div>
|
||||
<div class="objective_block objective_block_control flex1">
|
||||
<label for="objective-check-frequency">Task Check Frequency</label>
|
||||
<input id="objective-check-frequency" class="text_pole widthUnset" type="number" min="0" max="99" />
|
||||
<small>(0 = disabled)</small>
|
||||
</div>
|
||||
</div>
|
||||
<span> Messages until next AI task completion check <span id="objective-counter">0</span></span>
|
||||
<hr class="sysHR">
|
||||
</div>
|
||||
</div>`;
|
||||
|
||||
|
||||
$('#extensions_settings').append(settingsHtml);
|
||||
$('#objective-generate').on('click', onGenerateObjectiveClick)
|
||||
$('#objective-chat-depth').on('input',onChatDepthInput)
|
||||
$("#objective-check-frequency").on('input',onCheckFrequencyInput)
|
||||
$('#objective-chat-depth').on('input', onChatDepthInput)
|
||||
$("#objective-check-frequency").on('input', onCheckFrequencyInput)
|
||||
$('#objective-hide-tasks').on('click', onHideTasksInput)
|
||||
loadSettings()
|
||||
|
||||
|
||||
eventSource.on(event_types.CHAT_CHANGED, () => {
|
||||
resetState()
|
||||
});
|
||||
|
||||
eventSource.on(event_types.MESSAGE_RECEIVED, () => {
|
||||
if (currentChatId == undefined){
|
||||
if (currentChatId == undefined) {
|
||||
return
|
||||
}
|
||||
if ($("#objective-check-frequency").val() > 0) {
|
||||
|
Reference in New Issue
Block a user