diff --git a/public/index.html b/public/index.html index 68fc6a722..ce00ec169 100644 --- a/public/index.html +++ b/public/index.html @@ -2870,9 +2870,10 @@
-
-
-
+
+
+
+
@@ -3338,4 +3339,4 @@ - \ No newline at end of file + diff --git a/public/script.js b/public/script.js index 577916b05..bf20b6876 100644 --- a/public/script.js +++ b/public/script.js @@ -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(); 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 body = { is_group: !!selected_group, avatar_url: characters[this_chid]?.avatar, file: `${filename}.jsonl`, - exportfilename: `${filename}.txt`, + exportfilename: `${filename}.${format}`, + format: format, } console.log(body); try { @@ -6576,11 +6580,12 @@ $(document).ready(function () { toastr.error(`Error: ${data.message}`); return; } else { + const mimeType = format == 'txt' ? 'text/plain' : 'application/json'; // success, handle response data console.log(data); await delay(250); toastr.success(data.message); - download(data.result, body.exportfilename, 'text/plain'); + download(data.result, body.exportfilename, mimeType); } } catch (error) { // display error message diff --git a/public/style.css b/public/style.css index 46101b6de..bb630c438 100644 --- a/public/style.css +++ b/public/style.css @@ -2689,6 +2689,7 @@ h5 { } .renameChatButton, +.exportRawChatButton, .exportChatButton { cursor: pointer; } @@ -4690,4 +4691,4 @@ body.waifuMode #avatar_zoom_popup { #horde_model { height: unset; } -} \ No newline at end of file +} diff --git a/server.js b/server.js index f44a157d8..fad4c7eec 100644 --- a/server.js +++ b/server.js @@ -1847,8 +1847,29 @@ app.post("/exportchat", jsonParser, async function (request, response) { return response.status(404).json(errorMessage); } 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 fs = require('fs'); const readStream = fs.createReadStream(filename); const rl = readline.createInterface({ input: readStream,