11from datetime import datetime
22import os
3- from typing import Dict , Any , List , Optional
3+ from typing import Dict , Any , List , Optional , Callable
44from AgentCrew .modules .llm .base import BaseLLMService
55from AgentCrew .modules .llm .message import MessageTransformer
66from AgentCrew .modules .agents .base import BaseAgent , MessageType
@@ -385,7 +385,11 @@ def update_llm_service(self, new_llm_service: BaseLLMService) -> bool:
385385
386386 return True
387387
388- async def process_messages (self , messages : Optional [List [Dict [str , Any ]]] = None ):
388+ async def process_messages (
389+ self ,
390+ messages : Optional [List [Dict [str , Any ]]] = None ,
391+ callback : Optional [Callable ] = None ,
392+ ):
389393 """
390394 Process messages using this agent.
391395
@@ -400,9 +404,9 @@ async def process_messages(self, messages: Optional[List[Dict[str, Any]]] = None
400404 )
401405
402406 assistant_response = ""
403- self . tool_uses = []
404- self . input_tokens_usage = 0
405- self . output_tokens_usage = 0
407+ _tool_uses = []
408+ _input_tokens_usage = 0
409+ _output_tokens_usage = 0
406410 # Ensure the first message is a system message with the agent's prompt
407411 if not messages :
408412 final_messages = list (self .history )
@@ -452,15 +456,23 @@ async def process_messages(self, messages: Optional[List[Dict[str, Any]]] = None
452456 chunk_text ,
453457 thinking_chunk ,
454458 ) = self .llm .process_stream_chunk (
455- chunk , assistant_response , self . tool_uses
459+ chunk , assistant_response , _tool_uses
456460 )
461+ yield (assistant_response , chunk_text , thinking_chunk )
462+
457463 if tool_uses :
458- self . tool_uses = tool_uses
464+ _tool_uses = tool_uses
459465 if chunk_input_tokens > 0 :
460- self . input_tokens_usage = chunk_input_tokens
466+ _input_tokens_usage = chunk_input_tokens
461467 if chunk_output_tokens > 0 :
462- self .output_tokens_usage = chunk_output_tokens
463- yield (assistant_response , chunk_text , thinking_chunk )
468+ _output_tokens_usage = chunk_output_tokens
469+ if callback :
470+ callback (_tool_uses , _input_tokens_usage , _output_tokens_usage )
471+ else :
472+ self .tool_uses = _tool_uses
473+ self .input_tokens_usage = _input_tokens_usage
474+ self .output_tokens_usage = _output_tokens_usage
475+
464476 except GeneratorExit as e :
465477 logger .warning (f"Stream processing interrupted: { e } " )
466478 finally :
0 commit comments