@memori.ai/memori-react
Version:
[](https://www.npmjs.com/package/@memori.ai/memori-react)   => value
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"')
.replaceAll("'", ''');
export const getMetricValue = (metric) => {
if (typeof metric === 'number' && Number.isFinite(metric))
return metric;
if (!metric || typeof metric !== 'object')
return undefined;
if (typeof metric.parsedValue === 'number' &&
Number.isFinite(metric.parsedValue)) {
return metric.parsedValue;
}
if (typeof metric.source === 'string') {
const parsed = Number(metric.source);
if (Number.isFinite(parsed))
return parsed;
}
return undefined;
};
const formatMetricValue = (value, locale = 'it-IT') => {
if (!Number.isFinite(value))
return '—';
if (value === 0)
return '0';
const absValue = Math.abs(value);
return new Intl.NumberFormat(locale, {
minimumFractionDigits: 0,
maximumFractionDigits: absValue >= 1 ? 3 : 4,
}).format(value);
};
export const formatIntegerValue = (value, locale = 'it-IT') => {
if (!Number.isFinite(value))
return '0';
return new Intl.NumberFormat(locale, { maximumFractionDigits: 0 }).format(value);
};
export const formatDuration = (durationMs, locale = 'it-IT') => {
if (typeof durationMs !== 'number' || !Number.isFinite(durationMs)) {
return '—';
}
if (durationMs < 1000)
return `${formatIntegerValue(durationMs, locale)} ms`;
return `${formatMetricValue(durationMs / 1000, locale)} s`;
};
const floorToSingleDecimal = (value) => Math.floor(value * 10) / 10;
const formatComparisonNumber = (value, locale = 'it-IT') => new Intl.NumberFormat(locale, {
minimumFractionDigits: 1,
maximumFractionDigits: 1,
}).format(floorToSingleDecimal(value));
const formatReadableDuration = (seconds, locale = 'it-IT') => {
if (!Number.isFinite(seconds) || seconds <= 0)
return '0 s';
if (seconds < 60)
return `${formatComparisonNumber(seconds, locale)} s`;
const minutes = seconds / 60;
if (minutes < 60)
return `${formatComparisonNumber(minutes, locale)} min`;
return `${formatComparisonNumber(minutes / 60, locale)} h`;
};
const formatReadableDistance = (meters, locale = 'it-IT') => {
if (!Number.isFinite(meters) || meters <= 0)
return '0 m';
if (meters < 1000)
return `${formatComparisonNumber(meters, locale)} m`;
return `${formatComparisonNumber(meters / 1000, locale)} km`;
};
const getApiUnitToBaseFactor = (unitFromApi, metricType) => {
const u = (unitFromApi !== null && unitFromApi !== void 0 ? unitFromApi : '').trim().toLowerCase();
if (metricType === 'energy') {
if (u === 'kwh')
return 1;
if (u === 'wh')
return 0.001;
if (u === 'mwh')
return 0.000001;
return 1;
}
if (metricType === 'co2') {
if (u === 'kg' || u === 'kgco2eq')
return 1;
if (u === 'g')
return 0.001;
if (u === 'mg')
return 0.000001;
return 1;
}
if (u === 'l')
return 1;
if (u === 'ml')
return 0.001;
if (u === 'μl' || u === 'ul')
return 0.000001;
return 1;
};
export const formatImpactInReadableUnit = (value, metricType, locale = 'it-IT') => {
const absValue = Math.abs(value);
if (metricType === 'energy') {
if (absValue >= 1)
return `${formatMetricValue(value, locale)} kWh`;
const wattHours = value * 1000;
if (Math.abs(wattHours) >= 1)
return `${formatMetricValue(wattHours, locale)} Wh`;
return `${formatMetricValue(wattHours * 1000, locale)} mWh`;
}
if (metricType === 'co2') {
if (absValue >= 1)
return `${formatMetricValue(value, locale)} kg`;
const grams = value * 1000;
if (Math.abs(grams) >= 1)
return `${formatMetricValue(grams, locale)} g`;
return `${formatMetricValue(grams * 1000, locale)} mg`;
}
if (absValue >= 1)
return `${formatMetricValue(value, locale)} L`;
const milliliters = value * 1000;
if (Math.abs(milliliters) >= 1)
return `${formatMetricValue(milliliters, locale)} mL`;
return `${formatMetricValue(milliliters * 1000, locale)} μL`;
};
export const formatImpactWithApiUnit = (value, unitFromApi, fallbackUnit, metricType, locale = 'it-IT') => {
const factor = getApiUnitToBaseFactor(unitFromApi !== null && unitFromApi !== void 0 ? unitFromApi : fallbackUnit, metricType);
const baseValue = value * factor;
return formatImpactInReadableUnit(baseValue, metricType, locale);
};
export const getImpactComparison = (value, metricType, locale = 'it-IT', t) => {
if (!Number.isFinite(value) || value <= 0) {
return t('chatLogs.impactComparisonUnavailable');
}
if (metricType === 'energy') {
const ledSeconds = (value * 1000 * 3600) / 10;
return t('chatLogs.impactComparisonEnergy', {
duration: formatReadableDuration(ledSeconds, locale),
});
}
if (metricType === 'co2') {
const averageCarMeters = (value / 0.12) * 1000;
return t('chatLogs.impactComparisonCo2', {
distance: formatReadableDistance(averageCarMeters, locale),
});
}
const drops = (value * 1000) / 0.05;
return t('chatLogs.impactComparisonWater', {
count: formatComparisonNumber(drops, locale),
});
};
const buildUsageBadgeHtml = ({ badgeType, badgeClassName, label, value, lineIndex, emoji, }) => {
const escapedLabel = escapeHtml(label);
const valueHtml = value
? `<span class="memori-chat--usage-badge-value">${value}</span>`
: '';
const content = valueHtml ||
`<span class="memori-chat--usage-badge-label">${escapedLabel}</span>`;
return `<button type="button" class="memori-chat--usage-badge ${badgeClassName}" data-llm-badge-type="${badgeType}" data-line-index="${lineIndex}" aria-label="${escapedLabel}">${emoji} ${content}</button>`;
};
export const buildLlmUsageHtml = (usage, labels, lineIndex, locale = 'it-IT') => {
var _a, _b, _c, _d, _e, _f;
const badges = [
buildUsageBadgeHtml({
badgeType: 'llm',
badgeClassName: 'memori-chat--usage-badge-llm',
label: labels.llm,
lineIndex,
emoji: BADGE_EMOJI.llm,
}),
];
const energy = getMetricValue((_a = usage.energyImpact) === null || _a === void 0 ? void 0 : _a.energy);
const gwp = getMetricValue((_b = usage.energyImpact) === null || _b === void 0 ? void 0 : _b.gwp);
const wcf = getMetricValue((_c = usage.energyImpact) === null || _c === void 0 ? void 0 : _c.wcf);
if (typeof energy === 'number') {
const energyFormatted = formatImpactWithApiUnit(energy, (_d = usage.energyImpact) === null || _d === void 0 ? void 0 : _d.energyUnit, 'kWh', 'energy', locale);
badges.push(buildUsageBadgeHtml({
badgeType: 'energy',
badgeClassName: 'memori-chat--usage-badge-energy',
label: `${labels.energy} ${energyFormatted}`,
value: escapeHtml(energyFormatted),
lineIndex,
emoji: BADGE_EMOJI.energy,
}));
}
if (typeof gwp === 'number') {
const co2Formatted = formatImpactWithApiUnit(gwp, (_e = usage.energyImpact) === null || _e === void 0 ? void 0 : _e.gwpUnit, 'kgCO2eq', 'co2', locale);
badges.push(buildUsageBadgeHtml({
badgeType: 'co2',
badgeClassName: 'memori-chat--usage-badge-co2',
label: `${labels.co2} ${co2Formatted}`,
value: escapeHtml(co2Formatted),
lineIndex,
emoji: BADGE_EMOJI.co2,
}));
}
if (typeof wcf === 'number') {
const waterFormatted = formatImpactWithApiUnit(wcf, (_f = usage.energyImpact) === null || _f === void 0 ? void 0 : _f.wcfUnit, 'L', 'water', locale);
badges.push(buildUsageBadgeHtml({
badgeType: 'water',
badgeClassName: 'memori-chat--usage-badge-water',
label: `${labels.water} ${waterFormatted}`,
value: escapeHtml(waterFormatted),
lineIndex,
emoji: BADGE_EMOJI.water,
}));
}
return `<div class="memori-chat--llm-usage" data-llm-usage><hr class="memori-chat--llm-usage-hr" /><p class="memori-chat--llm-usage-hint">${escapeHtml(labels.usageBadgesHint)}</p><div class="memori-chat--llm-usage-badges">${badges.join('')}</div></div>`;
};
//# sourceMappingURL=llmUsage.js.map