-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add function to save file Signed-off-by: cbh778899 <[email protected]> * add handler to format chat history for export Signed-off-by: cbh778899 <[email protected]> * implement export history Signed-off-by: cbh778899 <[email protected]> * update style Signed-off-by: cbh778899 <[email protected]> --------- Signed-off-by: cbh778899 <[email protected]>
- Loading branch information
Showing
8 changed files
with
248 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const { ipcRenderer } = require('electron'); | ||
const { join } = require("path") | ||
const { writeFileSync, readFileSync } = require("fs") | ||
|
||
let save_path = '.'; | ||
|
||
async function initer() { | ||
const {downloads_path} = await ipcRenderer.invoke('os-settings'); | ||
save_path = downloads_path; | ||
} | ||
initer(); | ||
|
||
async function saveFile(name, content) { | ||
const { canceled, filePath } = await ipcRenderer.invoke('show-save-dialog', { | ||
title: "Save Chat History", | ||
defaultPath: join(save_path, name) | ||
}) | ||
|
||
if(canceled || !filePath) { | ||
return false; | ||
} else { | ||
writeFileSync(filePath, content, 'utf-8'); | ||
return true; | ||
} | ||
} | ||
|
||
async function loadFile() { | ||
const { canceled, filePaths } = await ipcRenderer.invoke('show-open-dialog', { | ||
title: "Load Chat History", | ||
properties: ["openFile"] | ||
}) | ||
|
||
if(canceled || !filePaths) { | ||
return null; | ||
} else { | ||
const content = readFileSync(filePaths[0], 'utf-8'); | ||
return content; | ||
} | ||
} | ||
|
||
module.exports = { | ||
saveFile, | ||
loadFile | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* @typedef HistoryItem | ||
* @property {"user"|"assistant"|"system"} role | ||
* @property {String} content | ||
* @property {Number} createdAt | ||
*/ | ||
|
||
/** | ||
* @typedef HistoryInfo | ||
* @property {Number} createdAt | ||
* @property {String} title | ||
*/ | ||
|
||
/** | ||
* Export history by generate a file | ||
* @param {"Human"|"JSON"} format the format to export, either human readable or json | ||
* @param {HistoryInfo} history_info the information of the session, includes create time and title | ||
* @param {HistoryItem[]} history the history to be exported | ||
*/ | ||
export async function exportChatHistory(format, history_info, history) { | ||
let formatted_sentence; | ||
switch(format) { | ||
case "JSON": | ||
formatted_sentence = jsonFormator(history_info, history); | ||
break; | ||
case "Human": | ||
formatted_sentence = humanFormator(history_info, history); | ||
break; | ||
} | ||
// const file = new Blob([formatted_sentence], { type: "text/plain" }); | ||
|
||
const extension = | ||
format === "JSON" ? 'json' : | ||
format === "Human" ? 'txt' : | ||
'' | ||
|
||
await window['file-handler'].saveFile(`${history_info.title}.${extension}`, formatted_sentence); | ||
} | ||
|
||
function dateFormator(timestamp) { | ||
const dt = new Date(timestamp); | ||
return `${dt.toDateString()}, ${dt.toTimeString().match(/(\d{2}:\d{2}:\d{2})/)[0]}` | ||
} | ||
|
||
/** | ||
* Format history to human readable format and return the formatted string | ||
* @param {HistoryInfo} history_info the information of the session to be formatted | ||
* @param {HistoryItem[]} history the history to be formatted | ||
* @returns {String} | ||
*/ | ||
function humanFormator(history_info, history) { | ||
const parts = [ | ||
`${history_info.title} | ||
${dateFormator(history_info.createdAt)}` | ||
] | ||
|
||
let current_part = ''; | ||
for(const { role, content, createdAt } of history) { | ||
current_part += | ||
role === "user" ? 'user ' : | ||
role === "system" ? 'system ': | ||
role === 'assistant' ? 'assistant' : '' | ||
current_part += ` (${dateFormator(createdAt)}): ` | ||
current_part += content | ||
|
||
if(/^(system|user)$/.test(role)) { | ||
current_part += '\n'; | ||
} else { | ||
parts.push(current_part); | ||
current_part = '' | ||
} | ||
} | ||
|
||
return parts.join('\n\n---\n\n') | ||
} | ||
|
||
/** | ||
* Format history to json format and return the formatted string | ||
* @param {HistoryInfo} history_info the information of the session to be formatted | ||
* @param {HistoryItem[]} history the history to be formatted | ||
* @returns {String} | ||
*/ | ||
function jsonFormator(history_info, history) { | ||
const { title, createdAt } = history_info; | ||
const formatted = | ||
`{ | ||
"title": "${title}", | ||
"createdAt": ${createdAt}, | ||
"messages": [ | ||
${history.map(({role, content, createdAt})=>{ | ||
const msg_str = content.replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll("\n", "\\n") | ||
return `{ "role": "${role}", "message": "${msg_str}", "createdAt": ${createdAt} }` | ||
}).join(`,\n${" ".repeat(8)}`)} | ||
] | ||
}` | ||
return formatted | ||
} |