text-generation-webui/js/save_files.js

35 lines
1.1 KiB
JavaScript
Raw Normal View History

// Functions for downloading JSON files
function getCurrentTimestamp() {
2023-10-07 19:07:57 -07:00
const now = new Date();
2026-03-30 17:44:19 -07:00
const timezoneOffset = now.getTimezoneOffset() * 60000; // Convert minutes to milliseconds
2023-10-07 19:07:57 -07:00
const localTime = new Date(now.getTime() - timezoneOffset);
2026-03-30 17:44:19 -07:00
return localTime.toISOString().replace(/[-:]/g, "").slice(0, 15);
}
function saveFile(contents, filename) {
2023-10-07 19:07:57 -07:00
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) {
2026-03-30 17:44:19 -07:00
let path;
2023-10-07 19:07:57 -07:00
if (["chat", "chat-instruct"].includes(mode) && character && character.trim() !== "") {
path = `history_${character}_${getCurrentTimestamp()}.json`;
} else {
2026-03-30 17:44:19 -07:00
path = `history_${mode || "unknown"}_${getCurrentTimestamp()}.json`;
2023-10-07 19:07:57 -07:00
}
2026-03-30 17:44:19 -07:00
2023-10-07 19:07:57 -07:00
saveFile(history, path);
}
2023-08-13 01:12:15 -03:00
function saveSession(session) {
2026-03-30 17:44:19 -07:00
const path = `session_${getCurrentTimestamp()}.json`;
2023-10-07 19:07:57 -07:00
saveFile(session, path);
}