Add direct download for session/chat history JSONs
This commit is contained in:
parent
32a2bbee4a
commit
0e8f9354b5
5 changed files with 66 additions and 63 deletions
40
css/save_files.js
Normal file
40
css/save_files.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
// Functions for downloading JSON files
|
||||
function getCurrentTimestamp() {
|
||||
const now = new Date();
|
||||
const timezoneOffset = now.getTimezoneOffset() * 60000; // Convert to milliseconds
|
||||
const localTime = new Date(now.getTime() - timezoneOffset);
|
||||
const formattedTimestamp = localTime.toISOString().replace(/[-:]/g, '').slice(0, 15);
|
||||
return formattedTimestamp;
|
||||
}
|
||||
|
||||
function saveFile(contents, filename) {
|
||||
const element = document.createElement('a');
|
||||
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(contents));
|
||||
element.setAttribute('download', filename);
|
||||
element.style.display = 'none';
|
||||
document.body.appendChild(element);
|
||||
element.click();
|
||||
document.body.removeChild(element);
|
||||
}
|
||||
|
||||
function saveHistory(history, character, mode) {
|
||||
let path = null;
|
||||
|
||||
if (['chat', 'chat-instruct'].includes(mode) && character && character.trim() !== '') {
|
||||
path = `history_${character}_${getCurrentTimestamp()}.json`;
|
||||
} else {
|
||||
try {
|
||||
path = `history_${mode}_${getCurrentTimestamp()}.json`;
|
||||
} catch (error) {
|
||||
path = `history_${getCurrentTimestamp()}.json`;
|
||||
}
|
||||
}
|
||||
saveFile(history, path);
|
||||
}
|
||||
|
||||
function saveSession(session, mode) {
|
||||
let path = null;
|
||||
|
||||
path = `session_${mode}_${getCurrentTimestamp()}.json`;
|
||||
saveFile(session, path);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue