@@ -108,6 +108,93 @@ def get_multimodal_message(text_prompt: str, images: list) -> list:
108108 })
109109 return content
110110
111+ def _prepare_task_prompt (task , task_description , context_text = None ):
112+ """
113+ Prepare task prompt with context and memory (DRY helper).
114+ """
115+ # Build task prompt - only use "User Input/Topic" format if there's actual content
116+ if context_text and context_text .strip ():
117+ task_prompt = f"""
118+ User Input/Topic: { context_text }
119+
120+ Task: { task_description }
121+ Expected Output: { task .expected_output }
122+
123+ IMPORTANT: Your response must be about the user's input/topic above. Incorporate it into your task."""
124+ else :
125+ task_prompt = f"""
126+ You need to do the following task: { task_description } .
127+ Expected Output: { task .expected_output } ."""
128+
129+ # Add memory context if available
130+ if task .memory :
131+ try :
132+ memory_context = task .memory .build_context_for_task (task .description )
133+ if memory_context :
134+ # Log detailed memory context for debugging
135+ logger .debug (f"Memory context for task '{ task .description } ': { memory_context } " )
136+ # Include actual memory content without verbose headers (essential for AI agent functionality)
137+ task_prompt += f"\n \n { memory_context } "
138+ except Exception as e :
139+ logger .error (f"Error getting memory context: { e } " )
140+
141+ task_prompt += "\n Please provide only the final result of your work. Do not add any conversation or extra explanation."
142+ return task_prompt
143+
144+ def _execute_with_agent_sync (executor_agent , task_prompt , task , tools , stream = None ):
145+ """
146+ Execute task with agent synchronously (DRY helper).
147+ """
148+ if task .images :
149+ return executor_agent .chat (
150+ get_multimodal_message (task_prompt , task .images ),
151+ tools = tools ,
152+ output_json = task .output_json ,
153+ output_pydantic = task .output_pydantic ,
154+ stream = stream ,
155+ task_name = task .name ,
156+ task_description = task .description ,
157+ task_id = task .id
158+ )
159+ else :
160+ return executor_agent .chat (
161+ task_prompt ,
162+ tools = tools ,
163+ output_json = task .output_json ,
164+ output_pydantic = task .output_pydantic ,
165+ stream = stream ,
166+ task_name = task .name ,
167+ task_description = task .description ,
168+ task_id = task .id
169+ )
170+
171+ async def _execute_with_agent_async (executor_agent , task_prompt , task , tools , stream = None ):
172+ """
173+ Execute task with agent asynchronously (DRY helper).
174+ """
175+ if task .images :
176+ return await executor_agent .achat (
177+ get_multimodal_message (task_prompt , task .images ),
178+ tools = tools ,
179+ output_json = task .output_json ,
180+ output_pydantic = task .output_pydantic ,
181+ stream = stream ,
182+ task_name = task .name ,
183+ task_description = task .description ,
184+ task_id = task .id
185+ )
186+ else :
187+ return await executor_agent .achat (
188+ task_prompt ,
189+ tools = tools ,
190+ output_json = task .output_json ,
191+ output_pydantic = task .output_pydantic ,
192+ stream = stream ,
193+ task_name = task .name ,
194+ task_description = task .description ,
195+ task_id = task .id
196+ )
197+
111198def process_task_context (context_item , verbose = 0 , user_id = None ):
112199 """
113200 Process a single context item for task execution.
@@ -703,59 +790,17 @@ async def aexecute_task(self, task_id):
703790 context_separator = '\n \n '
704791 context_text = context_separator .join (unique_contexts )
705792
706- # Build task prompt - only use "User Input/Topic" format if there's actual content
707- if context_text and context_text .strip ():
708- task_prompt = f"""
709- User Input/Topic: { context_text }
710-
711- Task: { task_description }
712- Expected Output: { task .expected_output }
713-
714- IMPORTANT: Your response must be about the user's input/topic above. Incorporate it into your task."""
715- else :
716- task_prompt = f"""
717- You need to do the following task: { task_description } .
718- Expected Output: { task .expected_output } ."""
719-
720- # Add memory context if available
721- if task .memory :
722- try :
723- memory_context = task .memory .build_context_for_task (task .description )
724- if memory_context :
725- # Log detailed memory context for debugging
726- logger .debug (f"Memory context for task '{ task .description } ': { memory_context } " )
727- # Include actual memory content without verbose headers (essential for AI agent functionality)
728- task_prompt += f"\n \n { memory_context } "
729- except Exception as e :
730- logger .error (f"Error getting memory context: { e } " )
731-
732- task_prompt += "\n Please provide only the final result of your work. Do not add any conversation or extra explanation."
793+ # Build task prompt using DRY helper
794+ task_prompt = _prepare_task_prompt (task , task_description , context_text )
733795
734796 if self .verbose >= 2 :
735797 logger .info (f"Executing task { task_id } : { task_description } using { executor_agent .display_name } " )
736798 logger .debug (f"Starting execution of task { task_id } with prompt:\n { task_prompt } " )
737799
738- if task .images :
739- # Use shared multimodal helper (DRY - defined at module level)
740- agent_output = await executor_agent .achat (
741- get_multimodal_message (task_prompt , task .images ),
742- tools = tools ,
743- output_json = task .output_json ,
744- output_pydantic = task .output_pydantic ,
745- task_name = task .name ,
746- task_description = task .description ,
747- task_id = task .id
748- )
749- else :
750- agent_output = await executor_agent .achat (
751- task_prompt ,
752- tools = tools ,
753- output_json = task .output_json ,
754- output_pydantic = task .output_pydantic ,
755- task_name = task .name ,
756- task_description = task .description ,
757- task_id = task .id
758- )
800+ # Execute with agent using DRY helper (fixes missing stream parameter)
801+ agent_output = await _execute_with_agent_async (
802+ executor_agent , task_prompt , task , tools , stream = self .stream
803+ )
759804
760805 if agent_output :
761806 # Store the response in memory
@@ -1066,60 +1111,17 @@ def execute_task(self, task_id):
10661111 context_separator = '\n \n '
10671112 context_text = context_separator .join (unique_contexts )
10681113
1069- # Build task prompt - only use "User Input/Topic" format if there's actual content
1070- if context_text and context_text .strip ():
1071- task_prompt = f"""
1072- User Input/Topic: { context_text }
1073-
1074- Task: { task_description }
1075- Expected Output: { task .expected_output }
1076-
1077- IMPORTANT: Your response must be about the user's input/topic above. Incorporate it into your task."""
1078- else :
1079- task_prompt = f"""
1080- You need to do the following task: { task_description } .
1081- Expected Output: { task .expected_output } ."""
1082-
1083- # Add memory context if available
1084- if task .memory :
1085- try :
1086- memory_context = task .memory .build_context_for_task (task .description )
1087- if memory_context :
1088- # Log detailed memory context for debugging
1089- logger .debug (f"Memory context for task '{ task .description } ': { memory_context } " )
1090- # Include actual memory content without verbose headers (essential for AI agent functionality)
1091- task_prompt += f"\n \n { memory_context } "
1092- except Exception as e :
1093- logger .error (f"Error getting memory context: { e } " )
1094-
1095- task_prompt += "\n Please provide only the final result of your work. Do not add any conversation or extra explanation."
1114+ # Build task prompt using DRY helper
1115+ task_prompt = _prepare_task_prompt (task , task_description , context_text )
10961116
10971117 if self .verbose >= 2 :
10981118 logger .info (f"Executing task { task_id } : { task .description } using { executor_agent .display_name } " )
10991119 logger .debug (f"Starting execution of task { task_id } with prompt:\n { task_prompt } " )
11001120
1101- if task .images :
1102- # Use shared multimodal helper (DRY - defined at module level)
1103- agent_output = executor_agent .chat (
1104- get_multimodal_message (task_prompt , task .images ),
1105- tools = tools ,
1106- output_json = task .output_json ,
1107- output_pydantic = task .output_pydantic ,
1108- task_name = task .name ,
1109- task_description = task .description ,
1110- task_id = task_id
1111- )
1112- else :
1113- agent_output = executor_agent .chat (
1114- task_prompt ,
1115- tools = tools ,
1116- output_json = task .output_json ,
1117- output_pydantic = task .output_pydantic ,
1118- stream = self .stream ,
1119- task_name = task .name ,
1120- task_description = task .description ,
1121- task_id = task_id
1122- )
1121+ # Execute with agent using DRY helper
1122+ agent_output = _execute_with_agent_sync (
1123+ executor_agent , task_prompt , task , tools , stream = self .stream
1124+ )
11231125
11241126 if agent_output :
11251127 # Store the response in memory
0 commit comments