Throw a recoverable error on invalid character naming

This commit is contained in:
maver 2023-07-01 20:00:21 +02:00
parent facf625cac
commit 2fc3577431
1 changed files with 17 additions and 4 deletions

View File

@ -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) {