@@ -1368,4 +1368,51 @@ public void testExtractSummaryFromResponse_WithEmptyDataMap() {
13681368 String result = mlChatAgentRunner .extractSummaryFromResponse (response );
13691369 assertEquals (null , result );
13701370 }
1371+
1372+ @ Test
1373+ public void testExtractSummaryFromResponse_ThrowsException_FallbackStrategyUsed () {
1374+ LLMSpec llmSpec = LLMSpec .builder ().modelId ("MODEL_ID" ).parameters (Map .of ("max_iteration" , "1" )).build ();
1375+ MLToolSpec firstToolSpec = MLToolSpec .builder ().name (FIRST_TOOL ).type (FIRST_TOOL ).build ();
1376+ final MLAgent mlAgent = MLAgent
1377+ .builder ()
1378+ .name ("TestAgent" )
1379+ .type (MLAgentType .CONVERSATIONAL .name ())
1380+ .llm (llmSpec )
1381+ .memory (mlMemorySpec )
1382+ .tools (Arrays .asList (firstToolSpec ))
1383+ .build ();
1384+
1385+ Mockito .reset (client );
1386+ Mockito .reset (firstTool );
1387+ when (firstTool .getName ()).thenReturn (FIRST_TOOL );
1388+ when (firstTool .validate (Mockito .anyMap ())).thenReturn (true );
1389+ Mockito .doAnswer (generateToolResponse ("First tool response" )).when (firstTool ).run (Mockito .anyMap (), any ());
1390+
1391+ Mockito .doAnswer (getLLMAnswer (ImmutableMap .of ("thought" , "Analyzing the problem" , "action" , FIRST_TOOL ))).doAnswer (invocation -> {
1392+ ActionListener <Object > listener = invocation .getArgument (2 );
1393+ Map <String , Object > invalidDataMap = new HashMap <>();
1394+ invalidDataMap .put ("output" , new HashMap <>());
1395+ ModelTensor modelTensor = ModelTensor .builder ().dataAsMap (invalidDataMap ).build ();
1396+ ModelTensors modelTensors = ModelTensors .builder ().mlModelTensors (Arrays .asList (modelTensor )).build ();
1397+ ModelTensorOutput mlModelTensorOutput = ModelTensorOutput .builder ().mlModelOutputs (Arrays .asList (modelTensors )).build ();
1398+ MLTaskResponse mlTaskResponse = MLTaskResponse .builder ().output (mlModelTensorOutput ).build ();
1399+ listener .onResponse (mlTaskResponse );
1400+ return null ;
1401+ }).when (client ).execute (any (ActionType .class ), any (ActionRequest .class ), isA (ActionListener .class ));
1402+
1403+ Map <String , String > params = new HashMap <>();
1404+ params .put (MLAgentExecutor .PARENT_INTERACTION_ID , "parent_interaction_id" );
1405+ mlChatAgentRunner .run (mlAgent , params , agentActionListener , null );
1406+
1407+ verify (agentActionListener ).onResponse (objectCaptor .capture ());
1408+ Object capturedResponse = objectCaptor .getValue ();
1409+ assertTrue (capturedResponse instanceof ModelTensorOutput );
1410+
1411+ ModelTensorOutput modelTensorOutput = (ModelTensorOutput ) capturedResponse ;
1412+ List <ModelTensor > agentOutput = modelTensorOutput .getMlModelOutputs ().get (1 ).getMlModelTensors ();
1413+ assertEquals (1 , agentOutput .size ());
1414+
1415+ String response = (String ) agentOutput .get (0 ).getDataAsMap ().get ("response" );
1416+ assertEquals ("Agent reached maximum iterations (1) without completing the task. Last thought: Analyzing the problem" , response );
1417+ }
13711418}
0 commit comments