@@ -60,10 +60,10 @@ def update_document_evaluation_status(document: Document, status: EvaluationStat
6060
6161def extract_document_from_event (event : Dict [str , Any ]) -> Optional [Document ]:
6262 """
63- Extract document from Lambda event
63+ Extract document from Lambda event (state machine format)
6464
6565 Args:
66- event: Lambda event
66+ event: Lambda event containing document data
6767
6868 Returns:
6969 Document object or None if not found
@@ -72,17 +72,16 @@ def extract_document_from_event(event: Dict[str, Any]) -> Optional[Document]:
7272 ValueError: If document cannot be extracted from event
7373 """
7474 try :
75- output_data = json .loads (event ['detail' ]['output' ])
75+ # State machine format: event['document'] contains the document data
76+ document_data = event .get ('document' )
7677
77- if not output_data :
78- raise ValueError ("No output data found in event" )
78+ if not document_data :
79+ raise ValueError ("No document data found in event" )
7980
80- # Get document from the final processing step
81+ # Get document from state machine format
8182 working_bucket = os .environ .get ('WORKING_BUCKET' )
82- # look for document_data in either output_data.Result.document (Pattern-1) or output_data (others)
83- document_data = output_data .get ('Result' ,{}).get ('document' , output_data )
8483 document = Document .load_document (document_data , working_bucket , logger )
85- logger .info (f"Successfully loaded actual document with { len (document .pages )} pages and { len (document .sections )} sections" )
84+ logger .info (f"Successfully loaded document with { len (document .pages )} pages and { len (document .sections )} sections" )
8685 return document
8786 except Exception as e :
8887 logger .error (f"Error extracting document from event: { str (e )} " )
@@ -154,34 +153,43 @@ def handler(event, context):
154153 context: Lambda context
155154
156155 Returns:
157- Response with evaluation results
156+ Document in state machine format: {'document': document.serialize_document()}
158157 """
159158 actual_document = None
160159 start_time = time .time ()
161160
162161 try :
163- logger .info (f"Starting evaluation process with event: { json . dumps ( event , indent = 2 ) } " )
162+ logger .info (f"Starting evaluation process" )
164163
165164 # Extract document from event
166165 actual_document = extract_document_from_event (event )
167166
168- # Update document status to RUNNING
167+ # Load configuration and check if evaluation is enabled
168+ config = get_config ()
169+ evaluation_enabled = config .get ('evaluation' , {}).get ('enabled' , True )
170+
171+ if not evaluation_enabled :
172+ logger .info ("Evaluation is disabled in configuration, skipping evaluation" )
173+ # Return document unchanged
174+ return {'document' : actual_document .serialize_document ()}
175+
176+ # Set document status to EVALUATING before processing
177+ actual_document .status = Status .EVALUATING
178+ document_service .update_document (actual_document )
179+
180+ # Update document evaluation status to RUNNING
169181 update_document_evaluation_status (actual_document , EvaluationStatus .RUNNING )
170182
171183 # Load baseline document
172184 expected_document = load_baseline_document (actual_document .input_key )
173185
174186 # If no baseline document is found, update status and exit
175187 if not expected_document :
176- update_document_evaluation_status (actual_document , EvaluationStatus .NO_BASELINE )
177- return create_response (
178- 200 ,
179- 'Evaluation skipped - no baseline data available' ,
180- {'document_key' : actual_document .input_key }
181- )
188+ actual_document = update_document_evaluation_status (actual_document , EvaluationStatus .NO_BASELINE )
189+ logger .info ("Evaluation skipped - no baseline data available" )
190+ return {'document' : actual_document .serialize_document ()}
182191
183- # Load configuration and create evaluation service
184- config = get_config ()
192+ # Create evaluation service
185193 evaluation_service = evaluation .EvaluationService (config = config )
186194
187195 # Run evaluation
@@ -196,8 +204,8 @@ def handler(event, context):
196204 if evaluated_document .errors :
197205 error_msg = f"Evaluation encountered errors: { evaluated_document .errors } "
198206 logger .error (error_msg )
199- update_document_evaluation_status (evaluated_document , EvaluationStatus .FAILED )
200- return create_response ( 500 , 'Evaluation failed' , { 'error ' : error_msg })
207+ evaluated_document = update_document_evaluation_status (evaluated_document , EvaluationStatus .FAILED )
208+ return { 'document ' : evaluated_document . serialize_document ()}
201209
202210 # Save evaluation results to reporting bucket for analytics using the SaveReportingData Lambda
203211 try :
@@ -224,18 +232,11 @@ def handler(event, context):
224232 # Continue execution - don't fail the entire function if reporting fails
225233
226234 # Update document evaluation status to COMPLETED
227- update_document_evaluation_status (evaluated_document , EvaluationStatus .COMPLETED )
228- logger .info ("Evaluation process completed successfully" )
229-
230- # Return success response
231- return create_response (
232- 200 ,
233- 'Evaluation completed successfully' ,
234- {
235- 'report_location' : evaluated_document .evaluation_report_uri ,
236- 'execution_time' : time .time () - start_time
237- }
238- )
235+ evaluated_document = update_document_evaluation_status (evaluated_document , EvaluationStatus .COMPLETED )
236+ logger .info (f"Evaluation process completed successfully in { time .time () - start_time :.2f} seconds" )
237+
238+ # Return document in state machine format
239+ return {'document' : evaluated_document .serialize_document ()}
239240
240241 except Exception as e :
241242 error_msg = f"Error in lambda_handler: { str (e )} "
@@ -244,8 +245,10 @@ def handler(event, context):
244245 # Update document status to FAILED if we have the document
245246 if actual_document :
246247 try :
247- update_document_evaluation_status (actual_document , EvaluationStatus .FAILED )
248+ actual_document = update_document_evaluation_status (actual_document , EvaluationStatus .FAILED )
249+ return {'document' : actual_document .serialize_document ()}
248250 except Exception as update_error :
249251 logger .error (f"Failed to update evaluation status: { str (update_error )} " )
250252
251- return create_response (500 , 'Evaluation failed' , {'error' : error_msg })
253+ # Re-raise exception to let Step Functions handle the error
254+ raise
0 commit comments