diff --git a/public/css/tags.css b/public/css/tags.css
index 534f92495..87b428a9f 100644
--- a/public/css/tags.css
+++ b/public/css/tags.css
@@ -134,7 +134,6 @@
.open_alternate_greetings {
margin: 0;
aspect-ratio: 1 / 1;
- height: 2rem;
}
.tag.selected {
@@ -164,4 +163,4 @@
-1px 1px 0px black,
1px -1px 0px black;
opacity: 1;
-}
\ No newline at end of file
+}
diff --git a/public/index.html b/public/index.html
index 0d73bd76a..bafce0fe9 100644
--- a/public/index.html
+++ b/public/index.html
@@ -3097,10 +3097,11 @@
-
-
-
-
-
Description
-
?
+
+
+ Calculating... Total Tokens
+
+
+
+
+ Tokens: counting...
+
@@ -3192,6 +3200,10 @@
+
+ Tokens: counting...
+
+
Creator's Notes
@@ -3403,10 +3415,16 @@
Main Prompt
+
+ Tokens: counting...
+
Jailbreak
+
+ Tokens: counting...
+
@@ -3450,6 +3468,9 @@
?
+
+ Tokens: counting...
+
@@ -3460,6 +3481,9 @@
+
+ Tokens: counting...
+
@@ -3480,6 +3504,9 @@
Important to set the character's writing style. ?
+
+ Tokens: counting...
+
diff --git a/public/script.js b/public/script.js
index da97371f9..96e094b3e 100644
--- a/public/script.js
+++ b/public/script.js
@@ -6696,7 +6696,7 @@ async function createOrEditCharacter(e) {
},
});
} else {
- $("#result_info").html("Name not entered");
+ toastr.error('Name is required');
}
} else {
let url = '/editcharacter';
@@ -6789,7 +6789,6 @@ async function createOrEditCharacter(e) {
},
error: function (jqXHR, exception) {
$("#create_button").removeAttr("disabled");
- $("#result_info").html("Error: no connection");
console.log('Error! Either a file with the same name already existed, or the image file provided was in an invalid format. Double check that the image is not a webp.');
toastr.error('Something went wrong while saving the character, or the image file provided was in an invalid format. Double check that the image is not a webp.');
},
diff --git a/public/scripts/RossAscends-mods.js b/public/scripts/RossAscends-mods.js
index 3a2d0a395..9a8d3597b 100644
--- a/public/scripts/RossAscends-mods.js
+++ b/public/scripts/RossAscends-mods.js
@@ -22,11 +22,6 @@ import {
selectCharacterById,
} from "../script.js";
-import {
- characterStatsHandler,
-} from "./stats.js";
-
-
import {
power_user,
send_on_enter_options,
@@ -38,7 +33,7 @@ import {
SECRET_KEYS,
secret_state,
} from "./secrets.js";
-import { debounce, delay } from "./utils.js";
+import { debounce, delay, getStringHash } from "./utils.js";
import { chat_completion_sources, oai_settings } from "./openai.js";
var NavToggle = document.getElementById("nav-toggle");
@@ -238,7 +233,6 @@ $("#rm_button_create").on("click", function () { //when "+New Ch
create_save_first_message = "";
create_save_scenario = "";
create_save_mes_example = "";
- $("#result_info").html('Type to start counting tokens!');
});
//when any input is made to the create/edit character form textareas
$("#rm_ch_create_block").on("input", function () { countTokensDebounced(); });
@@ -246,96 +240,41 @@ $("#rm_ch_create_block").on("input", function () { countTokensDebounced(); });
$("#character_popup").on("input", function () { countTokensDebounced(); });
//function:
export function RA_CountCharTokens() {
- //console.log('RA_TC -- starting with this_chid = ' + this_chid);
- if (menu_type === "create") { //if new char
- function saveFormVariables() {
- create_save_name = $("#character_name_pole").val();
- create_save_description = $("#description_textarea").val();
- create_save_first_message = $("#firstmessage_textarea").val();
+ let total_tokens = 0;
+
+ $('[data-token-counter]').each(function () {
+ const counter = $(this);
+ const input = $(document.getElementById(counter.data('token-counter')));
+ const value = input.val();
+
+ if (input.length === 0) {
+ counter.text('Invalid input reference');
+ return;
}
- function savePopupVariables() {
- create_save_personality = $("#personality_textarea").val();
- create_save_scenario = $("#scenario_pole").val();
- create_save_mes_example = $("#mes_example_textarea").val();
+ if (!value) {
+ counter.text(0);
+ return;
}
- saveFormVariables();
- savePopupVariables();
+ const valueHash = getStringHash(value);
- //count total tokens, including those that will be removed from context once chat history is long
- let count_string = [
- create_save_name,
- create_save_description,
- create_save_personality,
- create_save_scenario,
- create_save_first_message,
- create_save_mes_example,
- ].join('\n').replace(/\r/gm, '').trim();
- count_tokens = getTokenCount(count_string);
-
- //count permanent tokens that will never get flushed out of context
- let perm_string = [
- create_save_name,
- create_save_description,
- create_save_personality,
- create_save_scenario,
- // add examples to permanent if they are pinned
- (power_user.pin_examples ? create_save_mes_example : ''),
- ].join('\n').replace(/\r/gm, '').trim();
- perm_tokens = getTokenCount(perm_string);
-
- } else {
- if (this_chid !== undefined && this_chid !== "invalid-safety-id") { // if we are counting a valid pre-saved char
-
- //same as above, all tokens including temporary ones
- let count_string = [
- characters[this_chid].description,
- characters[this_chid].personality,
- characters[this_chid].scenario,
- characters[this_chid].first_mes,
- characters[this_chid].mes_example,
- ].join('\n').replace(/\r/gm, '').trim();
- count_tokens = getTokenCount(count_string);
-
- //permanent tokens count
- let perm_string = [
- characters[this_chid].name,
- characters[this_chid].description,
- characters[this_chid].personality,
- characters[this_chid].scenario,
- // add examples to permanent if they are pinned
- (power_user.pin_examples ? characters[this_chid].mes_example : ''),
- ].join('\n').replace(/\r/gm, '').trim();
- perm_tokens = getTokenCount(perm_string);
- // if neither, probably safety char or some error in loading
- } else { console.debug("RA_TC -- no valid char found, closing."); }
- }
- //label rm_stats_button with a tooltip indicating stats
- $("#result_info").html(`${count_tokens} Tokens (${perm_tokens} Permanent)
-
- `);
- // display the counted tokens
- const tokenLimit = Math.max(((main_api !== 'openai' ? max_context : oai_settings.openai_max_context) / 2), 1024);
- if (count_tokens < tokenLimit && perm_tokens < tokenLimit) {
-
- } else {
- $("#result_info").html(`
-
-
-
- ${count_tokens}
Tokens (${perm_tokens}
Permanent)
-
-
-
-
-
`);
-
-
- } //warn if either are over 1024
- $(".rm_stats_button").on('click', function () {
- characterStatsHandler(characters, this_chid);
+ if (input.data('last-value-hash') === valueHash) {
+ total_tokens += Number(counter.text());
+ } else {
+ const tokens = getTokenCount(value);
+ counter.text(tokens);
+ total_tokens += tokens;
+ input.data('last-value-hash', valueHash);
+ }
});
+
+ // Warn if total tokens exceeds the limit of half the max context
+ const tokenLimit = Math.max(((main_api !== 'openai' ? max_context : oai_settings.openai_max_context) / 2), 1024);
+ const showWarning = (total_tokens > tokenLimit);
+ $('#result_info_total_tokens').text(total_tokens);
+ $('#result_info_text').toggleClass('neutral_warning', showWarning);
+ $('#chartokenwarning').toggle(showWarning);
}
/**
* Auto load chat with the last active character or group.
diff --git a/public/scripts/stats.js b/public/scripts/stats.js
index 7a60df89f..d80997223 100644
--- a/public/scripts/stats.js
+++ b/public/scripts/stats.js
@@ -1,5 +1,5 @@
// statsHelper.js
-import { getRequestHeaders, callPopup, token, chat } from "../script.js";
+import { getRequestHeaders, callPopup, characters, this_chid } from "../script.js";
import { humanizeGenTime } from "./RossAscends-mods.js";
let charStats = {};
@@ -304,4 +304,10 @@ async function statMesProcess(line, type, characters, this_chid, oldMesssage) {
updateStats();
}
+jQuery(() => {
+ $(".rm_stats_button").on('click', function () {
+ characterStatsHandler(characters, this_chid);
+ });
+})
+
export { userStatsHandler, characterStatsHandler, getStats, statMesProcess, charStats };
diff --git a/public/style.css b/public/style.css
index 685b65246..2227a532f 100644
--- a/public/style.css
+++ b/public/style.css
@@ -1178,6 +1178,7 @@ input[type="file"] {
font-size: calc(var(--mainFontSize) * 0.9);
width: 100%;
text-align: right;
+ margin-bottom: 5px;
}
.floating_prompt_settings textarea {
@@ -1586,7 +1587,8 @@ input[type=search]:focus::-webkit-search-cancel-button {
}
#description_div {
- position: relative;
+ display: flex;
+ align-items: center;
}
#name_div {
@@ -1711,11 +1713,9 @@ grammarly-extension {
}
#result_info {
- font-size: calc(var(--mainFontSize) - 0.1rem);
- font-weight: bold;
- margin-bottom: 5px;
+ font-size: calc(var(--mainFontSize) * 0.9);
display: flex;
- align-items: center;
+ align-items: baseline;
}
.rm_stats_button {