private GenerateContentResponse autoRespond(GenerateContentResponse originalResponse)
      throws IOException {
    // Return the original response if there is no AFC responder or no function calls in the
    // response.
    if (!automaticFunctionCallingResponder.isPresent()) {
      return originalResponse;
    }
    ImmutableList<FunctionCall> functionCalls = getFunctionCalls(originalResponse);
    if (functionCalls.isEmpty()) {
      return originalResponse;
    }

    // Each time we call the `autoRespond`, we add 2 contents(user's functionResponse and model's
    // functionCall) to the history and update the previousHistorySize during
    // `checkLastResponseAndEditHistory`. But we shouldn't update the previousHistorySize because we
    // will revert the whole history if any intermediate step fails. So we offset the
    // previousHistorySize by 2 here.
    setPreviousHistorySize(getPreviousHistorySize() - 2);
    GenerateContentResponse response;
    try {
      // Let the responder generate the response content and send it to the model.
      Content autoRespondedContent =
          automaticFunctionCallingResponder.get().getContentFromFunctionCalls(functionCalls);
      response = sendMessage(autoRespondedContent);
    } finally {
      // Reset the responder whether it succeeds or fails.
      automaticFunctionCallingResponder.get().resetRemainingFunctionCalls();
    }
    return response;
  }
Automatically responds to the model if there is an AutomaticFunctionCallingResponder and
model's response contains function calls.
java-vertexai/google-cloud-vertexai/src/main/java/com/google/cloud/vertexai/generativeai/ChatSession.java