From 2fc3577431d6c44b9aaf47ab84c262a94f2f8fdf Mon Sep 17 00:00:00 2001 From: maver Date: Sat, 1 Jul 2023 20:00:21 +0200 Subject: [PATCH] Throw a recoverable error on invalid character naming --- public/scripts/openai.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 6e8b8bc31..4555c802a 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -410,10 +410,9 @@ function populateChatHistory(prompts, chatCompletion) { prompt.identifier = 'chatHistory-' + index; const chatMessage = Message.fromPrompt(promptManager.preparePrompt(prompt)); - if (true === promptManager.serviceSettings.names_in_completion && - prompt.name && - promptManager.isValidName(prompt.name)) - chatMessage.name = prompt.name; + if (true === promptManager.serviceSettings.names_in_completion && prompt.name) + if (promptManager.isValidName(prompt.name)) chatMessage.name = prompt.name; + else throw InvalidCharacterNameError(); if (chatCompletion.canAfford(chatMessage)) chatCompletion.insertAtStart(chatMessage, 'chatHistory'); else return false; @@ -663,6 +662,10 @@ function prepareOpenAIMessages({ toastr.error('An error occurred while counting tokens: Token budget exceeded.') chatCompletion.log('Token budget exceeded.'); promptManager.error = 'Not enough free tokens for mandatory prompts. Raise your token Limit or disable custom prompts.'; + } else if (error instanceof InvalidCharacterNameError) { + toastr.error('An error occurred while counting tokens: Invalid character name') + chatCompletion.log('Invalid character name'); + promptManager.error = 'The name of at least one character contained whitespaces or special characters. Please check your user and character name'; } else { toastr.error('An unknown error occurred while counting tokens. Further info available in console.') chatCompletion.log('Unexpected error:'); @@ -1234,6 +1237,7 @@ class IdentifierNotFoundError extends Error { } } +// Thrown by ChatCompletion when the token budget is unexpectedly exceeded class TokenBudgetExceededError extends Error { constructor(identifier = '') { super(`Token budged exceeded. Message: ${identifier}`); @@ -1241,6 +1245,15 @@ class TokenBudgetExceededError extends Error { } } +// Thrown when a character name is invalid +class InvalidCharacterNameError extends Error { + constructor(identifier = '') { + super(`Invalid character name. Message: ${identifier}`); + this.name = 'InvalidCharacterName'; + } +} + + class Message { tokens; identifier; role; content; name; constructor(role, content, identifier) {