#517 Export chats as JSONL

This commit is contained in:
Cohee
2023-06-19 22:29:09 +03:00
parent fb97d95dae
commit 9c28126ccd
4 changed files with 37 additions and 9 deletions

View File

@ -2870,9 +2870,10 @@
<div class="select_chat_block_mes"></div> <div class="select_chat_block_mes"></div>
</div> </div>
<div class="flex-container height100pSpaceEvenly"> <div class="flex-container height100pSpaceEvenly">
<div class="renameChatButton fa-solid fa-pen"></div> <div title="Rename chat file" class="renameChatButton fa-solid fa-pen"></div>
<div class="exportChatButton fa-solid fa-file-export"></div> <div title="Export JSONL chat file" data-format="jsonl" class="exportRawChatButton fa-solid fa-file-export"></div>
<div file_name="" class="PastChat_cross fa-solid fa-circle-xmark"></div> <div title="Download chat as plain text document" data-format="txt" class="exportChatButton fa-solid fa-file-lines"></div>
<div title="Delete chat file" file_name="" class="PastChat_cross fa-solid fa-circle-xmark"></div>
</div> </div>
</div> </div>
</div> </div>
@ -3338,4 +3339,4 @@
</script> </script>
</body> </body>
</html> </html>

View File

@ -6551,15 +6551,19 @@ $(document).ready(function () {
} }
}); });
$(document).on("click", ".exportChatButton", async function () { $(document).on("click", ".exportChatButton, .exportRawChatButton", async function () {
const format = $(this).data('format') || 'txt';
await saveChatConditional(); await saveChatConditional();
const filenamefull = $(this).closest('.select_chat_block_wrapper').find('.select_chat_block_filename').text(); const filenamefull = $(this).closest('.select_chat_block_wrapper').find('.select_chat_block_filename').text();
console.log(`exporting ${filenamefull} in ${format} format`);
const filename = filenamefull.replace('.jsonl', ''); const filename = filenamefull.replace('.jsonl', '');
const body = { const body = {
is_group: !!selected_group, is_group: !!selected_group,
avatar_url: characters[this_chid]?.avatar, avatar_url: characters[this_chid]?.avatar,
file: `${filename}.jsonl`, file: `${filename}.jsonl`,
exportfilename: `${filename}.txt`, exportfilename: `${filename}.${format}`,
format: format,
} }
console.log(body); console.log(body);
try { try {
@ -6576,11 +6580,12 @@ $(document).ready(function () {
toastr.error(`Error: ${data.message}`); toastr.error(`Error: ${data.message}`);
return; return;
} else { } else {
const mimeType = format == 'txt' ? 'text/plain' : 'application/json';
// success, handle response data // success, handle response data
console.log(data); console.log(data);
await delay(250); await delay(250);
toastr.success(data.message); toastr.success(data.message);
download(data.result, body.exportfilename, 'text/plain'); download(data.result, body.exportfilename, mimeType);
} }
} catch (error) { } catch (error) {
// display error message // display error message

View File

@ -2689,6 +2689,7 @@ h5 {
} }
.renameChatButton, .renameChatButton,
.exportRawChatButton,
.exportChatButton { .exportChatButton {
cursor: pointer; cursor: pointer;
} }
@ -4690,4 +4691,4 @@ body.waifuMode #avatar_zoom_popup {
#horde_model { #horde_model {
height: unset; height: unset;
} }
} }

View File

@ -1847,8 +1847,29 @@ app.post("/exportchat", jsonParser, async function (request, response) {
return response.status(404).json(errorMessage); return response.status(404).json(errorMessage);
} }
try { try {
// Short path for JSONL files
if (request.body.format == 'jsonl') {
try {
const rawFile = fs.readFileSync(filename, 'utf8');
const successMessage = {
message: `Chat saved to ${exportfilename}`,
result: rawFile,
}
console.log(`Chat exported as ${exportfilename}`);
return response.status(200).json(successMessage);
}
catch (err) {
console.error(err);
const errorMessage = {
message: `Could not read JSONL file to export. Source chat file: ${filename}.`
}
console.log(errorMessage.message);
return response.status(500).json(errorMessage);
}
}
const readline = require('readline'); const readline = require('readline');
const fs = require('fs');
const readStream = fs.createReadStream(filename); const readStream = fs.createReadStream(filename);
const rl = readline.createInterface({ const rl = readline.createInterface({
input: readStream, input: readStream,