Fix and UI imporvements 2
This commit is contained in:
@@ -118,22 +118,98 @@ export function renderDisabledReason(
|
||||
|
||||
/**
|
||||
* Render AI reasoning summary with parameter substitution
|
||||
* Enhanced to extract orchestrator reasoning from event_metadata.reasoning_data
|
||||
*/
|
||||
export function renderAIReasoning(
|
||||
event: Alert,
|
||||
t: TFunction
|
||||
): string | null {
|
||||
if (!event.ai_reasoning?.summary_key) return null;
|
||||
|
||||
try {
|
||||
return t(
|
||||
event.ai_reasoning.summary_key,
|
||||
event.ai_reasoning.summary_params || {}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error rendering AI reasoning:', error);
|
||||
return null;
|
||||
// First, try the standard ai_reasoning field
|
||||
if (event.ai_reasoning?.summary_key) {
|
||||
try {
|
||||
return t(
|
||||
event.ai_reasoning.summary_key,
|
||||
event.ai_reasoning.summary_params || {}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error rendering AI reasoning:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// For orchestrator-generated POs, extract reasoning from event_metadata.reasoning_data
|
||||
if (event.event_type?.includes('po') && event.event_metadata?.reasoning_data) {
|
||||
try {
|
||||
const reasoningData = event.event_metadata.reasoning_data;
|
||||
const params = reasoningData.parameters || {};
|
||||
const metadata = reasoningData.metadata || {};
|
||||
|
||||
// Check if this is an orchestrator-generated PO
|
||||
const isOrchestratorGenerated =
|
||||
metadata.ai_assisted === true ||
|
||||
metadata.trigger_source === 'orchestrator_auto' ||
|
||||
event.event_metadata?.source === 'orchestrator' ||
|
||||
event.event_metadata?.auto_generated === true;
|
||||
|
||||
if (!isOrchestratorGenerated) return null;
|
||||
|
||||
// Extract reasoning details from parameters
|
||||
const minDepletionHours = params.min_depletion_hours;
|
||||
const minDepletionDays = params.min_depletion_days;
|
||||
const supplierLeadTimeDays = params.supplier_lead_time_days;
|
||||
const orderUrgency = params.order_urgency;
|
||||
const potentialLossEur = params.potential_loss_eur;
|
||||
const affectedBatches = params.affected_batches || [];
|
||||
const supplierName = params.supplier_name;
|
||||
const criticalProducts = params.critical_products || [];
|
||||
const productDetails = params.product_details || [];
|
||||
|
||||
// Build ICU MessageFormat parameters
|
||||
const i18nParams: Record<string, any> = {
|
||||
critical_product_count: criticalProducts.length || 0,
|
||||
min_depletion_hours: minDepletionHours || 0,
|
||||
min_depletion_days: minDepletionDays || 0,
|
||||
supplier_name: supplierName || t('common:supplier'),
|
||||
supplier_lead_time_days: supplierLeadTimeDays || 0,
|
||||
order_urgency: orderUrgency || 'now',
|
||||
affected_batches_count: affectedBatches.length || 0,
|
||||
potential_loss_eur: potentialLossEur || 0,
|
||||
};
|
||||
|
||||
// Add critical product names to parameters
|
||||
criticalProducts.forEach((product: string, index: number) => {
|
||||
i18nParams[`critical_products_${index}`] = product;
|
||||
});
|
||||
|
||||
// Add affected batch names to parameters
|
||||
affectedBatches.forEach((batch: string, index: number) => {
|
||||
i18nParams[`affected_batches_${index}`] = batch;
|
||||
});
|
||||
|
||||
// Generate reasoning text using i18n template
|
||||
let result = t('reasoning:purchaseOrder.low_stock_detection_detailed', i18nParams);
|
||||
|
||||
// Add stock details for each product if available
|
||||
if (productDetails.length > 0) {
|
||||
const stockDetails = productDetails.map((detail: any) => {
|
||||
const productName = detail.product_name;
|
||||
const currentStock = detail.current_stock_kg;
|
||||
const reorderPoint = detail.reorder_point_kg;
|
||||
const daysUntilDepletion = detail.days_until_depletion;
|
||||
|
||||
return `${productName}: ${currentStock?.toFixed(1)}kg actual vs ${reorderPoint?.toFixed(0)}kg punto de reorden (${daysUntilDepletion?.toFixed(1)} días hasta agotamiento)`;
|
||||
}).join('. ');
|
||||
|
||||
result += ` ${stockDetails}`;
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error('Error extracting orchestrator reasoning:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
|
||||
Reference in New Issue
Block a user