cozy-search
Version:
UI components about search bar and IA assistant
101 lines (78 loc) • 4.26 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.sanitizeChatContent = exports.makeConversationId = exports.isAssistantEnabled = exports.getNameOfConversation = exports.getDescriptionOfConversation = exports.formatConversationDate = void 0;
var _cozyFlags = _interopRequireDefault(require("cozy-flags"));
var makeConversationId = function makeConversationId() {
return "".concat(Date.now(), "-").concat(Math.floor(Math.random() * 90000) + 10000);
};
exports.makeConversationId = makeConversationId;
var isAssistantEnabled = function isAssistantEnabled() {
return (0, _cozyFlags.default)('cozy.assistant.enabled');
};
/**
* Sanitize chat content by removing special sources tags like
* [REF]...[/REF] or [doc_X] that are not currently handled.
*
* @param {string} content - content to sanitize
* @returns {string} sanitized content
*/
exports.isAssistantEnabled = isAssistantEnabled;
var sanitizeChatContent = function sanitizeChatContent(content) {
if (!content) {
return '';
}
return content // Remove REFdoc_1/REF
.replace(/\s?\[REF\][\s\S]*?\[\/REF\]/g, '') // Remove [REF]doc_1[/REF]
.replace(/\s?REF[\s\S]*?\/REF/g, '') // remove « [doc_1] »
.replace(/\s?\[doc_\d+\]/g, '') // remove « [Source 1] », « [Source 4, 6] » or « [Source 4, Source 6] »
.replace(/\s?\[Source\s+\d+(?:\s*,\s*(?:Source\s+)?\d+)*\]/g, '') // remove « [Sources: 1, 3, 6] » citations, with optional empty link parens
.replace(/\s?\[Sources?:\s*\d+(?:\s*,\s*\d+)*\s*\](?:\([^)]*\))?/g, '');
};
exports.sanitizeChatContent = sanitizeChatContent;
var formatConversationDate = function formatConversationDate(dateString, t, lang) {
if (!dateString) return '';
var date = new Date(dateString);
if (isNaN(date.getTime())) return '';
var now = new Date();
var yesterday = new Date(now);
yesterday.setDate(yesterday.getDate() - 1);
var isToday = date.getDate() === now.getDate() && date.getMonth() === now.getMonth() && date.getFullYear() === now.getFullYear();
var isYesterday = date.getDate() === yesterday.getDate() && date.getMonth() === yesterday.getMonth() && date.getFullYear() === yesterday.getFullYear();
if (isToday || isYesterday) {
var timeStr = date.toLocaleTimeString(lang, {
hour: 'numeric',
minute: '2-digit'
});
return "".concat(isToday ? t('assistant.time.today') : t('assistant.time.yesterday'), ", ").concat(timeStr);
}
return date.toLocaleDateString(lang, {
month: 'short',
day: '2-digit',
year: 'numeric'
});
};
/**
* Get name of the conversation
* Since we don't have rule for conversation's name
* So temporary we get the last question from user as name of the conversation
*/
exports.formatConversationDate = formatConversationDate;
var getNameOfConversation = function getNameOfConversation(conversation) {
var _conversation$message, _conversation$message2, _conversation$message3;
return conversation.name || ((_conversation$message = conversation.messages) === null || _conversation$message === void 0 ? void 0 : (_conversation$message2 = _conversation$message[((_conversation$message3 = conversation.messages) === null || _conversation$message3 === void 0 ? void 0 : _conversation$message3.length) - 2]) === null || _conversation$message2 === void 0 ? void 0 : _conversation$message2.content);
};
/**
* Get description of the conversation
* Since we don't have rule for description of the conversation
* So temporary we get the last answer from assistant as description of the conversation
*/
exports.getNameOfConversation = getNameOfConversation;
var getDescriptionOfConversation = function getDescriptionOfConversation(conversation) {
var _conversation$message4, _conversation$message5;
var content = (_conversation$message4 = conversation.messages) === null || _conversation$message4 === void 0 ? void 0 : (_conversation$message5 = _conversation$message4[conversation.messages.length - 1]) === null || _conversation$message5 === void 0 ? void 0 : _conversation$message5.content;
return content && sanitizeChatContent(content);
};
exports.getDescriptionOfConversation = getDescriptionOfConversation;