@@ -82,6 +82,7 @@ def chat_with_agent(message: str, user_id: str = "default", selected_images: Opt
8282 Returns:
8383 Tuple of (agent_response, generated_image_data)
8484 """
85+ print (f"[AGENT] Starting chat_with_agent - user_id: { user_id } , message: { message [:100 ]} ..." )
8586 agent = get_agent ()
8687
8788 # Prepare the message with context
@@ -101,26 +102,35 @@ def chat_with_agent(message: str, user_id: str = "default", selected_images: Opt
101102 config = {"configurable" : {"thread_id" : user_id }}
102103
103104 # Get response from agent
105+ print (f"[AGENT] Invoking agent with config: { config } " )
104106 response = agent .invoke ({"messages" : [{"role" : "user" , "content" : full_message }]}, config = config )
107+ print (f"[AGENT] Agent response received: { type (response )} " )
105108
106109 # Extract the last message from the agent
107110 agent_response = "I'm sorry, I couldn't process your request. Please try again."
108111 generated_image_data = None
109112
110113 if response and "messages" in response and len (response ["messages" ]) > 0 :
114+ print (f"[AGENT] Found { len (response ['messages' ])} messages in response" )
111115 last_message = response ["messages" ][- 1 ]
116+ print (f"[AGENT] Last message type: { type (last_message )} " )
112117 # Handle both AIMessage objects and dictionaries
113118 if hasattr (last_message , "content" ):
114119 agent_response = last_message .content
115120 elif isinstance (last_message , dict ) and "content" in last_message :
116121 agent_response = last_message ["content" ]
122+ print (f"[AGENT] Extracted agent response: { agent_response [:100 ]} ..." )
117123
118124 # Check if any tools were used (image generation)
125+ print (f"[AGENT] Checking intermediate steps: { response .get ('intermediate_steps' , [])} " )
119126 if "intermediate_steps" in response and response ["intermediate_steps" ]:
120- for step in response ["intermediate_steps" ]:
127+ print (f"[AGENT] Found { len (response ['intermediate_steps' ])} intermediate steps" )
128+ for i , step in enumerate (response ["intermediate_steps" ]):
129+ print (f"[AGENT] Step { i } : { step } " )
121130 if len (step ) >= 2 and "generate_image" in str (step [0 ]):
122131 # Extract image data from the tool result
123132 tool_result = step [1 ]
133+ print (f"[AGENT] Found generate_image tool result: { tool_result } " )
124134
125135 # Try multiple patterns to find the image ID
126136 image_id = None
@@ -143,6 +153,7 @@ def chat_with_agent(message: str, user_id: str = "default", selected_images: Opt
143153 title = title_match .group (1 )
144154
145155 if image_id :
156+ print (f"[AGENT] Found image_id: { image_id } , attempting to get S3 metadata" )
146157 # Get metadata from S3
147158 import boto3
148159
@@ -154,18 +165,23 @@ def chat_with_agent(message: str, user_id: str = "default", selected_images: Opt
154165 )
155166
156167 bucket_name = os .environ .get ("AWS_S3_BUCKET_NAME" )
168+ print (f"[AGENT] Using bucket: { bucket_name } " )
157169 if bucket_name :
158170 try :
159171 # Get metadata from S3
160- metadata_response = s3_client .head_object (Bucket = bucket_name , Key = f"users/{ user_id } /images/{ image_id } " )
172+ s3_key = f"users/{ user_id } /images/{ image_id } "
173+ print (f"[AGENT] Getting metadata for S3 key: { s3_key } " )
174+ metadata_response = s3_client .head_object (Bucket = bucket_name , Key = s3_key )
161175 metadata = metadata_response .get ("Metadata" , {})
176+ print (f"[AGENT] Retrieved metadata: { metadata } " )
162177
163178 # Generate presigned URL
164179 presigned_url = s3_client .generate_presigned_url (
165180 "get_object" ,
166181 Params = {"Bucket" : bucket_name , "Key" : f"users/{ user_id } /images/{ image_id } " },
167182 ExpiresIn = 7200 , # 2 hours
168183 )
184+ print (f"[AGENT] Generated presigned URL: { presigned_url [:50 ]} ..." )
169185
170186 generated_image_data = {
171187 "id" : image_id ,
@@ -175,15 +191,17 @@ def chat_with_agent(message: str, user_id: str = "default", selected_images: Opt
175191 "timestamp" : metadata .get ("uploadedAt" , datetime .now ().isoformat ()),
176192 "type" : "generated" ,
177193 }
194+ print (f"[AGENT] Created generated_image_data: { generated_image_data } " )
178195
179196 except Exception as e :
180- print (f"Error getting S3 metadata: { e } " )
197+ print (f"[AGENT] Error getting S3 metadata: { e } " )
181198 # Don't return image data if we can't get a valid URL
182199 generated_image_data = None
183200 # Add error message to agent response
184201 agent_response += "\n \n ⚠️ Note: I generated the image successfully, \
185202 but there was an issue retrieving it from the database. Please try again."
186203
204+ print (f"[AGENT] Returning response - agent_response length: { len (agent_response )} , generated_image_data: { generated_image_data is not None } " )
187205 return agent_response , generated_image_data
188206
189207
0 commit comments