Skip to content

Commit 8715f6a

Browse files
committed
Updated setp functions tool implementation
1 parent 4137929 commit 8715f6a

File tree

1 file changed

+59
-18
lines changed

1 file changed

+59
-18
lines changed

lib/idp_common_pkg/idp_common/agents/error_analyzer/tools/stepfunction_tool.py

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
@tool
20-
def analyze_workflow_execution(execution_arn: str) -> Dict[str, Any]:
20+
def analyze_workflow_execution(document_id: str = "") -> Dict[str, Any]:
2121
"""
2222
Analyze Step Function workflow execution to identify failures and state transitions.
2323
@@ -27,25 +27,21 @@ def analyze_workflow_execution(execution_arn: str) -> Dict[str, Any]:
2727
document processing failures and understanding workflow behavior.
2828
2929
Use this tool when:
30-
- Document processing failed and you have a Step Function execution ARN
30+
- Document processing failed and you need workflow analysis
3131
- Need to understand where in the workflow a failure occurred
3232
- Investigating workflow performance or timeout issues
3333
- Analyzing state transitions and execution timeline
3434
- User reports document processing stuck or failed
3535
36-
Tool chaining: Get execution ARN from fetch_document_record, then use this tool
37-
for detailed workflow analysis. Follow up with search_cloudwatch_logs for
38-
specific Lambda function errors identified in the failure analysis.
39-
4036
Example usage:
41-
- "Analyze the workflow execution for this document"
42-
- "What went wrong in the Step Function execution?"
43-
- "Show me the workflow timeline and failure point"
44-
- "Why did the document processing workflow fail?"
45-
- "Trace the execution flow and identify issues"
37+
- "Analyze the workflow execution for document report.pdf"
38+
- "What went wrong in the Step Function execution for lending_package.pdf?"
39+
- "Show me the workflow timeline and failure point for document ABC123"
40+
- "Why did the document processing workflow fail for my_document.pdf?"
41+
- "Trace the execution flow and identify issues for this document"
4642
4743
Args:
48-
execution_arn: Step Function execution ARN (get from document record's WorkflowExecutionArn or ExecutionArn field)
44+
document_id: Document filename/S3 object key (e.g., "report.pdf", "lending_package.pdf")
4945
5046
Returns:
5147
Dict with keys:
@@ -56,12 +52,24 @@ def analyze_workflow_execution(execution_arn: str) -> Dict[str, Any]:
5652
- recommendations (list): Actionable next steps for investigation
5753
"""
5854
try:
55+
if not document_id:
56+
return _build_response(
57+
execution_status=None,
58+
analysis_summary="No document ID provided",
59+
recommendations=[
60+
"Use search_cloudwatch_logs or fetch_recent_records for general troubleshooting"
61+
],
62+
)
63+
64+
# Get execution ARN from document record
65+
execution_arn = _get_execution_arn_from_document(document_id)
5966
if not execution_arn:
6067
return _build_response(
61-
execution_status="ERROR",
62-
analysis_summary="No execution ARN provided",
68+
execution_status=None,
69+
analysis_summary=f"No execution ARN found for document {document_id}",
6370
recommendations=[
64-
"Use search_cloudwatch_logs for detailed error information"
71+
"Use search_cloudwatch_logs for detailed error information",
72+
"Verify document exists using fetch_document_record",
6573
],
6674
)
6775

@@ -93,9 +101,11 @@ def analyze_workflow_execution(execution_arn: str) -> Dict[str, Any]:
93101
)
94102

95103
except Exception as e:
96-
logger.error(f"Error analyzing Step Function execution {execution_arn}: {e}")
104+
logger.error(
105+
f"Error analyzing workflow execution for document {document_id}: {e}"
106+
)
97107
return _build_response(
98-
execution_status="ERROR",
108+
execution_status=None,
99109
analysis_summary=f"Failed to analyze workflow execution: {str(e)}",
100110
recommendations=[
101111
"Use search_cloudwatch_logs for detailed error information"
@@ -170,7 +180,7 @@ def _generate_recommendations(timeline_analysis: Dict[str, Any]) -> List[str]:
170180

171181

172182
def _build_response(
173-
execution_status: str,
183+
execution_status: Optional[str],
174184
duration_seconds: Optional[float] = None,
175185
timeline_analysis: Optional[Dict[str, Any]] = None,
176186
analysis_summary: str = "",
@@ -253,6 +263,37 @@ def _extract_failure_details(event: Dict[str, Any]) -> Optional[Dict[str, Any]]:
253263
return details
254264

255265

266+
def _get_execution_arn_from_document(document_id: str) -> Optional[str]:
267+
"""
268+
Get execution ARN from document record using fetch_document_record.
269+
"""
270+
from .dynamodb_tool import fetch_document_record
271+
272+
try:
273+
doc_response = fetch_document_record(document_id)
274+
275+
if not doc_response.get("document_found"):
276+
logger.warning(f"Document {document_id} not found in tracking table")
277+
return None
278+
279+
document = doc_response.get("document", {})
280+
execution_arn = document.get("WorkflowExecutionArn") or document.get(
281+
"ExecutionArn"
282+
)
283+
284+
if not execution_arn:
285+
logger.warning(
286+
f"No execution ARN found in document record for {document_id}"
287+
)
288+
return None
289+
290+
return execution_arn
291+
292+
except Exception as e:
293+
logger.error(f"Error retrieving execution ARN for document {document_id}: {e}")
294+
return None
295+
296+
256297
def _analyze_execution_timeline(events: List[Dict[str, Any]]) -> Dict[str, Any]:
257298
"""
258299
Analyze Step Function execution timeline to identify failure patterns and state transitions.

0 commit comments

Comments
 (0)