mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
A little error-proofing to chat loading and history viewing
1. revised the new date format (reformatted to be a function so it would return new dates each time called) 2. re-added new date format functionality as I had previously rolled it back while debugging the avatar loading issues discovered in last push. 3. resolved some issues related to loading past chats for bots with no chat save files. Usually this wouldn't happen, as a new (or previous) chat is loaded each time the character is selected. But in cases where the files are deleted after being created, the chat history loading screen would remain in a 'loading.svg' loop. I added errorhandling to avoid this, and to kick GetAllCharaChats() from trying to read empty arrays. 4. increased potential size of past chats popup box
This commit is contained in:
@@ -61,15 +61,18 @@
|
|||||||
//New chats made with characters will use this new formatting.
|
//New chats made with characters will use this new formatting.
|
||||||
//Useable variable is (( humanizedISO8601Datetime ))
|
//Useable variable is (( humanizedISO8601Datetime ))
|
||||||
|
|
||||||
var baseDate = new Date(Date.now());
|
function humanizedISO8601DateTime() {
|
||||||
var humanYear = baseDate.getFullYear();
|
let baseDate = new Date(Date.now());
|
||||||
var humanMonth = (baseDate.getMonth()+1);
|
let humanYear = baseDate.getFullYear();
|
||||||
var humanDate = baseDate.getDate();
|
let humanMonth = (baseDate.getMonth()+1);
|
||||||
var humanHour = (baseDate.getHours() < 10? '0' : '') + baseDate.getHours();
|
let humanDate = baseDate.getDate();
|
||||||
var humanMinute = (baseDate.getMinutes() < 10? '0' : '') + baseDate.getMinutes();
|
let humanHour = (baseDate.getHours() < 10? '0' : '') + baseDate.getHours();
|
||||||
var humanSecond = (baseDate.getSeconds() < 10? '0' : '') + baseDate.getSeconds();
|
let humanMinute = (baseDate.getMinutes() < 10? '0' : '') + baseDate.getMinutes();
|
||||||
var humanMillisecond = (baseDate.getMilliseconds() < 10? '0' : '') + baseDate.getMilliseconds();
|
let humanSecond = (baseDate.getSeconds() < 10? '0' : '') + baseDate.getSeconds();
|
||||||
var humanizedISO8601DateTime = (humanYear+"-"+humanMonth+"-"+humanDate+" @"+humanHour+"h "+humanMinute+"m "+humanSecond+"s "+humanMillisecond+"ms");
|
let humanMillisecond = (baseDate.getMilliseconds() < 10? '0' : '') + baseDate.getMilliseconds();
|
||||||
|
let HumanizedDateTime = (humanYear+"-"+humanMonth+"-"+humanDate+" @"+humanHour+"h "+humanMinute+"m "+humanSecond+"s "+humanMillisecond+"ms");
|
||||||
|
return HumanizedDateTime;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
var default_ch_mes = "Hello";
|
var default_ch_mes = "Hello";
|
||||||
@@ -673,7 +676,7 @@
|
|||||||
chat[chat.length-1]['name'] = name1;
|
chat[chat.length-1]['name'] = name1;
|
||||||
chat[chat.length-1]['is_user'] = true;
|
chat[chat.length-1]['is_user'] = true;
|
||||||
chat[chat.length-1]['is_name'] = true;
|
chat[chat.length-1]['is_name'] = true;
|
||||||
chat[chat.length-1]['send_date'] = Date.now();
|
chat[chat.length-1]['send_date'] = humanizedISO8601DateTime();
|
||||||
chat[chat.length-1]['mes'] = textareaText;
|
chat[chat.length-1]['mes'] = textareaText;
|
||||||
addOneMessage(chat[chat.length-1]);
|
addOneMessage(chat[chat.length-1]);
|
||||||
}
|
}
|
||||||
@@ -1090,12 +1093,13 @@
|
|||||||
chat[chat.length-1]['name'] = name2;
|
chat[chat.length-1]['name'] = name2;
|
||||||
chat[chat.length-1]['is_user'] = false;
|
chat[chat.length-1]['is_user'] = false;
|
||||||
chat[chat.length-1]['is_name'] = this_mes_is_name;
|
chat[chat.length-1]['is_name'] = this_mes_is_name;
|
||||||
chat[chat.length-1]['send_date'] = Date.now();
|
chat[chat.length-1]['send_date'] = humanizedISO8601DateTime();
|
||||||
getMessage = $.trim(getMessage);
|
getMessage = $.trim(getMessage);
|
||||||
chat[chat.length-1]['mes'] = getMessage;
|
chat[chat.length-1]['mes'] = getMessage;
|
||||||
addOneMessage(chat[chat.length-1]);
|
addOneMessage(chat[chat.length-1]);
|
||||||
$( "#send_but" ).css("display", "inline");
|
$( "#send_but" ).css("display", "inline");
|
||||||
$( "#loading_mes" ).css("display", "none");
|
$( "#loading_mes" ).css("display", "none");
|
||||||
|
console.log('/savechat called by /Generate');
|
||||||
saveChat();
|
saveChat();
|
||||||
}else{
|
}else{
|
||||||
//console.log('run force_name2 protocol');
|
//console.log('run force_name2 protocol');
|
||||||
@@ -1157,6 +1161,7 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
async function getChat() {
|
async function getChat() {
|
||||||
|
console.log('/getChat entered');
|
||||||
//console.log(characters[this_chid].chat);
|
//console.log(characters[this_chid].chat);
|
||||||
jQuery.ajax({
|
jQuery.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
@@ -1177,13 +1182,16 @@
|
|||||||
}
|
}
|
||||||
//chat = data;
|
//chat = data;
|
||||||
chat_create_date = chat[0]['create_date'];
|
chat_create_date = chat[0]['create_date'];
|
||||||
|
console.log('/getchat saw chat_create_date: '+chat_create_date);
|
||||||
chat.shift();
|
chat.shift();
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
chat_create_date = Date.now();
|
chat_create_date = humanizedISO8601DateTime();
|
||||||
}
|
}
|
||||||
//console.log(chat);
|
//console.log(chat);
|
||||||
|
console.log('getChatResults called by /getchat');
|
||||||
getChatResult();
|
getChatResult();
|
||||||
|
console.log('savechat called by /getchat');
|
||||||
saveChat();
|
saveChat();
|
||||||
},
|
},
|
||||||
error: function (jqXHR, exception) {
|
error: function (jqXHR, exception) {
|
||||||
@@ -1213,7 +1221,7 @@
|
|||||||
chat[0]['name'] = name2;
|
chat[0]['name'] = name2;
|
||||||
chat[0]['is_user'] = false;
|
chat[0]['is_user'] = false;
|
||||||
chat[0]['is_name'] = true;
|
chat[0]['is_name'] = true;
|
||||||
chat[0]['send_date'] = Date.now();
|
chat[0]['send_date'] = humanizedISO8601DateTime();
|
||||||
if(characters[this_chid].first_mes != ""){
|
if(characters[this_chid].first_mes != ""){
|
||||||
chat[0]['mes'] = characters[this_chid].first_mes;
|
chat[0]['mes'] = characters[this_chid].first_mes;
|
||||||
}else{
|
}else{
|
||||||
@@ -1582,11 +1590,11 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
//Make a new chat for selected character
|
||||||
if(popup_type == 'new_chat' && this_chid != undefined && menu_type != "create"){//Fix it; New chat doesn't create while open create character menu
|
if(popup_type == 'new_chat' && this_chid != undefined && menu_type != "create"){//Fix it; New chat doesn't create while open create character menu
|
||||||
clearChat();
|
clearChat();
|
||||||
chat.length = 0;
|
chat.length = 0;
|
||||||
characters[this_chid].chat = Date.now(); //RossAscends: added character name to new chat filenames and replaced Date.now() with humanizedISO8601DateTime;
|
characters[this_chid].chat = (name2 +' - '+ humanizedISO8601DateTime()); //RossAscends: added character name to new chat filenames and replaced Date.now() with humanizedISO8601DateTime;
|
||||||
//characters[this_chid].chat = (name2 +' - '+ humanizedISO8601DateTime); //RossAscends: added character name to new chat filenames and replaced Date.now() with humanizedISO8601DateTime;
|
|
||||||
$("#selected_chat_pole").val(characters[this_chid].chat);
|
$("#selected_chat_pole").val(characters[this_chid].chat);
|
||||||
timerSaveEdit = setTimeout(() => {$("#create_button").click();},durationSaveEdit);
|
timerSaveEdit = setTimeout(() => {$("#create_button").click();},durationSaveEdit);
|
||||||
getChat();
|
getChat();
|
||||||
@@ -1720,7 +1728,8 @@
|
|||||||
if($("#form_create").attr("actiontype") == "createcharacter"){
|
if($("#form_create").attr("actiontype") == "createcharacter"){
|
||||||
|
|
||||||
if($("#character_name_pole").val().length > 0){
|
if($("#character_name_pole").val().length > 0){
|
||||||
|
console.log('/createcharacter entered');
|
||||||
|
console.log('CharCreate formData: '+formData);
|
||||||
jQuery.ajax({
|
jQuery.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
url: '/createcharacter',
|
url: '/createcharacter',
|
||||||
@@ -1763,7 +1772,7 @@
|
|||||||
select_rm_info("Character created");
|
select_rm_info("Character created");
|
||||||
|
|
||||||
$('#rm_info_block').transition({ opacity: 1.0 ,duration: 2000});
|
$('#rm_info_block').transition({ opacity: 1.0 ,duration: 2000});
|
||||||
|
console.log('/getcharacters called after /createchracter');
|
||||||
getCharacters();
|
getCharacters();
|
||||||
}else{
|
}else{
|
||||||
$('#result_info').html(html);
|
$('#result_info').html(html);
|
||||||
@@ -1777,7 +1786,7 @@
|
|||||||
$('#result_info').html("Name not entered");
|
$('#result_info').html("Name not entered");
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
console.log($("#add_avatar_button").val());
|
console.log('Avatar Button Value:'+$("#add_avatar_button").val());
|
||||||
|
|
||||||
jQuery.ajax({
|
jQuery.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
@@ -2576,6 +2585,7 @@
|
|||||||
});
|
});
|
||||||
//Select chat
|
//Select chat
|
||||||
async function getAllCharaChats() {
|
async function getAllCharaChats() {
|
||||||
|
console.log('entered getAllCharaChats');
|
||||||
$('#select_chat_div').html('');
|
$('#select_chat_div').html('');
|
||||||
//console.log(characters[this_chid].chat);
|
//console.log(characters[this_chid].chat);
|
||||||
jQuery.ajax({
|
jQuery.ajax({
|
||||||
@@ -2591,20 +2601,24 @@
|
|||||||
success: function(data){
|
success: function(data){
|
||||||
$('#load_select_chat_div').css('display', 'none');
|
$('#load_select_chat_div').css('display', 'none');
|
||||||
let dataArr = Object.values(data);
|
let dataArr = Object.values(data);
|
||||||
|
console.log('dataArr = '+ Object.values(data));
|
||||||
data = dataArr.sort((a, b) => a['file_name'].localeCompare(b['file_name']));
|
data = dataArr.sort((a, b) => a['file_name'].localeCompare(b['file_name']));
|
||||||
data = data.reverse();
|
data = data.reverse();
|
||||||
|
|
||||||
for (const key in data) {
|
for (const key in data) {
|
||||||
let strlen = 40;
|
let strlen = 40;
|
||||||
let mes = data[key]['mes'];
|
let mes = data[key]['mes'];
|
||||||
if(mes.length > strlen){
|
if(mes !== undefined){
|
||||||
mes = '...'+mes.substring(mes.length - strlen);
|
if(mes.length > strlen){
|
||||||
}
|
mes = '...'+mes.substring(mes.length - strlen);
|
||||||
$('#select_chat_div').append('<div class="select_chat_block" file_name="'+data[key]['file_name']+'"><div class=avatar><img src="characters/'+characters[this_chid]['avatar']+'""></div><div class="select_chat_block_filename">'+data[key]['file_name']+'</div><div class="select_chat_block_mes">'+mes+'</div></div>');
|
}
|
||||||
if(characters[this_chid]['chat'] == data[key]['file_name'].replace('.jsonl', '')){
|
$('#select_chat_div').append('<div class="select_chat_block" file_name="'+data[key]['file_name']+'"><div class=avatar><img src="characters/'+characters[this_chid]['avatar']+'""></div><div class="select_chat_block_filename">'+data[key]['file_name']+'</div><div class="select_chat_block_mes">'+mes+'</div></div>');
|
||||||
//children().last()
|
if(characters[this_chid]['chat'] == data[key]['file_name'].replace('.jsonl', '')){
|
||||||
$('#select_chat_div').children(':nth-last-child(1)').attr('highlight', true);
|
//children().last()
|
||||||
}
|
$('#select_chat_div').children(':nth-last-child(1)').attr('highlight', true);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
//<div id="select_chat_div">
|
//<div id="select_chat_div">
|
||||||
|
|
||||||
//<div id="load_select_chat_div">
|
//<div id="load_select_chat_div">
|
||||||
@@ -2614,11 +2628,14 @@
|
|||||||
//chat = data;
|
//chat = data;
|
||||||
//getChatResult();
|
//getChatResult();
|
||||||
//saveChat();
|
//saveChat();
|
||||||
},
|
console.log('Finished getAllCharaChats successfully');
|
||||||
|
},
|
||||||
error: function (jqXHR, exception) {
|
error: function (jqXHR, exception) {
|
||||||
//getChatResult();
|
//getChatResult();
|
||||||
console.log(exception);
|
console.log('Failed to Finished getAllCharaChats');
|
||||||
|
console.log(exception);
|
||||||
console.log(jqXHR);
|
console.log(jqXHR);
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -2816,7 +2833,7 @@
|
|||||||
$("#chat_import_file_type").val(format);
|
$("#chat_import_file_type").val(format);
|
||||||
//console.log(format);
|
//console.log(format);
|
||||||
var formData = new FormData($("#form_import_chat").get(0));
|
var formData = new FormData($("#form_import_chat").get(0));
|
||||||
|
console.log('/importchat entered with: '+formData);
|
||||||
jQuery.ajax({
|
jQuery.ajax({
|
||||||
type: 'POST',
|
type: 'POST',
|
||||||
url: '/importchat',
|
url: '/importchat',
|
||||||
|
@@ -1589,19 +1589,22 @@ label.checkbox :checked + span:after {
|
|||||||
#select_chat_popup{
|
#select_chat_popup{
|
||||||
display: block;
|
display: block;
|
||||||
grid-template-rows: 50px 100px 100px auto 45px;
|
grid-template-rows: 50px 100px 100px auto 45px;
|
||||||
max-width:800px;
|
max-width: 800px;
|
||||||
height: 440px;
|
max-height: 80vh;
|
||||||
|
height: min-content;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
z-index: 2066;
|
z-index: 2066;
|
||||||
margin-left: auto;
|
margin-left: auto;
|
||||||
margin-right: auto;
|
margin-right: auto;
|
||||||
left: 0;
|
left: 0;
|
||||||
right: 0;
|
right: 0;
|
||||||
margin-top: 21vh;
|
margin-top: 6vh;
|
||||||
box-shadow: 0 0 10px rgba(0,0,0,0.5);
|
box-shadow: 0 0 10px rgb(0 0 0 / 50%);
|
||||||
padding: 4px;
|
padding: 10px;
|
||||||
background-color: rgba(0,0,0,0.7);
|
/* padding-top: 50px; */
|
||||||
|
background-color: rgba(0,0,0,0.7);
|
||||||
border-radius: 20px;
|
border-radius: 20px;
|
||||||
|
overflow-y: auto;
|
||||||
}
|
}
|
||||||
#select_chat_popup a{
|
#select_chat_popup a{
|
||||||
color: #936f4a;
|
color: #936f4a;
|
||||||
@@ -1633,7 +1636,8 @@ label.checkbox :checked + span:after {
|
|||||||
overflow-x: hidden;
|
overflow-x: hidden;
|
||||||
overflow-y: scroll;
|
overflow-y: scroll;
|
||||||
|
|
||||||
height: 350px;
|
height: min-content;
|
||||||
|
max-height:100%;
|
||||||
}
|
}
|
||||||
#select_chat_div hr{
|
#select_chat_div hr{
|
||||||
margin:0;
|
margin:0;
|
||||||
|
139
server.js
139
server.js
@@ -58,16 +58,20 @@ var api_key_novel;
|
|||||||
//New chats made with characters will use this new formatting.
|
//New chats made with characters will use this new formatting.
|
||||||
//Useable variable is (( humanizedISO8601Datetime ))
|
//Useable variable is (( humanizedISO8601Datetime ))
|
||||||
|
|
||||||
var baseDate = new Date(Date.now());
|
|
||||||
var humanYear = baseDate.getFullYear();
|
|
||||||
var humanMonth = (baseDate.getMonth()+1);
|
|
||||||
var humanDate = baseDate.getDate();
|
|
||||||
var humanHour = (baseDate.getHours() < 10? '0' : '') + baseDate.getHours();
|
|
||||||
var humanMinute = (baseDate.getMinutes() < 10? '0' : '') + baseDate.getMinutes();
|
|
||||||
var humanSecond = (baseDate.getSeconds() < 10? '0' : '') + baseDate.getSeconds();
|
|
||||||
var humanMillisecond = (baseDate.getMilliseconds() < 10? '0' : '') + baseDate.getMilliseconds();
|
|
||||||
|
|
||||||
var humanizedISO8601DateTime = (humanYear+"-"+humanMonth+"-"+humanDate+" @"+humanHour+"h "+humanMinute+"m "+humanSecond+"s "+humanMillisecond+"ms");
|
|
||||||
|
function humanizedISO8601DateTime() {
|
||||||
|
let baseDate = new Date(Date.now());
|
||||||
|
let humanYear = baseDate.getFullYear();
|
||||||
|
let humanMonth = (baseDate.getMonth()+1);
|
||||||
|
let humanDate = baseDate.getDate();
|
||||||
|
let humanHour = (baseDate.getHours() < 10? '0' : '') + baseDate.getHours();
|
||||||
|
let humanMinute = (baseDate.getMinutes() < 10? '0' : '') + baseDate.getMinutes();
|
||||||
|
let humanSecond = (baseDate.getSeconds() < 10? '0' : '') + baseDate.getSeconds();
|
||||||
|
let humanMillisecond = (baseDate.getMilliseconds() < 10? '0' : '') + baseDate.getMilliseconds();
|
||||||
|
let HumanizedDateTime = (humanYear+"-"+humanMonth+"-"+humanDate+" @"+humanHour+"h "+humanMinute+"m "+humanSecond+"s "+humanMillisecond+"ms");
|
||||||
|
return HumanizedDateTime;
|
||||||
|
};
|
||||||
|
|
||||||
var is_colab = false;
|
var is_colab = false;
|
||||||
var charactersPath = 'public/characters/';
|
var charactersPath = 'public/characters/';
|
||||||
@@ -281,7 +285,7 @@ app.post("/generate", jsonParser, function(request, response_generate = response
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
app.post("/savechat", jsonParser, function(request, response){
|
app.post("/savechat", jsonParser, function(request, response){
|
||||||
//console.log('/savechat/ entered');
|
console.log(humanizedISO8601DateTime()+':/savechat/ entered');
|
||||||
//console.log(request.data);
|
//console.log(request.data);
|
||||||
//console.log(request.body.bg);
|
//console.log(request.body.bg);
|
||||||
//const data = request.body;
|
//const data = request.body;
|
||||||
@@ -289,8 +293,10 @@ app.post("/savechat", jsonParser, function(request, response){
|
|||||||
//console.log(request.body.chat);
|
//console.log(request.body.chat);
|
||||||
//var bg = "body {background-image: linear-gradient(rgba(19,21,44,0.75), rgba(19,21,44,0.75)), url(../backgrounds/"+request.body.bg+");}";
|
//var bg = "body {background-image: linear-gradient(rgba(19,21,44,0.75), rgba(19,21,44,0.75)), url(../backgrounds/"+request.body.bg+");}";
|
||||||
var dir_name = String(request.body.avatar_url).replace('.png','');
|
var dir_name = String(request.body.avatar_url).replace('.png','');
|
||||||
|
console.log(humanizedISO8601DateTime()+':/savechat sees '+dir_name+' as the character name (derived from avatar PNG filename)');
|
||||||
let chat_data = request.body.chat;
|
let chat_data = request.body.chat;
|
||||||
let jsonlData = chat_data.map(JSON.stringify).join('\n');
|
let jsonlData = chat_data.map(JSON.stringify).join('\n');
|
||||||
|
console.log(humanizedISO8601DateTime()+':/savechat saving a chat named '+request.body.file_name+'.jsonl');
|
||||||
fs.writeFile(chatsPath+dir_name+"/"+request.body.file_name+'.jsonl', jsonlData, 'utf8', function(err) {
|
fs.writeFile(chatsPath+dir_name+"/"+request.body.file_name+'.jsonl', jsonlData, 'utf8', function(err) {
|
||||||
if(err) {
|
if(err) {
|
||||||
response.send(err);
|
response.send(err);
|
||||||
@@ -310,7 +316,7 @@ app.post("/getchat", jsonParser, function(request, response){
|
|||||||
//console.log(request);
|
//console.log(request);
|
||||||
//console.log(request.body.chat);
|
//console.log(request.body.chat);
|
||||||
//var bg = "body {background-image: linear-gradient(rgba(19,21,44,0.75), rgba(19,21,44,0.75)), url(../backgrounds/"+request.body.bg+");}";
|
//var bg = "body {background-image: linear-gradient(rgba(19,21,44,0.75), rgba(19,21,44,0.75)), url(../backgrounds/"+request.body.bg+");}";
|
||||||
//console.log('/getchat entered');
|
console.log(humanizedISO8601DateTime()+':/getchat entered');
|
||||||
var dir_name = String(request.body.avatar_url).replace('.png','');
|
var dir_name = String(request.body.avatar_url).replace('.png','');
|
||||||
|
|
||||||
fs.stat(chatsPath+dir_name, function(err, stat) {
|
fs.stat(chatsPath+dir_name, function(err, stat) {
|
||||||
@@ -327,7 +333,7 @@ app.post("/getchat", jsonParser, function(request, response){
|
|||||||
fs.stat(chatsPath+dir_name+"/"+request.body.file_name+".jsonl", function(err, stat) {
|
fs.stat(chatsPath+dir_name+"/"+request.body.file_name+".jsonl", function(err, stat) {
|
||||||
|
|
||||||
if (err === null) { //if no error (the file exists), read the file
|
if (err === null) { //if no error (the file exists), read the file
|
||||||
|
console.log(humanizedISO8601DateTime()+':/getchat tries to access: '+chatsPath+dir_name+'/'+request.body.file_name+'.jsonl');
|
||||||
if(stat !== undefined){
|
if(stat !== undefined){
|
||||||
fs.readFile(chatsPath+dir_name+"/"+request.body.file_name+".jsonl", 'utf8', (err, data) => {
|
fs.readFile(chatsPath+dir_name+"/"+request.body.file_name+".jsonl", 'utf8', (err, data) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -355,11 +361,8 @@ app.post("/getchat", jsonParser, function(request, response){
|
|||||||
console.error(err);
|
console.error(err);
|
||||||
response.send({});
|
response.send({});
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@@ -415,7 +418,7 @@ function checkServer(){
|
|||||||
|
|
||||||
//***************** Main functions
|
//***************** Main functions
|
||||||
function charaFormatData(data){
|
function charaFormatData(data){
|
||||||
var char = {"name": data.ch_name, "description": data.description, "personality": data.personality, "first_mes": data.first_mes, "avatar": 'none', "chat": Date.now(), "mes_example": data.mes_example, "scenario": data.scenario, "create_date": Date.now()};
|
var char = {"name": data.ch_name, "description": data.description, "personality": data.personality, "first_mes": data.first_mes, "avatar": 'none', "chat": data.ch_name+' - '+humanizedISO8601DateTime(), "mes_example": data.mes_example, "scenario": data.scenario, "create_date": humanizedISO8601DateTime};
|
||||||
return char;
|
return char;
|
||||||
}
|
}
|
||||||
app.post("/createcharacter", urlencodedParser, function(request, response){
|
app.post("/createcharacter", urlencodedParser, function(request, response){
|
||||||
@@ -911,12 +914,14 @@ app.post("/getallchatsofcharacter", jsonParser, function(request, response){
|
|||||||
var char_dir = (request.body.avatar_url).replace('.png','')
|
var char_dir = (request.body.avatar_url).replace('.png','')
|
||||||
fs.readdir(chatsPath+char_dir, (err, files) => {
|
fs.readdir(chatsPath+char_dir, (err, files) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
console.log('found error in history loading');
|
||||||
console.error(err);
|
console.error(err);
|
||||||
response.send({error: true});
|
response.send({error: true});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter for JSON files
|
// filter for JSON files
|
||||||
|
console.log('looking for JSONL files');
|
||||||
const jsonFiles = files.filter(file => path.extname(file) === '.jsonl');
|
const jsonFiles = files.filter(file => path.extname(file) === '.jsonl');
|
||||||
|
|
||||||
// sort the files by name
|
// sort the files by name
|
||||||
@@ -924,41 +929,46 @@ app.post("/getallchatsofcharacter", jsonParser, function(request, response){
|
|||||||
// print the sorted file names
|
// print the sorted file names
|
||||||
var chatData = {};
|
var chatData = {};
|
||||||
let ii = jsonFiles.length; //this is the number of files belonging to the character
|
let ii = jsonFiles.length; //this is the number of files belonging to the character
|
||||||
|
if (ii !== 0) {
|
||||||
for(let i = jsonFiles.length-1; i >= 0; i--){
|
console.log('found '+ii+' chat logs to load');
|
||||||
const file = jsonFiles[i];
|
for(let i = jsonFiles.length-1; i >= 0; i--){
|
||||||
|
const file = jsonFiles[i];
|
||||||
|
const fileStream = fs.createReadStream(chatsPath+char_dir+'/'+file);
|
||||||
|
const rl = readline.createInterface({
|
||||||
|
input: fileStream,
|
||||||
|
crlfDelay: Infinity
|
||||||
|
});
|
||||||
|
|
||||||
const fileStream = fs.createReadStream(chatsPath+char_dir+'/'+file);
|
let lastLine;
|
||||||
const rl = readline.createInterface({
|
rl.on('line', (line) => {
|
||||||
input: fileStream,
|
lastLine = line;
|
||||||
crlfDelay: Infinity
|
});
|
||||||
});
|
rl.on('close', () => {
|
||||||
|
if(lastLine){
|
||||||
let lastLine;
|
let jsonData = JSON.parse(lastLine);
|
||||||
rl.on('line', (line) => {
|
if(jsonData.name !== undefined){
|
||||||
lastLine = line;
|
chatData[i] = {};
|
||||||
});
|
chatData[i]['file_name'] = file;
|
||||||
rl.on('close', () => {
|
chatData[i]['mes'] = jsonData['mes'];
|
||||||
if(lastLine){
|
ii--;
|
||||||
let jsonData = JSON.parse(lastLine);
|
if(ii === 0){
|
||||||
if(jsonData.name !== undefined){
|
console.log('ii count went to zero, responding with chatData');
|
||||||
chatData[i] = {};
|
response.send(chatData);
|
||||||
chatData[i]['file_name'] = file;
|
}
|
||||||
chatData[i]['mes'] = jsonData['mes'];
|
}else{
|
||||||
ii--;
|
console.log('just returning from getallchatsofcharacter');
|
||||||
if(ii === 0){
|
return;
|
||||||
response.send(chatData);
|
}
|
||||||
}
|
}
|
||||||
}else{
|
console.log('successfully closing getallchatsofcharacter');
|
||||||
return;
|
rl.close();
|
||||||
}
|
});
|
||||||
}
|
};
|
||||||
rl.close();
|
}else{
|
||||||
});
|
console.log('Found No Chats. Exiting Load Routine.');
|
||||||
}
|
response.send({error: true});
|
||||||
});
|
};
|
||||||
|
})});
|
||||||
});
|
|
||||||
function getPngName(file){
|
function getPngName(file){
|
||||||
let i = 1;
|
let i = 1;
|
||||||
let base_name = file;
|
let base_name = file;
|
||||||
@@ -969,6 +979,7 @@ function getPngName(file){
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
app.post("/importcharacter", urlencodedParser, function(request, response){
|
app.post("/importcharacter", urlencodedParser, function(request, response){
|
||||||
|
|
||||||
if(!request.body) return response.sendStatus(400);
|
if(!request.body) return response.sendStatus(400);
|
||||||
|
|
||||||
let png_name = '';
|
let png_name = '';
|
||||||
@@ -987,12 +998,12 @@ app.post("/importcharacter", urlencodedParser, function(request, response){
|
|||||||
|
|
||||||
if(jsonData.name !== undefined){
|
if(jsonData.name !== undefined){
|
||||||
png_name = getPngName(jsonData.name);
|
png_name = getPngName(jsonData.name);
|
||||||
let char = {"name": jsonData.name, "description": jsonData.description ?? '', "personality": jsonData.personality ?? '', "first_mes": jsonData.first_mes ?? '', "avatar": 'none', "chat": Date.now(), "mes_example": jsonData.mes_example ?? '', "scenario": jsonData.scenario ?? '', "create_date": Date.now()};
|
let char = {"name": jsonData.name, "description": jsonData.description ?? '', "personality": jsonData.personality ?? '', "first_mes": jsonData.first_mes ?? '', "avatar": 'none', "chat": humanizedISO8601DateTime(), "mes_example": jsonData.mes_example ?? '', "scenario": jsonData.scenario ?? '', "create_date": humanizedISO8601DateTime};
|
||||||
char = JSON.stringify(char);
|
char = JSON.stringify(char);
|
||||||
charaWrite('./public/img/fluffy.png', char, png_name, response, {file_name: png_name});
|
charaWrite('./public/img/fluffy.png', char, png_name, response, {file_name: png_name});
|
||||||
}else if(jsonData.char_name !== undefined){//json Pygmalion notepad
|
}else if(jsonData.char_name !== undefined){//json Pygmalion notepad
|
||||||
png_name = getPngName(jsonData.char_name);
|
png_name = getPngName(jsonData.char_name);
|
||||||
let char = {"name": jsonData.char_name, "description": jsonData.char_persona ?? '', "personality": '', "first_mes": jsonData.char_greeting ?? '', "avatar": 'none', "chat": Date.now(), "mes_example": jsonData.example_dialogue ?? '', "scenario": jsonData.world_scenario ?? '', "create_date": Date.now()};
|
let char = {"name": jsonData.char_name, "description": jsonData.char_persona ?? '', "personality": '', "first_mes": jsonData.char_greeting ?? '', "avatar": 'none', "chat": humanizedISO8601DateTime(), "mes_example": jsonData.example_dialogue ?? '', "scenario": jsonData.world_scenario ?? '', "create_date": humanizedISO8601DateTime};
|
||||||
char = JSON.stringify(char);
|
char = JSON.stringify(char);
|
||||||
charaWrite('./public/img/fluffy.png', char, png_name, response, {file_name: png_name});
|
charaWrite('./public/img/fluffy.png', char, png_name, response, {file_name: png_name});
|
||||||
}else{
|
}else{
|
||||||
@@ -1008,7 +1019,7 @@ app.post("/importcharacter", urlencodedParser, function(request, response){
|
|||||||
png_name = getPngName(jsonData.name);
|
png_name = getPngName(jsonData.name);
|
||||||
|
|
||||||
if(jsonData.name !== undefined){
|
if(jsonData.name !== undefined){
|
||||||
let char = {"name": jsonData.name, "description": jsonData.description ?? '', "personality": jsonData.personality ?? '', "first_mes": jsonData.first_mes ?? '', "avatar": 'none', "chat": Date.now(), "mes_example": jsonData.mes_example ?? '', "scenario": jsonData.scenario ?? '', "create_date": Date.now()};
|
let char = {"name": jsonData.name, "description": jsonData.description ?? '', "personality": jsonData.personality ?? '', "first_mes": jsonData.first_mes ?? '', "avatar": 'none', "chat": humanizedISO8601DateTime(), "mes_example": jsonData.mes_example ?? '', "scenario": jsonData.scenario ?? '', "create_date": humanizedISO8601DateTime};
|
||||||
char = JSON.stringify(char);
|
char = JSON.stringify(char);
|
||||||
charaWrite('./uploads/'+filedata.filename, char, png_name, response, {file_name: png_name});
|
charaWrite('./uploads/'+filedata.filename, char, png_name, response, {file_name: png_name});
|
||||||
/*
|
/*
|
||||||
@@ -1040,6 +1051,7 @@ app.post("/importcharacter", urlencodedParser, function(request, response){
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.post("/importchat", urlencodedParser, function(request, response){
|
app.post("/importchat", urlencodedParser, function(request, response){
|
||||||
|
console.log(humanizedISO8601DateTime()+':/importchat begun');
|
||||||
if(!request.body) return response.sendStatus(400);
|
if(!request.body) return response.sendStatus(400);
|
||||||
|
|
||||||
var format = request.body.file_type;
|
var format = request.body.file_type;
|
||||||
@@ -1063,11 +1075,12 @@ app.post("/importchat", urlencodedParser, function(request, response){
|
|||||||
const jsonData = JSON.parse(data);
|
const jsonData = JSON.parse(data);
|
||||||
var new_chat = [];
|
var new_chat = [];
|
||||||
if(jsonData.histories !== undefined){
|
if(jsonData.histories !== undefined){
|
||||||
|
console.log('/importchat confirms JSON histories are defined');
|
||||||
let i = 0;
|
let i = 0;
|
||||||
new_chat[i] = {};
|
new_chat[i] = {};
|
||||||
new_chat[0]['user_name'] = 'You';
|
new_chat[0]['user_name'] = 'You';
|
||||||
new_chat[0]['character_name'] = ch_name;
|
new_chat[0]['character_name'] = ch_name;
|
||||||
new_chat[0]['create_date'] = Date.now() //Date.now();
|
new_chat[0]['create_date'] = humanizedISO8601DateTime() //Date.now();
|
||||||
i++;
|
i++;
|
||||||
jsonData.histories.histories[0].msgs.forEach(function(item) {
|
jsonData.histories.histories[0].msgs.forEach(function(item) {
|
||||||
new_chat[i] = {};
|
new_chat[i] = {};
|
||||||
@@ -1078,12 +1091,13 @@ app.post("/importchat", urlencodedParser, function(request, response){
|
|||||||
}
|
}
|
||||||
new_chat[i]['is_user'] = item.src.is_human;
|
new_chat[i]['is_user'] = item.src.is_human;
|
||||||
new_chat[i]['is_name'] = true;
|
new_chat[i]['is_name'] = true;
|
||||||
new_chat[i]['send_date'] = Date.now() //Date.now();
|
new_chat[i]['send_date'] = humanizedISO8601DateTime() //Date.now();
|
||||||
new_chat[i]['mes'] = item.text;
|
new_chat[i]['mes'] = item.text;
|
||||||
i++;
|
i++;
|
||||||
});
|
});
|
||||||
const chatJsonlData = new_chat.map(JSON.stringify).join('\n');
|
const chatJsonlData = new_chat.map(JSON.stringify).join('\n');
|
||||||
fs.writeFile(chatsPath+avatar_url+'/'+ch_name+' - '+humanizedISO8601DateTime+' imported.jsonl', chatJsonlData, 'utf8', function(err) { //added ch_name and replaced Date.now() with humanizedISO8601DateTime
|
console.log('/importchat saving a file: '+ch_name+' - '+humanizedISO8601DateTime()+' imported.jsonl');
|
||||||
|
fs.writeFile(chatsPath+avatar_url+'/'+ch_name+' - '+humanizedISO8601DateTime()+' imported.jsonl', chatJsonlData, 'utf8', function(err) { //added ch_name and replaced Date.now() with humanizedISO8601DateTime
|
||||||
|
|
||||||
if(err) {
|
if(err) {
|
||||||
response.send(err);
|
response.send(err);
|
||||||
@@ -1103,6 +1117,7 @@ app.post("/importchat", urlencodedParser, function(request, response){
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
if(format === 'jsonl'){
|
if(format === 'jsonl'){
|
||||||
|
console.log(humanizedISO8601DateTime()+':imported chat format is JSONL');
|
||||||
const fileStream = fs.createReadStream('./uploads/'+filedata.filename);
|
const fileStream = fs.createReadStream('./uploads/'+filedata.filename);
|
||||||
const rl = readline.createInterface({
|
const rl = readline.createInterface({
|
||||||
input: fileStream,
|
input: fileStream,
|
||||||
@@ -1113,7 +1128,8 @@ app.post("/importchat", urlencodedParser, function(request, response){
|
|||||||
let jsonData = JSON.parse(line);
|
let jsonData = JSON.parse(line);
|
||||||
|
|
||||||
if(jsonData.user_name !== undefined){
|
if(jsonData.user_name !== undefined){
|
||||||
fs.copyFile('./uploads/'+filedata.filename, chatsPath+avatar_url+'/'+ch_name+' - '+humanizedISO8601DateTime+'.jsonl', (err) => { //added character name and replaced Date.now() with humanizedISO8601DateTime
|
console.log(humanizedISO8601DateTime()+':/importchat copying chat as '+ch_name+' - '+humanizedISO8601DateTime()+'.jsonl');
|
||||||
|
fs.copyFile('./uploads/'+filedata.filename, chatsPath+avatar_url+'/'+ch_name+' - '+humanizedISO8601DateTime()+'.jsonl', (err) => { //added character name and replaced Date.now() with humanizedISO8601DateTime
|
||||||
if(err) {
|
if(err) {
|
||||||
response.send({error:true});
|
response.send({error:true});
|
||||||
return console.log(err);
|
return console.log(err);
|
||||||
@@ -1192,7 +1208,7 @@ function convertStage2(){
|
|||||||
//console.log(directoriesB[key]);
|
//console.log(directoriesB[key]);
|
||||||
|
|
||||||
var char = JSON.parse(charactersB[key]);
|
var char = JSON.parse(charactersB[key]);
|
||||||
char.create_date = Date.now();
|
char.create_date = humanizedISO8601DateTime();
|
||||||
charactersB[key] = JSON.stringify(char);
|
charactersB[key] = JSON.stringify(char);
|
||||||
var avatar = 'public/img/fluffy.png';
|
var avatar = 'public/img/fluffy.png';
|
||||||
if(char.avatar !== 'none'){
|
if(char.avatar !== 'none'){
|
||||||
@@ -1222,7 +1238,7 @@ function convertStage2(){
|
|||||||
}
|
}
|
||||||
let i = 0;
|
let i = 0;
|
||||||
let ii = 0;
|
let ii = 0;
|
||||||
new_chat_data[i] = {user_name:'You', character_name:char.name, create_date: Date.now()};
|
new_chat_data[i] = {user_name:'You', character_name:char.name, create_date: humanizedISO8601DateTime()};
|
||||||
i++;
|
i++;
|
||||||
ii++;
|
ii++;
|
||||||
chat_data.forEach(function(mes) {
|
chat_data.forEach(function(mes) {
|
||||||
@@ -1238,14 +1254,14 @@ function convertStage2(){
|
|||||||
new_chat_data[ii]['name'] = char.name;
|
new_chat_data[ii]['name'] = char.name;
|
||||||
new_chat_data[ii]['is_user'] = false;
|
new_chat_data[ii]['is_user'] = false;
|
||||||
new_chat_data[ii]['is_name'] = is_name;
|
new_chat_data[ii]['is_name'] = is_name;
|
||||||
new_chat_data[ii]['send_date'] = Date.now(); //Date.now();
|
new_chat_data[ii]['send_date'] = humanizedISO8601DateTime(); //Date.now();
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
mes = mes.replace(this_chat_user_name+':','');
|
mes = mes.replace(this_chat_user_name+':','');
|
||||||
new_chat_data[ii]['name'] = 'You';
|
new_chat_data[ii]['name'] = 'You';
|
||||||
new_chat_data[ii]['is_user'] = true;
|
new_chat_data[ii]['is_user'] = true;
|
||||||
new_chat_data[ii]['is_name'] = true;
|
new_chat_data[ii]['is_name'] = true;
|
||||||
new_chat_data[ii]['send_date'] = Date.now() //Date.now();
|
new_chat_data[ii]['send_date'] = humanizedISO8601DateTime(); //Date.now();
|
||||||
|
|
||||||
}
|
}
|
||||||
new_chat_data[ii]['mes'] = mes.trim();
|
new_chat_data[ii]['mes'] = mes.trim();
|
||||||
@@ -1257,6 +1273,7 @@ function convertStage2(){
|
|||||||
});
|
});
|
||||||
const jsonlData = new_chat_data.map(JSON.stringify).join('\n');
|
const jsonlData = new_chat_data.map(JSON.stringify).join('\n');
|
||||||
// Write the contents to the destination folder
|
// Write the contents to the destination folder
|
||||||
|
console.log('convertstage2 writing a file: '+chatsPath+char.name+'/' + file+'l');
|
||||||
fs.writeFileSync(chatsPath+char.name+'/' + file+'l', jsonlData);
|
fs.writeFileSync(chatsPath+char.name+'/' + file+'l', jsonlData);
|
||||||
});
|
});
|
||||||
//fs.rmSync('public/characters/'+directoriesB[key],{ recursive: true });
|
//fs.rmSync('public/characters/'+directoriesB[key],{ recursive: true });
|
||||||
|
Reference in New Issue
Block a user