@@ -703,64 +703,101 @@ def test_save_assistant_message(self, basic_durable_agent):
703703 assert entry .last_message .role == "assistant"
704704
705705 def test_get_last_message_from_state (self , basic_durable_agent ):
706- """Test _get_last_message_from_state helper method."""
706+ """Test accessing last_message from instance state."""
707+ from datetime import datetime , timezone
708+
707709 instance_id = "test-instance-123"
708710
709- # Set up instance with last_message
710- basic_durable_agent .state ["instances" ][instance_id ] = {
711- "input" : "Test task" ,
712- "source" : "test_source" ,
713- "triggering_workflow_instance_id" : None ,
714- "workflow_instance_id" : instance_id ,
715- "workflow_name" : "AgenticWorkflow" ,
716- "status" : "RUNNING" ,
717- "messages" : [],
718- "tool_history" : [],
719- "end_time" : None ,
720- "trace_context" : None ,
721- "last_message" : AgentWorkflowMessage (
722- role = "assistant" , content = "Last message"
723- ).model_dump (mode = "json" ),
724- }
711+ # Set up instance with last_message using AgentWorkflowEntry
712+ if not hasattr (basic_durable_agent ._state_model , 'instances' ):
713+ basic_durable_agent ._state_model .instances = {}
714+
715+ last_msg = AgentWorkflowMessage (role = "assistant" , content = "Last message" )
716+ basic_durable_agent ._state_model .instances [instance_id ] = AgentWorkflowEntry (
717+ input_value = "Test task" ,
718+ source = "test_source" ,
719+ triggering_workflow_instance_id = None ,
720+ workflow_instance_id = instance_id ,
721+ workflow_name = "AgenticWorkflow" ,
722+ status = "RUNNING" ,
723+ messages = [],
724+ tool_history = [],
725+ end_time = None ,
726+ start_time = datetime .now (timezone .utc ),
727+ last_message = last_msg ,
728+ )
725729
726- result = basic_durable_agent ._get_last_message_from_state (instance_id )
727- assert result ["role" ] == "assistant"
728- assert result ["content" ] == "Last message"
730+ # Access last_message directly from the entry
731+ entry = basic_durable_agent ._state_model .instances .get (instance_id )
732+ assert entry is not None
733+ assert entry .last_message .role == "assistant"
734+ assert entry .last_message .content == "Last message"
729735
730736 # Test with non-existent instance
731- result = basic_durable_agent ._get_last_message_from_state ("non-existent" )
737+ result = basic_durable_agent ._state_model . instances . get ("non-existent" )
732738 assert result is None
733739
734- def test_create_tool_message_objects (self , basic_durable_agent ):
735- """Test _create_tool_message_objects helper method."""
736- tool_result = {
737- "tool_call_id" : "call_123" ,
738- "tool_name" : "test_tool" ,
739- "tool_args" : {"arg1" : "value1" },
740- "execution_result" : "tool_result" ,
740+ @pytest .mark .asyncio
741+ async def test_create_tool_message_objects (self , basic_durable_agent ):
742+ """Test that tool message objects are created correctly (via run_tool activity)."""
743+ from datetime import datetime , timezone
744+
745+ instance_id = "test-instance-123"
746+ tool_call = {
747+ "id" : "call_123" ,
748+ "function" : {"name" : "test_tool" , "arguments" : '{"arg1": "value1"}' },
741749 }
742750
743- (
744- tool_msg ,
745- agent_msg ,
746- tool_history_entry ,
747- ) = basic_durable_agent ._create_tool_message_objects (tool_result )
748-
749- # Verify tool message
750- assert tool_msg .tool_call_id == "call_123"
751- assert tool_msg .name == "test_tool"
752- assert tool_msg .content == "tool_result"
753-
754- # Verify agent message (AgentWorkflowMessage)
755- assert agent_msg .role == "tool"
756- assert agent_msg .tool_call_id == "call_123"
757- assert agent_msg .content == "tool_result"
758-
759- # Verify tool history entry (ToolExecutionRecord)
760- assert tool_history_entry .tool_call_id == "call_123"
761- assert tool_history_entry .tool_name == "test_tool"
762- assert tool_history_entry .tool_args == {"arg1" : "value1" }
763- assert tool_history_entry .execution_result == "tool_result"
751+ # Set up instance
752+ if not hasattr (basic_durable_agent ._state_model , 'instances' ):
753+ basic_durable_agent ._state_model .instances = {}
754+
755+ basic_durable_agent ._state_model .instances [instance_id ] = AgentWorkflowEntry (
756+ input_value = "Test task" ,
757+ source = "test_source" ,
758+ triggering_workflow_instance_id = None ,
759+ workflow_instance_id = instance_id ,
760+ workflow_name = "AgenticWorkflow" ,
761+ status = "RUNNING" ,
762+ messages = [],
763+ tool_history = [],
764+ end_time = None ,
765+ start_time = datetime .now (timezone .utc ),
766+ )
767+
768+ # Mock tool executor
769+ with patch .object (type (basic_durable_agent .tool_executor ), "run_tool" , new_callable = AsyncMock ) as mock_run :
770+ mock_run .return_value = "tool_result"
771+
772+ mock_ctx = Mock ()
773+
774+ with patch .object (basic_durable_agent , 'save_state' ):
775+ result = await basic_durable_agent .run_tool (
776+ mock_ctx ,
777+ {
778+ "tool_call" : tool_call ,
779+ "instance_id" : instance_id ,
780+ "time" : datetime .now (timezone .utc ).isoformat (),
781+ "order" : 1
782+ }
783+ )
784+
785+ # Verify the tool result structure
786+ assert result ["tool_call_id" ] == "call_123"
787+ assert result ["tool_name" ] == "test_tool"
788+ assert result ["execution_result" ] == "tool_result"
789+
790+ # Verify messages and history were added to instance
791+ entry = basic_durable_agent ._state_model .instances [instance_id ]
792+ assert len (entry .messages ) == 1
793+ assert entry .messages [0 ].role == "tool"
794+ assert entry .messages [0 ].id == "call_123" # AgentWorkflowMessage uses 'id' not 'tool_call_id'
795+ assert entry .messages [0 ].name == "test_tool"
796+
797+ assert len (entry .tool_history ) == 1
798+ assert entry .tool_history [0 ].tool_call_id == "call_123"
799+ assert entry .tool_history [0 ].tool_name == "test_tool"
800+ assert entry .tool_history [0 ].execution_result == "tool_result"
764801
765802 def test_append_tool_message_to_instance (self , basic_durable_agent ):
766803 """Test _append_tool_message_to_instance helper method."""
0 commit comments