1414import json
1515from typing import Any , Dict , Optional
1616
17- from pydantic import BaseModel , Field , validator
18- from langchain_core .prompts import PromptTemplate
1917from langchain_core .output_parsers import StrOutputParser
18+ from langchain_core .prompts import PromptTemplate
19+ from pydantic import BaseModel , Field , validator
2020
2121from ..prompts import (
2222 TEMPLATE_EXECUTION_ANALYSIS ,
2828
2929class AnalysisError (Exception ):
3030 """Base exception for code analysis errors."""
31+
3132 pass
3233
3334
3435class InvalidStateError (AnalysisError ):
3536 """Exception raised when state dictionary is missing required keys."""
37+
3638 pass
3739
3840
3941class CodeAnalysisState (BaseModel ):
4042 """Base model for code analysis state validation."""
43+
4144 generated_code : str = Field (..., description = "The generated code to analyze" )
42- errors : Dict [str , Any ] = Field (..., description = "Dictionary containing error information" )
45+ errors : Dict [str , Any ] = Field (
46+ ..., description = "Dictionary containing error information"
47+ )
4348
44- @validator (' errors' )
49+ @validator (" errors" )
4550 def validate_errors (cls , v ):
4651 """Ensure errors dictionary has expected structure."""
4752 if not isinstance (v , dict ):
@@ -51,28 +56,30 @@ def validate_errors(cls, v):
5156
5257class ExecutionAnalysisState (CodeAnalysisState ):
5358 """Model for execution analysis state validation."""
59+
5460 html_code : Optional [str ] = Field (None , description = "HTML code if available" )
5561 html_analysis : Optional [str ] = Field (None , description = "Analysis of HTML code" )
5662
57- @validator (' errors' )
63+ @validator (" errors" )
5864 def validate_execution_errors (cls , v ):
5965 """Ensure errors dictionary contains execution key."""
6066 super ().validate_errors (v )
61- if ' execution' not in v :
67+ if " execution" not in v :
6268 raise ValueError ("errors dictionary must contain 'execution' key" )
6369 return v
6470
6571
6672class ValidationAnalysisState (CodeAnalysisState ):
6773 """Model for validation analysis state validation."""
74+
6875 json_schema : Dict [str , Any ] = Field (..., description = "JSON schema for validation" )
6976 execution_result : Any = Field (..., description = "Result of code execution" )
7077
71- @validator (' errors' )
78+ @validator (" errors" )
7279 def validate_validation_errors (cls , v ):
7380 """Ensure errors dictionary contains validation key."""
7481 super ().validate_errors (v )
75- if ' validation' not in v :
82+ if " validation" not in v :
7683 raise ValueError ("errors dictionary must contain 'validation' key" )
7784 return v
7885
@@ -121,7 +128,7 @@ def syntax_focused_analysis(state: Dict[str, Any], llm_model) -> str:
121128 # Validate state using Pydantic model
122129 validated_state = CodeAnalysisState (
123130 generated_code = state .get ("generated_code" , "" ),
124- errors = state .get ("errors" , {})
131+ errors = state .get ("errors" , {}),
125132 )
126133
127134 # Check if syntax errors exist
@@ -131,15 +138,17 @@ def syntax_focused_analysis(state: Dict[str, Any], llm_model) -> str:
131138 # Create prompt template and chain
132139 prompt = PromptTemplate (
133140 template = get_optimal_analysis_template ("syntax" ),
134- input_variables = ["generated_code" , "errors" ]
141+ input_variables = ["generated_code" , "errors" ],
135142 )
136143 chain = prompt | llm_model | StrOutputParser ()
137144
138145 # Execute chain with validated state
139- return chain .invoke ({
140- "generated_code" : validated_state .generated_code ,
141- "errors" : validated_state .errors ["syntax" ]
142- })
146+ return chain .invoke (
147+ {
148+ "generated_code" : validated_state .generated_code ,
149+ "errors" : validated_state .errors ["syntax" ],
150+ }
151+ )
143152
144153 except KeyError as e :
145154 raise InvalidStateError (f"Missing required key in state dictionary: { e } " )
@@ -176,7 +185,7 @@ def execution_focused_analysis(state: Dict[str, Any], llm_model) -> str:
176185 generated_code = state .get ("generated_code" , "" ),
177186 errors = state .get ("errors" , {}),
178187 html_code = state .get ("html_code" , "" ),
179- html_analysis = state .get ("html_analysis" , "" )
188+ html_analysis = state .get ("html_analysis" , "" ),
180189 )
181190
182191 # Create prompt template and chain
@@ -187,12 +196,14 @@ def execution_focused_analysis(state: Dict[str, Any], llm_model) -> str:
187196 chain = prompt | llm_model | StrOutputParser ()
188197
189198 # Execute chain with validated state
190- return chain .invoke ({
191- "generated_code" : validated_state .generated_code ,
192- "errors" : validated_state .errors ["execution" ],
193- "html_code" : validated_state .html_code ,
194- "html_analysis" : validated_state .html_analysis ,
195- })
199+ return chain .invoke (
200+ {
201+ "generated_code" : validated_state .generated_code ,
202+ "errors" : validated_state .errors ["execution" ],
203+ "html_code" : validated_state .html_code ,
204+ "html_analysis" : validated_state .html_analysis ,
205+ }
206+ )
196207
197208 except KeyError as e :
198209 raise InvalidStateError (f"Missing required key in state dictionary: { e } " )
@@ -230,23 +241,30 @@ def validation_focused_analysis(state: Dict[str, Any], llm_model) -> str:
230241 generated_code = state .get ("generated_code" , "" ),
231242 errors = state .get ("errors" , {}),
232243 json_schema = state .get ("json_schema" , {}),
233- execution_result = state .get ("execution_result" , {})
244+ execution_result = state .get ("execution_result" , {}),
234245 )
235246
236247 # Create prompt template and chain
237248 prompt = PromptTemplate (
238249 template = get_optimal_analysis_template ("validation" ),
239- input_variables = ["generated_code" , "errors" , "json_schema" , "execution_result" ],
250+ input_variables = [
251+ "generated_code" ,
252+ "errors" ,
253+ "json_schema" ,
254+ "execution_result" ,
255+ ],
240256 )
241257 chain = prompt | llm_model | StrOutputParser ()
242258
243259 # Execute chain with validated state
244- return chain .invoke ({
245- "generated_code" : validated_state .generated_code ,
246- "errors" : validated_state .errors ["validation" ],
247- "json_schema" : validated_state .json_schema ,
248- "execution_result" : validated_state .execution_result ,
249- })
260+ return chain .invoke (
261+ {
262+ "generated_code" : validated_state .generated_code ,
263+ "errors" : validated_state .errors ["validation" ],
264+ "json_schema" : validated_state .json_schema ,
265+ "execution_result" : validated_state .execution_result ,
266+ }
267+ )
250268
251269 except KeyError as e :
252270 raise InvalidStateError (f"Missing required key in state dictionary: { e } " )
@@ -286,7 +304,7 @@ def semantic_focused_analysis(
286304 # Validate state using Pydantic model
287305 validated_state = CodeAnalysisState (
288306 generated_code = state .get ("generated_code" , "" ),
289- errors = state .get ("errors" , {})
307+ errors = state .get ("errors" , {}),
290308 )
291309
292310 # Validate comparison_result
@@ -303,11 +321,13 @@ def semantic_focused_analysis(
303321 chain = prompt | llm_model | StrOutputParser ()
304322
305323 # Execute chain with validated inputs
306- return chain .invoke ({
307- "generated_code" : validated_state .generated_code ,
308- "differences" : json .dumps (comparison_result ["differences" ], indent = 2 ),
309- "explanation" : comparison_result ["explanation" ],
310- })
324+ return chain .invoke (
325+ {
326+ "generated_code" : validated_state .generated_code ,
327+ "differences" : json .dumps (comparison_result ["differences" ], indent = 2 ),
328+ "explanation" : comparison_result ["explanation" ],
329+ }
330+ )
311331
312332 except KeyError as e :
313333 raise InvalidStateError (f"Missing required key: { e } " )
0 commit comments