mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Chat Filename Humanization + Larger Past Chats Box
1. Replaced all Date.now() instances with a humanized ISO8601 timestamp that is easily readable by users. This has a seamless effect on the embedded timestamping of chat mesages. Old chat files can still be edited (messages deleted), and continued. Character PNG files get updated with the new date format, and this will be reflected in github but there is no functional change to the user. 2. Added character name to the front of all newly saved/imported chat files. Old chats will retain their original filename unless re-imported. With these two changes, the new chat filenames are as such: "Aqua - 2023-3-3 @03h 08m 36s 948ms.jsonl" (I would like to make it smaller, perhaps by removing the milliseconds. Let me know if this is possible, or if it would introduce potential overwrites if users make/update files within the same second.) 3. Increased the size of the 'view past chats' popup box to max 800px so accommodate the new larger filenames.
This commit is contained in:
64
server.js
64
server.js
@@ -51,6 +51,24 @@ var response_getstatus_novel;
|
||||
var response_getlastversion;
|
||||
var api_key_novel;
|
||||
|
||||
//RossAscends: Added function to format dates used in files and chat timestamps to a humanized format.
|
||||
//Mostly I wanted this to be for file names, but couldn't figure out exactly where the filename save code was as everything seemed to be connected.
|
||||
//During testing, this performs the same as previous date.now() structure.
|
||||
//It also does not break old characters/chats, as the code just uses whatever timestamp exists in the chat.
|
||||
//New chats made with characters will use this new formatting.
|
||||
//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");
|
||||
|
||||
var is_colab = false;
|
||||
var charactersPath = 'public/characters/';
|
||||
var chatsPath = 'public/chats/';
|
||||
@@ -263,6 +281,7 @@ app.post("/generate", jsonParser, function(request, response_generate = response
|
||||
});
|
||||
});
|
||||
app.post("/savechat", jsonParser, function(request, response){
|
||||
//console.log('/savechat/ entered');
|
||||
//console.log(request.data);
|
||||
//console.log(request.body.bg);
|
||||
//const data = request.body;
|
||||
@@ -291,22 +310,23 @@ app.post("/getchat", jsonParser, function(request, response){
|
||||
//console.log(request);
|
||||
//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+");}";
|
||||
//console.log('/getchat entered');
|
||||
var dir_name = String(request.body.avatar_url).replace('.png','');
|
||||
|
||||
fs.stat(chatsPath+dir_name, function(err, stat) {
|
||||
|
||||
if(stat === undefined){
|
||||
if(stat === undefined){ //if no chat dir for the character is found, make one with the character name
|
||||
|
||||
fs.mkdirSync(chatsPath+dir_name);
|
||||
response.send({});
|
||||
return;
|
||||
}else{
|
||||
|
||||
if(err === null){
|
||||
if(err === null){ //if there is a dir, then read the requested file from the JSON call
|
||||
|
||||
fs.stat(chatsPath+dir_name+"/"+request.body.file_name+".jsonl", function(err, stat) {
|
||||
|
||||
if (err === null) {
|
||||
if (err === null) { //if no error (the file exists), read the file
|
||||
|
||||
if(stat !== undefined){
|
||||
fs.readFile(chatsPath+dir_name+"/"+request.body.file_name+".jsonl", 'utf8', (err, data) => {
|
||||
@@ -321,7 +341,7 @@ app.post("/getchat", jsonParser, function(request, response){
|
||||
// Iterate through the array of strings and parse each line as JSON
|
||||
const jsonData = lines.map(JSON.parse);
|
||||
response.send(jsonData);
|
||||
|
||||
//console.log('read the requested file')
|
||||
|
||||
});
|
||||
}
|
||||
@@ -335,6 +355,7 @@ app.post("/getchat", jsonParser, function(request, response){
|
||||
console.error(err);
|
||||
response.send({});
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -394,7 +415,7 @@ function checkServer(){
|
||||
|
||||
//***************** Main functions
|
||||
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": humanizedISO8601DateTime, "mes_example": data.mes_example, "scenario": data.scenario, "create_date": humanizedISO8601DateTime};
|
||||
return char;
|
||||
}
|
||||
app.post("/createcharacter", urlencodedParser, function(request, response){
|
||||
@@ -884,7 +905,7 @@ app.post("/generate_novelai", jsonParser, function(request, response_generate_no
|
||||
});
|
||||
});
|
||||
|
||||
app.post("/getallchatsofchatacter", jsonParser, function(request, response){
|
||||
app.post("/getallchatsofcharacter", jsonParser, function(request, response){
|
||||
if(!request.body) return response.sendStatus(400);
|
||||
|
||||
var char_dir = (request.body.avatar_url).replace('.png','')
|
||||
@@ -900,10 +921,10 @@ app.post("/getallchatsofchatacter", jsonParser, function(request, response){
|
||||
|
||||
// sort the files by name
|
||||
//jsonFiles.sort().reverse();
|
||||
|
||||
// print the sorted file names
|
||||
var chatData = {};
|
||||
let ii = jsonFiles.length;
|
||||
let ii = jsonFiles.length; //this is the number of files belonging to the character
|
||||
|
||||
for(let i = jsonFiles.length-1; i >= 0; i--){
|
||||
const file = jsonFiles[i];
|
||||
|
||||
@@ -914,11 +935,9 @@ app.post("/getallchatsofchatacter", jsonParser, function(request, response){
|
||||
});
|
||||
|
||||
let lastLine;
|
||||
|
||||
rl.on('line', (line) => {
|
||||
lastLine = line;
|
||||
lastLine = line;
|
||||
});
|
||||
|
||||
rl.on('close', () => {
|
||||
if(lastLine){
|
||||
let jsonData = JSON.parse(lastLine);
|
||||
@@ -968,12 +987,12 @@ app.post("/importcharacter", urlencodedParser, function(request, response){
|
||||
|
||||
if(jsonData.name !== undefined){
|
||||
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);
|
||||
charaWrite('./public/img/fluffy.png', char, png_name, response, {file_name: png_name});
|
||||
}else if(jsonData.char_name !== undefined){//json Pygmalion notepad
|
||||
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);
|
||||
charaWrite('./public/img/fluffy.png', char, png_name, response, {file_name: png_name});
|
||||
}else{
|
||||
@@ -989,7 +1008,7 @@ app.post("/importcharacter", urlencodedParser, function(request, response){
|
||||
png_name = getPngName(jsonData.name);
|
||||
|
||||
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);
|
||||
charaWrite('./uploads/'+filedata.filename, char, png_name, response, {file_name: png_name});
|
||||
/*
|
||||
@@ -1048,7 +1067,7 @@ app.post("/importchat", urlencodedParser, function(request, response){
|
||||
new_chat[i] = {};
|
||||
new_chat[0]['user_name'] = 'You';
|
||||
new_chat[0]['character_name'] = ch_name;
|
||||
new_chat[0]['create_date'] = Date.now();
|
||||
new_chat[0]['create_date'] = humanizedISO8601DateTime //Date.now();
|
||||
i++;
|
||||
jsonData.histories.histories[0].msgs.forEach(function(item) {
|
||||
new_chat[i] = {};
|
||||
@@ -1059,12 +1078,13 @@ app.post("/importchat", urlencodedParser, function(request, response){
|
||||
}
|
||||
new_chat[i]['is_user'] = item.src.is_human;
|
||||
new_chat[i]['is_name'] = true;
|
||||
new_chat[i]['send_date'] = Date.now();
|
||||
new_chat[i]['send_date'] = humanizedISO8601DateTime //Date.now();
|
||||
new_chat[i]['mes'] = item.text;
|
||||
i++;
|
||||
});
|
||||
const chatJsonlData = new_chat.map(JSON.stringify).join('\n');
|
||||
fs.writeFile(chatsPath+avatar_url+'/'+Date.now()+'.jsonl', chatJsonlData, 'utf8', function(err) {
|
||||
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) {
|
||||
response.send(err);
|
||||
return console.log(err);
|
||||
@@ -1093,7 +1113,7 @@ app.post("/importchat", urlencodedParser, function(request, response){
|
||||
let jsonData = JSON.parse(line);
|
||||
|
||||
if(jsonData.user_name !== undefined){
|
||||
fs.copyFile('./uploads/'+filedata.filename, chatsPath+avatar_url+'/'+Date.now()+'.jsonl', (err) => {
|
||||
fs.copyFile('./uploads/'+filedata.filename, chatsPath+avatar_url+'/'+ch_name+' - '+humanizedISO8601DateTime+'.jsonl', (err) => { //Date.now() replaced for humanizedISO8601DateTime
|
||||
if(err) {
|
||||
response.send({error:true});
|
||||
return console.log(err);
|
||||
@@ -1172,7 +1192,7 @@ function convertStage2(){
|
||||
//console.log(directoriesB[key]);
|
||||
|
||||
var char = JSON.parse(charactersB[key]);
|
||||
char.create_date = Date.now();
|
||||
char.create_date = humanizedISO8601DateTime;
|
||||
charactersB[key] = JSON.stringify(char);
|
||||
var avatar = 'public/img/fluffy.png';
|
||||
if(char.avatar !== 'none'){
|
||||
@@ -1202,7 +1222,7 @@ function convertStage2(){
|
||||
}
|
||||
let i = 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++;
|
||||
ii++;
|
||||
chat_data.forEach(function(mes) {
|
||||
@@ -1218,14 +1238,14 @@ function convertStage2(){
|
||||
new_chat_data[ii]['name'] = char.name;
|
||||
new_chat_data[ii]['is_user'] = false;
|
||||
new_chat_data[ii]['is_name'] = is_name;
|
||||
new_chat_data[ii]['send_date'] = Date.now();
|
||||
new_chat_data[ii]['send_date'] = humanizedISO8601DateTime; //Date.now();
|
||||
|
||||
}else{
|
||||
mes = mes.replace(this_chat_user_name+':','');
|
||||
new_chat_data[ii]['name'] = 'You';
|
||||
new_chat_data[ii]['is_user'] = true;
|
||||
new_chat_data[ii]['is_name'] = true;
|
||||
new_chat_data[ii]['send_date'] = Date.now();
|
||||
new_chat_data[ii]['send_date'] = humanizedISO8601DateTime //Date.now();
|
||||
|
||||
}
|
||||
new_chat_data[ii]['mes'] = mes.trim();
|
||||
|
Reference in New Issue
Block a user