diff --git a/services/spamshield/src/constants/decision-engine.constants.ts b/services/spamshield/src/constants/decision-engine.constants.ts new file mode 100644 index 0000000..f64c4b7 --- /dev/null +++ b/services/spamshield/src/constants/decision-engine.constants.ts @@ -0,0 +1,27 @@ +/** + * Decision Engine Constants + * + * Scoring weights, thresholds, and behavioral factors used in spam detection. + * These values should be reviewed periodically and updated based on model performance. + */ + +/** Scoring Weights */ +export const DEFAULT_REPUTATION_WEIGHT = 0.4; +export const DEFAULT_RULE_WEIGHT = 0.3; +export const DEFAULT_BEHAVIORAL_WEIGHT = 0.2; +export const DEFAULT_USER_HISTORY_WEIGHT = 0.1; + +/** Decision Thresholds */ +export const DEFAULT_BLOCK_THRESHOLD = 0.85; +export const DEFAULT_FLAG_THRESHOLD = 0.60; + +/** Behavioral Analysis Scores */ +export const SHORT_CALL_SCORE = 0.3; // Call duration < 5 seconds +export const SHORT_SMS_SCORE = 0.1; // SMS call type +export const SHORT_CONTENT_SCORE = 0.2; // SMS body < 10 characters +export const URGENT_KEYWORD_SCORE = 0.3; // Contains urgent/act now/limited keywords + +/** Default Fallback Values */ +export const DEFAULT_EVALUATION_TIMEOUT = 200; // milliseconds +export const DEFAULT_FALLBACK_DECISION = 'ALLOW'; +export const DEFAULT_FALLBACK_ON_TIMEOUT = true; diff --git a/services/spamshield/src/engine/decision-engine.ts b/services/spamshield/src/engine/decision-engine.ts index e2c1d2a..d7ffccc 100644 --- a/services/spamshield/src/engine/decision-engine.ts +++ b/services/spamshield/src/engine/decision-engine.ts @@ -1,5 +1,16 @@ import { SpamShieldService, ReputationResult } from '../services/spamshield.service'; import { RuleEngine, RuleMatch } from './rule-engine'; +import { + DEFAULT_REPUTATION_WEIGHT, + DEFAULT_RULE_WEIGHT, + DEFAULT_BEHAVIORAL_WEIGHT, + DEFAULT_USER_HISTORY_WEIGHT, + DEFAULT_BLOCK_THRESHOLD, + DEFAULT_FLAG_THRESHOLD, + DEFAULT_EVALUATION_TIMEOUT, + DEFAULT_FALLBACK_DECISION, + DEFAULT_FALLBACK_ON_TIMEOUT, +} from '../constants/decision-engine.constants'; export interface CallMetadata { callId: string; @@ -69,17 +80,7 @@ export interface DecisionEngineConfig { fallbackDecision?: 'BLOCK' | 'FLAG' | 'ALLOW'; } -const DEFAULT_CONFIG: Required = { - reputationWeight: 0.4, - ruleWeight: 0.3, - behavioralWeight: 0.2, - userHistoryWeight: 0.1, - blockThreshold: 0.85, - flagThreshold: 0.60, - evaluationTimeout: 200, - fallbackOnTimeout: true, - fallbackDecision: 'ALLOW', -}; +// Configuration defaults exported from constants module export class DecisionEngine { private readonly config: Required; @@ -91,7 +92,17 @@ export class DecisionEngine { ruleEngine: RuleEngine, config?: DecisionEngineConfig ) { - this.config = { ...DEFAULT_CONFIG, ...config }; + this.config = { + reputationWeight: config?.reputationWeight ?? DEFAULT_REPUTATION_WEIGHT, + ruleWeight: config?.ruleWeight ?? DEFAULT_RULE_WEIGHT, + behavioralWeight: config?.behavioralWeight ?? DEFAULT_BEHAVIORAL_WEIGHT, + userHistoryWeight: config?.userHistoryWeight ?? DEFAULT_USER_HISTORY_WEIGHT, + blockThreshold: config?.blockThreshold ?? DEFAULT_BLOCK_THRESHOLD, + flagThreshold: config?.flagThreshold ?? DEFAULT_FLAG_THRESHOLD, + evaluationTimeout: config?.evaluationTimeout ?? DEFAULT_EVALUATION_TIMEOUT, + fallbackOnTimeout: config?.fallbackOnTimeout ?? DEFAULT_FALLBACK_ON_TIMEOUT, + fallbackDecision: config?.fallbackDecision ?? DEFAULT_FALLBACK_DECISION, + }; this.reputationService = reputationService; this.ruleEngine = ruleEngine; }