-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmodels.py
More file actions
140 lines (100 loc) · 5.71 KB
/
Copy pathmodels.py
File metadata and controls
140 lines (100 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""
Data models for the Rule Engine Agent.
"""
from __future__ import annotations
from enum import Enum
from typing import Any
from pydantic import BaseModel, ConfigDict, Field
from src.core.models import Violation # noqa: TCH001, TCH002, TC001
from src.rules.conditions.base import BaseCondition # noqa: TCH001, TCH002, TC001
from src.rules.models import Rule, RuleWhen # noqa: TCH001, TCH002, TC001
class EngineRequest(BaseModel):
"""Request model for the Rule Engine Agent."""
event_type: str = Field(description="The type of event (e.g., pull_request)")
event_data: dict[str, Any] = Field(description="Normalized event payload")
rules: list[Rule | dict[str, Any]] = Field(description="List of active rules")
model_config = ConfigDict(arbitrary_types_allowed=True)
class ValidationStrategy(str, Enum):
"""Validation strategies for rule evaluation."""
VALIDATOR = "validator" # Use fast validator
LLM_REASONING = "llm_reasoning" # Use LLM for complex rules
HYBRID = "hybrid" # Try validator first, fallback to LLM
class ValidatorDescription(BaseModel):
"""Description of a validator for dynamic strategy selection."""
name: str = Field(description="Name of the validator")
description: str = Field(description="What this validator does")
parameter_patterns: list[str] = Field(
description="Parameter patterns this validator can handle", default_factory=list
)
event_types: list[str] = Field(description="Event types this validator supports", default_factory=list)
examples: list[dict[str, Any]] = Field(description="Example rule configurations", default_factory=list)
class StrategySelectionResponse(BaseModel):
"""Structured response for validation strategy selection."""
strategy: ValidationStrategy = Field(description="Selected validation strategy")
validator_name: str | None = Field(description="Name of selected validator or null", default=None)
reasoning: str = Field(description="Explanation of why this strategy was chosen")
class LLMEvaluationResponse(BaseModel):
"""Structured response for LLM rule evaluation."""
is_violated: bool = Field(description="Whether the rule is violated")
message: str = Field(description="Explanation of the violation or why the rule passed")
details: dict[str, Any] = Field(
description="Detailed reasoning and metadata",
default_factory=dict,
)
how_to_fix: str | None = Field(description="Specific instructions on how to fix the violation", default=None)
class HowToFixResponse(BaseModel):
"""Structured response for generating 'how to fix' messages."""
how_to_fix: str = Field(description="Specific, actionable instructions on how to fix the violation")
steps: list[str] = Field(description="Step-by-step instructions", default_factory=list)
examples: list[str] = Field(description="Example commands or actions", default_factory=list)
context: str = Field(description="Additional context or explanation", default="")
class RuleViolation(Violation):
"""Represents a violation of a specific rule."""
# Inherits: rule_description, rule_id, severity, message, details, how_to_fix from Violation
docs_url: str | None = None
validation_strategy: ValidationStrategy = ValidationStrategy.VALIDATOR
execution_time_ms: float = 0.0
class RuleEvaluationResult(BaseModel):
"""The result of evaluating all rules against an event."""
event_type: str
repo_full_name: str
violations: list[RuleViolation] = Field(default_factory=list)
total_rules_evaluated: int = 0
rules_triggered: int = 0
total_rules: int = 0
evaluation_time_ms: float | None = None
validator_usage: dict[str, int] = Field(default_factory=dict) # Track validator usage
llm_usage: int = 0 # Track LLM usage
class RuleDescription(BaseModel):
"""Enhanced rule description with parameters and validation strategy."""
description: str = Field(description="Human-readable description of the rule")
rule_id: str | None = Field(default=None, description="Stable rule ID for acknowledgment lookup")
parameters: dict[str, Any] = Field(default_factory=dict, description="Rule parameters")
event_types: list[str] = Field(default_factory=list, description="Supported event types")
severity: str = Field(default="medium", description="Rule severity level")
validation_strategy: ValidationStrategy = Field(
default=ValidationStrategy.HYBRID, description="Validation strategy"
)
validator_name: str | None = Field(default=None, description="Specific validator to use")
fallback_to_llm: bool = Field(default=True, description="Whether to fallback to LLM if validator fails")
conditions: list["BaseCondition"] = Field(default_factory=list, description="Attached executable conditions") # noqa: UP037
when: RuleWhen | None = Field(
default=None, description="Optional predicate block for conditional rule applicability"
)
model_config = ConfigDict(arbitrary_types_allowed=True)
class EngineState(BaseModel):
"""State for the rule engine workflow."""
event_type: str
event_data: dict[str, Any]
rules: list["Rule"] # noqa: UP037
rule_descriptions: list[RuleDescription] = Field(default_factory=list)
available_validators: list[ValidatorDescription] = Field(default_factory=list)
violations: list[dict[str, Any]] = Field(default_factory=list)
evaluation_context: dict[str, Any] = Field(default_factory=dict)
analysis_steps: list[str] = Field(default_factory=list)
validator_usage: dict[str, int] = Field(default_factory=dict)
llm_usage: int = 0
model_config = ConfigDict(arbitrary_types_allowed=True)
# Update forward references
RuleDescription.model_rebuild()
EngineState.model_rebuild()