diff --git a/src/endpoints/chats.js b/src/endpoints/chats.js index 62c790400..2df194797 100644 --- a/src/endpoints/chats.js +++ b/src/endpoints/chats.js @@ -264,6 +264,34 @@ function flattenChubChat(userName, characterName, lines) { return (lines ?? []).map(convert).join('\n'); } +/** + * Imports a chat from RisuAI format. + * @param {string} userName User name + * @param {string} characterName Character name + * @param {object} jsonData Imported chat data + * @returns {string} Chat data + */ +function importRisuChat(userName, characterName, jsonData) { + /** @type {object[]} */ + const chat = [{ + user_name: userName, + character_name: characterName, + create_date: humanizedISO8601DateTime(), + }]; + + for (const message of jsonData.data.message) { + const isUser = message.role === 'user'; + chat.push({ + name: message.name ?? (isUser ? userName : characterName), + is_user: isUser, + send_date: Number(message.time ?? Date.now()), + mes: message.data ?? '', + }); + } + + return chat.map(obj => JSON.stringify(obj)).join('\n'); +} + export const router = express.Router(); router.post('/save', jsonParser, function (request, response) { @@ -481,6 +509,8 @@ router.post('/import', urlencodedParser, function (request, response) { importFunc = importOobaChat; } else if (Array.isArray(jsonData.messages)) { // Agnai's format importFunc = importAgnaiChat; + } else if (jsonData.type === 'risuChat') { // RisuAI format + importFunc = importRisuChat; } else { // Unknown format console.log('Incorrect chat format .json'); return response.send({ error: true });