Import all chats from a characterAI chat history dump

Instead of only first chat found.
This commit is contained in:
maver
2023-04-18 20:10:28 +02:00
parent 57ea659540
commit 89aa307f16

View File

@@ -1456,47 +1456,51 @@ app.post("/importchat", urlencodedParser, function (request, response) {
} }
const jsonData = json5.parse(data); const jsonData = json5.parse(data);
var new_chat = [];
if (jsonData.histories !== undefined) { if (jsonData.histories !== undefined) {
//console.log('/importchat confirms JSON histories are defined'); //console.log('/importchat confirms JSON histories are defined');
let i = 0; const chat = {
new_chat[i] = {}; from(history) {
new_chat[0]['user_name'] = 'You'; return [
new_chat[0]['character_name'] = ch_name; {
new_chat[0]['create_date'] = humanizedISO8601DateTime() //Date.now(); user_name: 'You',
i++; character_name: ch_name,
jsonData.histories.histories[0].msgs.forEach(function (item) { create_date: humanizedISO8601DateTime(),
new_chat[i] = {};
if (item.src.is_human == true) { },
new_chat[i]['name'] = 'You'; ...history.msgs.map(
} else { (message) => ({
new_chat[i]['name'] = ch_name; name: message.src.is_human ? 'You' : ch_name,
} is_user: message.src.is_human,
new_chat[i]['is_user'] = item.src.is_human; is_name: true,
new_chat[i]['is_name'] = true; send_date: humanizedISO8601DateTime(),
new_chat[i]['send_date'] = humanizedISO8601DateTime() //Date.now(); mes: message.text,
new_chat[i]['mes'] = item.text; })
i++; )];
}); }
const chatJsonlData = new_chat.map(JSON.stringify).join('\n'); }
//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 const newChats = [];
(jsonData.histories.histories ?? []).forEach((history) => {
newChats.push(chat.from(history));
});
const errors = [];
newChats.forEach(chat => fs.writeFile(
chatsPath + avatar_url + '/' + ch_name + ' - ' + humanizedISO8601DateTime() + ' imported.jsonl',
chat.map(JSON.stringify).join('\n'),
'utf8',
(err) => err ?? errors.push(err)
)
);
if (0 < errors.length) {
response.send('Errors occurred while writing character files. Errors: ' + JSON.stringify(errors));
}
if (err) {
response.send(err);
return console.log(err);
//response.send(err);
} else {
//response.redirect("/");
response.send({res: true}); response.send({res: true});
}
});
} else { } else {
response.send({ error: true }); response.send({ error: true });
return;
} }
}); });
} }
if (format === 'jsonl') { if (format === 'jsonl') {