mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-04-17 04:07:21 +02:00
Throw a recoverable error on invalid character naming
This commit is contained in:
parent
facf625cac
commit
2fc3577431
@ -410,10 +410,9 @@ function populateChatHistory(prompts, chatCompletion) {
|
|||||||
prompt.identifier = 'chatHistory-' + index;
|
prompt.identifier = 'chatHistory-' + index;
|
||||||
const chatMessage = Message.fromPrompt(promptManager.preparePrompt(prompt));
|
const chatMessage = Message.fromPrompt(promptManager.preparePrompt(prompt));
|
||||||
|
|
||||||
if (true === promptManager.serviceSettings.names_in_completion &&
|
if (true === promptManager.serviceSettings.names_in_completion && prompt.name)
|
||||||
prompt.name &&
|
if (promptManager.isValidName(prompt.name)) chatMessage.name = prompt.name;
|
||||||
promptManager.isValidName(prompt.name))
|
else throw InvalidCharacterNameError();
|
||||||
chatMessage.name = prompt.name;
|
|
||||||
|
|
||||||
if (chatCompletion.canAfford(chatMessage)) chatCompletion.insertAtStart(chatMessage, 'chatHistory');
|
if (chatCompletion.canAfford(chatMessage)) chatCompletion.insertAtStart(chatMessage, 'chatHistory');
|
||||||
else return false;
|
else return false;
|
||||||
@ -663,6 +662,10 @@ function prepareOpenAIMessages({
|
|||||||
toastr.error('An error occurred while counting tokens: Token budget exceeded.')
|
toastr.error('An error occurred while counting tokens: Token budget exceeded.')
|
||||||
chatCompletion.log('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.';
|
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 {
|
} else {
|
||||||
toastr.error('An unknown error occurred while counting tokens. Further info available in console.')
|
toastr.error('An unknown error occurred while counting tokens. Further info available in console.')
|
||||||
chatCompletion.log('Unexpected error:');
|
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 {
|
class TokenBudgetExceededError extends Error {
|
||||||
constructor(identifier = '') {
|
constructor(identifier = '') {
|
||||||
super(`Token budged exceeded. Message: ${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 {
|
class Message {
|
||||||
tokens; identifier; role; content; name;
|
tokens; identifier; role; content; name;
|
||||||
constructor(role, content, identifier) {
|
constructor(role, content, identifier) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user