rcc-virtual-model-rules
Version:
RCC Virtual Model Rules Module - Claude Code Router rules implementation
101 lines • 3.41 kB
JavaScript
;
// Rule Evaluator Component for Virtual Model Rules Module
Object.defineProperty(exports, "__esModule", { value: true });
exports.RuleEvaluator = void 0;
/**
* Rule Evaluator component handles condition evaluation and matching logic
* Provides various condition operators and evaluation strategies
*/
class RuleEvaluator {
/**
* Evaluate rule conditions
*/
async evaluateConditions(rule, context) {
console.log(`Evaluating conditions for rule: ${rule.id}`);
const results = [];
let totalConfidence = 0;
let matchCount = 0;
for (const condition of rule.conditions) {
const result = await this.evaluateCondition(condition, context);
results.push(result);
totalConfidence += result.confidence;
if (result.matched)
matchCount++;
}
const avgConfidence = results.length > 0 ? totalConfidence / results.length : 0;
const matched = matchCount === rule.conditions.length;
return {
matched,
confidence: avgConfidence,
details: results
};
}
/**
* Evaluate a single condition
*/
async evaluateCondition(condition, context) {
console.log(`Evaluating condition: ${condition.field} ${condition.operator} ${condition.value}`);
// Get field value from context
const fieldValue = this.getFieldValue(context, condition.field);
// Evaluate based on operator
let matched = false;
let confidence = 0;
switch (condition.operator) {
case 'equals':
matched = fieldValue === condition.value;
confidence = matched ? 1 : 0;
break;
case 'contains':
matched = typeof fieldValue === 'string' && fieldValue.includes(condition.value);
confidence = matched ? 0.9 : 0;
break;
case 'greater_than':
matched = typeof fieldValue === 'number' && fieldValue > condition.value;
confidence = matched ? 1 : 0;
break;
case 'less_than':
matched = typeof fieldValue === 'number' && fieldValue < condition.value;
confidence = matched ? 1 : 0;
break;
// Add more operators as needed
default:
matched = false;
confidence = 0;
}
return {
matched,
confidence,
details: { fieldValue, condition }
};
}
/**
* Get field value from context
*/
getFieldValue(context, field) {
const parts = field.split('.');
let value = context;
for (const part of parts) {
if (value && typeof value === 'object') {
value = value[part];
}
else {
return undefined;
}
}
return value;
}
/**
* Calculate condition weight
*/
calculateConditionWeight(condition) {
return condition.weight || 1;
}
/**
* Validate condition structure
*/
validateCondition(condition) {
return !!(condition.field && condition.operator && condition.value !== undefined);
}
}
exports.RuleEvaluator = RuleEvaluator;
//# sourceMappingURL=RuleEvaluator.js.map