@@ -5313,6 +5313,7 @@ async def aget_tools(
53135313 session : AgentSession ,
53145314 user_id : Optional [str ] = None ,
53155315 knowledge_filters : Optional [Dict [str , Any ]] = None ,
5316+ check_mcp_tools : bool = True ,
53165317 ) -> List [Union [Toolkit , Callable , Function , Dict ]]:
53175318 agent_tools : List [Union [Toolkit , Callable , Function , Dict ]] = []
53185319
@@ -5339,7 +5340,7 @@ async def aget_tools(
53395340 continue
53405341
53415342 # Only add the tool if it successfully connected and built its tools
5342- if not tool .initialized : # type: ignore
5343+ if check_mcp_tools and not tool .initialized : # type: ignore
53435344 continue
53445345
53455346 agent_tools .append (tool )
@@ -5353,7 +5354,9 @@ async def aget_tools(
53535354 agent_tools .append (self ._get_tool_call_history_function (session = session ))
53545355 if self .search_session_history :
53555356 agent_tools .append (
5356- await self ._aget_previous_sessions_messages_function (num_history_sessions = self .num_history_sessions )
5357+ await self ._aget_previous_sessions_messages_function (
5358+ num_history_sessions = self .num_history_sessions , user_id = user_id
5359+ )
53575360 )
53585361
53595362 if self .enable_agentic_memory :
@@ -5427,14 +5430,14 @@ def _determine_tools_for_model(
54275430 if name in _function_names :
54285431 continue
54295432 _function_names .append (name )
5430-
5433+ _func = _func . model_copy ( deep = True )
54315434 _func ._agent = self
54325435 _func .process_entrypoint (strict = strict )
54335436 if strict and _func .strict is None :
54345437 _func .strict = True
54355438 if self .tool_hooks is not None :
54365439 _func .tool_hooks = self .tool_hooks
5437- _functions .append (_func . model_copy ( deep = True ) )
5440+ _functions .append (_func )
54385441 log_debug (f"Added tool { name } from { tool .name } " )
54395442
54405443 # Add instructions from the toolkit
@@ -5446,13 +5449,15 @@ def _determine_tools_for_model(
54465449 continue
54475450 _function_names .append (tool .name )
54485451
5449- tool ._agent = self
54505452 tool .process_entrypoint (strict = strict )
5453+ tool = tool .model_copy (deep = True )
5454+
5455+ tool ._agent = self
54515456 if strict and tool .strict is None :
54525457 tool .strict = True
54535458 if self .tool_hooks is not None :
54545459 tool .tool_hooks = self .tool_hooks
5455- _functions .append (tool . model_copy ( deep = True ) )
5460+ _functions .append (tool )
54565461 log_debug (f"Added tool { tool .name } " )
54575462
54585463 # Add instructions from the Function
@@ -5468,12 +5473,13 @@ def _determine_tools_for_model(
54685473 _function_names .append (function_name )
54695474
54705475 _func = Function .from_callable (tool , strict = strict )
5476+ _func = _func .model_copy (deep = True )
54715477 _func ._agent = self
54725478 if strict :
54735479 _func .strict = True
54745480 if self .tool_hooks is not None :
54755481 _func .tool_hooks = self .tool_hooks
5476- _functions .append (_func . model_copy ( deep = True ) )
5482+ _functions .append (_func )
54775483 log_debug (f"Added tool { _func .name } " )
54785484 except Exception as e :
54795485 log_warning (f"Could not add tool { tool } : { e } " )
@@ -5806,7 +5812,11 @@ def get_run_output(self, run_id: str, session_id: Optional[str] = None) -> Optio
58065812 Returns:
58075813 Optional[RunOutput]: The RunOutput from the database or None if not found.
58085814 """
5809- return cast (RunOutput , get_run_output_util (self , run_id = run_id , session_id = session_id ))
5815+ if not session_id and not self .session_id :
5816+ raise Exception ("No session_id provided" )
5817+
5818+ session_id_to_load = session_id or self .session_id
5819+ return cast (RunOutput , get_run_output_util (self , run_id = run_id , session_id = session_id_to_load ))
58105820
58115821 async def aget_run_output (self , run_id : str , session_id : Optional [str ] = None ) -> Optional [RunOutput ]:
58125822 """
@@ -5818,7 +5828,11 @@ async def aget_run_output(self, run_id: str, session_id: Optional[str] = None) -
58185828 Returns:
58195829 Optional[RunOutput]: The RunOutput from the database or None if not found.
58205830 """
5821- return cast (RunOutput , await aget_run_output_util (self , run_id = run_id , session_id = session_id ))
5831+ if not session_id and not self .session_id :
5832+ raise Exception ("No session_id provided" )
5833+
5834+ session_id_to_load = session_id or self .session_id
5835+ return cast (RunOutput , await aget_run_output_util (self , run_id = run_id , session_id = session_id_to_load ))
58225836
58235837 def get_last_run_output (self , session_id : Optional [str ] = None ) -> Optional [RunOutput ]:
58245838 """
@@ -5830,7 +5844,11 @@ def get_last_run_output(self, session_id: Optional[str] = None) -> Optional[RunO
58305844 Returns:
58315845 Optional[RunOutput]: The last run response from the database or None if not found.
58325846 """
5833- return cast (RunOutput , get_last_run_output_util (self , session_id = session_id ))
5847+ if not session_id and not self .session_id :
5848+ raise Exception ("No session_id provided" )
5849+
5850+ session_id_to_load = session_id or self .session_id
5851+ return cast (RunOutput , get_last_run_output_util (self , session_id = session_id_to_load ))
58345852
58355853 async def aget_last_run_output (self , session_id : Optional [str ] = None ) -> Optional [RunOutput ]:
58365854 """
@@ -5842,7 +5860,11 @@ async def aget_last_run_output(self, session_id: Optional[str] = None) -> Option
58425860 Returns:
58435861 Optional[RunOutput]: The last run response from the database or None if not found.
58445862 """
5845- return cast (RunOutput , await aget_last_run_output_util (self , session_id = session_id ))
5863+ if not session_id and not self .session_id :
5864+ raise Exception ("No session_id provided" )
5865+
5866+ session_id_to_load = session_id or self .session_id
5867+ return cast (RunOutput , await aget_last_run_output_util (self , session_id = session_id_to_load ))
58465868
58475869 def get_session (
58485870 self ,
@@ -9541,12 +9563,14 @@ def get_previous_session_messages() -> str:
95419563
95429564 return get_previous_session_messages
95439565
9544- async def _aget_previous_sessions_messages_function (self , num_history_sessions : Optional [int ] = 2 ) -> Callable :
9566+ async def _aget_previous_sessions_messages_function (
9567+ self , num_history_sessions : Optional [int ] = 2 , user_id : Optional [str ] = None
9568+ ) -> Function :
95459569 """Factory function to create a get_previous_session_messages function.
95469570
95479571 Args:
95489572 num_history_sessions: The last n sessions to be taken from db
9549-
9573+ user_id: The user ID to filter sessions by
95509574 Returns:
95519575 Callable: A function that retrieves messages from previous sessions
95529576 """
@@ -9564,12 +9588,22 @@ async def aget_previous_session_messages() -> str:
95649588 if self .db is None :
95659589 return "Previous session messages not available"
95669590
9567- if isinstance (self .db , AsyncBaseDb ):
9568- selected_sessions = await self .db .get_sessions (
9569- session_type = SessionType .AGENT , limit = num_history_sessions
9591+ if self ._has_async_db ():
9592+ selected_sessions = await self .db .get_sessions ( # type: ignore
9593+ session_type = SessionType .AGENT ,
9594+ limit = num_history_sessions ,
9595+ user_id = user_id ,
9596+ sort_by = "created_at" ,
9597+ sort_order = "desc" ,
95709598 )
95719599 else :
9572- selected_sessions = self .db .get_sessions (session_type = SessionType .AGENT , limit = num_history_sessions )
9600+ selected_sessions = self .db .get_sessions (
9601+ session_type = SessionType .AGENT ,
9602+ limit = num_history_sessions ,
9603+ user_id = user_id ,
9604+ sort_by = "created_at" ,
9605+ sort_order = "desc" ,
9606+ )
95739607
95749608 all_messages = []
95759609 seen_message_pairs = set ()
@@ -9602,7 +9636,7 @@ async def aget_previous_session_messages() -> str:
96029636
96039637 return json .dumps ([msg .to_dict () for msg in all_messages ]) if all_messages else "No history found"
96049638
9605- return aget_previous_session_messages
9639+ return Function . from_callable ( aget_previous_session_messages , name = "get_previous_session_messages" )
96069640
96079641 ###########################################################################
96089642 # Print Response
0 commit comments