diff --git a/public/scripts/PromptManager.js b/public/scripts/PromptManager.js index c46b30f78..801748ee9 100644 --- a/public/scripts/PromptManager.js +++ b/public/scripts/PromptManager.js @@ -1,6 +1,6 @@ import {DraggablePromptListModule as DraggableList} from "./DraggableList.js"; import {event_types, eventSource, substituteParams} from "../script.js"; -import {IdentifierNotFoundError, TokenHandler} from "./openai.js"; +import {TokenHandler} from "./openai.js"; class Prompt { identifier; role; content; name; system_prompt; @@ -240,14 +240,26 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti }); }; -PromptManagerModule.prototype.render = function () { +/** + * Main rendering function + * + * @param afterTryGenerate - Whether a dry run should be attempted before rendering + */ +PromptManagerModule.prototype.render = function (afterTryGenerate = true) { if (null === this.activeCharacter) return; this.error = null; - this.tryGenerate().then(() => { + + if (true === afterTryGenerate) { + this.tryGenerate().then(() => { + this.renderPromptManager(); + this.renderPromptManagerListItems() + this.makeDraggable(); + }); + } else { this.renderPromptManager(); this.renderPromptManagerListItems() this.makeDraggable(); - }); + } } /** @@ -590,6 +602,7 @@ PromptManagerModule.prototype.getPromptCollection = function () { } PromptManagerModule.prototype.populateTokenHandler = function(messageCollection) { + this.tokenHandler.resetCounts(); const counts = this.tokenHandler.getCounts(); messageCollection.getCollection().forEach((message) => { counts[message.identifier] = message.getTokens(); diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 5412eed62..3c46d5caa 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -610,6 +610,7 @@ function prepareOpenAIMessages({ } } finally { promptManager.populateTokenHandler(chatCompletion.getMessages()); + promptManager.render(false); } const chat = chatCompletion.getChat(); @@ -1075,6 +1076,10 @@ class TokenHandler { return this.counts; } + resetCounts() { + Object.keys(this.counts).forEach((key) => this.counts[key] = 0 ); + } + setCounts(counts) { this.counts = counts; }