| 264 | 264 | return (lines ?? []).map(convert).join('\n'); |
| 265 | 265 | } |
| 266 | 266 | |
| 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 | + |
| 267 | 295 | export const router = express.Router(); |
| 268 | 296 | |
| 269 | 297 | router.post('/save', jsonParser, function (request, response) { |