11import json
22import os
3+ import re
34
45import boto3
56import botocore
@@ -35,7 +36,7 @@ def eval(tf_plan_json):
3536 List the resources that will be created, modified or deleted in the following terraform plan using the following rules:
3637 1. Think step by step using the "thinking" json field
3738 2. For AMI changes, include the old and new AMI ID
38- 3. Use the following schema
39+ 3. Use the following schema. Skip the preamble:
3940 <schema>
4041 {
4142 "$id": "https://example.com/arrays.schema.json",
@@ -85,9 +86,11 @@ def eval(tf_plan_json):
8586 bedrock_client , model_id , messages , system_text
8687 )
8788
88- analysis_response_text = json .loads (analysis_response ["content" ][0 ]["text" ])[
89- "resources"
90- ]
89+ logger .debug ("Analysis response: {}" .format (analysis_response ))
90+
91+ analysis_response_text = clean_response (analysis_response ["content" ][0 ]["text" ])["resources" ]
92+
93+ logger .debug ("Analysis response Text: {}" .format (analysis_response_text ))
9194
9295 #####################################################################
9396 ######## Secondly, evaluate AMIs per analysis ########
@@ -150,9 +153,11 @@ def eval(tf_plan_json):
150153 release_details = GetECSAmisReleases ().execute (
151154 tool ["input" ]["image_ids" ]
152155 )
156+ release_details_info = release_details if release_details else "No release notes were found the ami."
157+
153158 tool_result = {
154159 "toolUseId" : tool ["toolUseId" ],
155- "content" : [{"json" : {"release_detail" : release_details }}],
160+ "content" : [{"json" : {"release_detail" : release_details_info }}],
156161 }
157162
158163 tool_result_message = {
@@ -257,4 +262,11 @@ def guardrail_inspection(input_text, input_mode = 'OUTPUT'):
257262 return True , "No Guardrail action required"
258263
259264 else :
260- return True , "Guardrail inspection skipped"
265+ return True , "Guardrail inspection skipped"
266+
267+ def clean_response (json_str ):
268+ # Remove any tags in the format <tag> or </tag>
269+ cleaned_str = re .sub (r'<\/?[\w\s]+>' , '' , json_str )
270+ last_brace_index = cleaned_str .rfind ('}' )
271+ cleaned_str = cleaned_str [:last_brace_index + 1 ]
272+ return json .loads (cleaned_str )
0 commit comments