@intelidexer/react-native-chat-screen
Version:
A chat screen for InteliDexer based AI Agents.
92 lines (91 loc) • 3.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractOptInActions = exports.enforceTextDirection = exports.detectLanguage = exports.id = void 0;
function id() {
return Math.random().toString(36).slice(2);
}
exports.id = id;
// Language detection utility
function detectLanguage(text) {
if (!text || text.trim().length === 0)
return 'en';
const cleanText = text
.replace(/[#*_`\[\]()]/g, '') // Remove markdown characters
.replace(/\s+/g, ' ') // Normalize whitespace
.trim();
if (cleanText.length === 0)
return 'en';
// Check for Arabic characters
const arabicRegex = /[\u0600-\u06FF]/;
const hasArabicChars = arabicRegex.test(cleanText);
if (hasArabicChars) {
const arabicCharCount = (cleanText.match(/[\u0600-\u06FF]/g) || []).length;
const totalCharCount = cleanText.replace(/\s/g, '').length;
const arabicRatio = arabicCharCount / totalCharCount;
// If more than 30% of characters are Arabic, consider it Arabic
return arabicRatio > 0.3 ? 'ar' : 'en';
}
return 'en';
}
exports.detectLanguage = detectLanguage;
const RTL_ISOLATE = '\u2067';
const LTR_ISOLATE = '\u2066';
const POP_DIRECTION_ISOLATE = '\u2069';
function enforceTextDirection(text, direction) {
if (!text)
return text;
const isolateMark = direction === 'rtl' ? RTL_ISOLATE : LTR_ISOLATE;
const trimmed = text.trim();
if (!trimmed) {
return text;
}
const alreadyWrapped = trimmed.startsWith(isolateMark) && trimmed.endsWith(POP_DIRECTION_ISOLATE);
if (alreadyWrapped) {
return text;
}
return `${isolateMark}${text}${POP_DIRECTION_ISOLATE}`;
}
exports.enforceTextDirection = enforceTextDirection;
const extractOptInActions = (content) => {
if (!content)
return { text: "", optIns: [] };
const actionRegex = /<actions>\s*<opt_in>([\s\S]*?)<\/opt_in>\s*<\/actions>/gi;
const matches = Array.from(content.matchAll(actionRegex));
const optIns = [];
for (const match of matches) {
const optInBlock = match[1];
if (!optInBlock)
continue;
const payloadMatch = optInBlock.match(/\{([\s\S]*?)\}/);
if (!payloadMatch || !payloadMatch[1])
continue;
const entries = payloadMatch[1].split(',');
const optIn = {};
for (const entry of entries) {
const [rawKey, ...rest] = entry.split(':');
if (!rawKey || rest.length === 0)
continue;
const key = rawKey.trim().replace(/^['"]|['"]$/g, '');
let value = rest.join(':').trim();
value = value.replace(/^['"]|['"]$/g, '').trim();
const normalizedKey = key.replace(/[_\s]/g, '').toLowerCase();
if (normalizedKey === 'name')
optIn.name = value;
if (normalizedKey === 'date')
optIn.date = value;
if (normalizedKey === 'time')
optIn.time = value;
if (normalizedKey === 'sessionid')
optIn.sessionId = value;
if (normalizedKey === 'id')
optIn.id = value;
}
if (Object.keys(optIn).length > 0) {
optIns.push(optIn);
}
}
const anyActionRegex = /<actions>[\s\S]*?<\/actions>/gi;
const remainingText = content.replace(anyActionRegex, '').trim();
return { text: remainingText, optIns };
};
exports.extractOptInActions = extractOptInActions;