|
| 1 | +import { DecisionLog } from '../../types'; |
| 2 | + |
| 3 | +export interface RiskEvaluationInput { |
| 4 | + action: string; |
| 5 | + context: Record<string, any>; |
| 6 | +} |
| 7 | + |
| 8 | +export interface RiskEvaluationOutput { |
| 9 | + riskScore: number; |
| 10 | + warnings: string[]; |
| 11 | + recommendedAlternative?: string; |
| 12 | + explainability: string; |
| 13 | + categoryScores?: Record<string, number>; |
| 14 | + futureSimulation?: any; |
| 15 | + confidence?: number; |
| 16 | + activeRiskLoad?: number; |
| 17 | +} |
| 18 | + |
| 19 | +// Dummy similarity function |
| 20 | +function similarity(a: any, b: any): number { |
| 21 | + // Simple context overlap |
| 22 | + let score = 0; |
| 23 | + for (const key in a) { |
| 24 | + if (b[key] !== undefined && a[key] === b[key]) score += 1; |
| 25 | + } |
| 26 | + return score; |
| 27 | +} |
| 28 | + |
| 29 | +export function evaluateRisk( |
| 30 | + input: RiskEvaluationInput, |
| 31 | + pastDecisions: DecisionLog[] |
| 32 | +): RiskEvaluationOutput { |
| 33 | + // Find similar past decisions |
| 34 | + const similar = pastDecisions |
| 35 | + .map(d => ({ d, sim: similarity(input.context, d.context) })) |
| 36 | + .filter(x => x.sim > 0) |
| 37 | + .sort((a, b) => b.sim - a.sim) |
| 38 | + .slice(0, 5); |
| 39 | + |
| 40 | + // Calculate risk factors |
| 41 | + let riskScore = 0; |
| 42 | + let warnings: string[] = []; |
| 43 | + let categoryScores: Record<string, number> = {}; |
| 44 | + |
| 45 | + // Similar past failures |
| 46 | + const failures = similar.filter(x => x.d.outcome !== 'success'); |
| 47 | + if (failures.length) { |
| 48 | + riskScore += failures.length * 15; |
| 49 | + warnings.push('Similar past actions resulted in failure.'); |
| 50 | + } |
| 51 | + |
| 52 | + // Sleep deficit |
| 53 | + if (input.context.sleep !== undefined && input.context.sleep < 6) { |
| 54 | + riskScore += 20; |
| 55 | + warnings.push('Sleep deficit detected.'); |
| 56 | + categoryScores['physical'] = 20; |
| 57 | + } |
| 58 | + |
| 59 | + // Overcommitment |
| 60 | + if (input.context.active_projects !== undefined && input.context.active_projects > 3) { |
| 61 | + riskScore += 15; |
| 62 | + warnings.push('Too many active projects.'); |
| 63 | + categoryScores['execution'] = 15; |
| 64 | + } |
| 65 | + |
| 66 | + // Stress level |
| 67 | + if (input.context.stress !== undefined && input.context.stress > 7) { |
| 68 | + riskScore += 20; |
| 69 | + warnings.push('High stress level.'); |
| 70 | + categoryScores['emotional'] = 20; |
| 71 | + } |
| 72 | + |
| 73 | + // Emotional state |
| 74 | + if (input.context.emotion !== undefined && input.context.emotion === 'regret') { |
| 75 | + riskScore += 10; |
| 76 | + warnings.push('Negative emotional state.'); |
| 77 | + categoryScores['emotional'] = (categoryScores['emotional'] || 0) + 10; |
| 78 | + } |
| 79 | + |
| 80 | + // Productivity drop |
| 81 | + if (input.context.productivity_drop !== undefined && input.context.productivity_drop > 2) { |
| 82 | + riskScore += 10; |
| 83 | + warnings.push('Recent productivity drop.'); |
| 84 | + categoryScores['execution'] = (categoryScores['execution'] || 0) + 10; |
| 85 | + } |
| 86 | + |
| 87 | + // Clamp risk score |
| 88 | + riskScore = Math.min(100, riskScore); |
| 89 | + |
| 90 | + // Recommended alternative (dummy) |
| 91 | + let recommendedAlternative = undefined; |
| 92 | + if (riskScore > 60) recommendedAlternative = 'Delay action or reduce workload.'; |
| 93 | + |
| 94 | + // Explainability |
| 95 | + const explainability = `Risk factors: ${warnings.join(' | ')}`; |
| 96 | + |
| 97 | + // Confidence (dummy) |
| 98 | + const confidence = 1 - riskScore / 100; |
| 99 | + |
| 100 | + // Active risk load (dummy) |
| 101 | + const activeRiskLoad = riskScore; |
| 102 | + |
| 103 | + return { |
| 104 | + riskScore, |
| 105 | + warnings, |
| 106 | + recommendedAlternative, |
| 107 | + explainability, |
| 108 | + categoryScores, |
| 109 | + confidence, |
| 110 | + activeRiskLoad, |
| 111 | + }; |
| 112 | +} |
0 commit comments