Add RisuAI chats JSON import

ac455ebb45235e3f842f49f983f6cc452c7acaa8

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +30 -0Ignore whitespace
src/endpoints/chats.js+30 -0
@@ -264,6 +264,34 @@ function flattenChubChat(userName, characterName, lines) {
264264 return (lines ?? []).map(convert).join('\n');
265265}
266266
267+/**
268+ * Imports a chat from RisuAI format.
269+ * @param {string} userName User name
270+ * @param {string} characterName Character name
271+ * @param {object} jsonData Imported chat data
272+ * @returns {string} Chat data
273+ */
274+function importRisuChat(userName, characterName, jsonData) {
275+ /** @type {object[]} */
276+ const chat = [{
277+ user_name: userName,
278+ character_name: characterName,
279+ create_date: humanizedISO8601DateTime(),
280+ }];
281+
282+ for (const message of jsonData.data.message) {
283+ const isUser = message.role === 'user';
284+ chat.push({
285+ name: message.name ?? (isUser ? userName : characterName),
286+ is_user: isUser,
287+ send_date: Number(message.time ?? Date.now()),
288+ mes: message.data ?? '',
289+ });
290+ }
291+
292+ return chat.map(obj => JSON.stringify(obj)).join('\n');
293+}
294+
267295export const router = express.Router();
268296
269297router.post('/save', jsonParser, function (request, response) {
@@ -481,6 +509,8 @@ router.post('/import', urlencodedParser, function (request, response) {
481509 importFunc = importOobaChat;
482510 } else if (Array.isArray(jsonData.messages)) { // Agnai's format
483511 importFunc = importAgnaiChat;
512+ } else if (jsonData.type === 'risuChat') { // RisuAI format
513+ importFunc = importRisuChat;
484514 } else { // Unknown format
485515 console.log('Incorrect chat format .json');
486516 return response.send({ error: true });