In experiments utilizing 4th-generation synchrotron radiation and free electron laser (FEL) beamlines, a primary challenge for X-ray optical elements is to achieve and maintain high-intensity focused X-ray beams with near-perfect wavefront quality and high stability. These optical elements inherently demand more stringent specifications than those for other applications because of the shorter wavelength and the ultra-small emittance of the radiation beams from these sources. Coherent photons from diffraction-limited light sources further underscore the necessity for a controlled wavefront. Maintaining a uniform wavefront is crucial for phase-sensitive imaging techniques and for various coherent X-ray scattering experiments, such as tomography, coherent X-ray diffraction imaging, X-ray photon correlation spectroscopy and coherent surface scattering imaging. Therefore, X-ray optics must be manufactured close to ideal mathematical shapes, automatically align and focus beams according to experimental needs, and offer real-time correction to wavefront deformations. At the Advanced Photon Source (APS), we have demonstrated the application of a neural network model to automatically control deformable mirrors and the use of Bayesian optimization with Gaussian processes to align and stabilize focusing optical systems.