4. Given $a>\sqrt{2}$, the minimum value of the function $y=(\sin x+a)(\cos x+a)$ is $(\quad)$.
A. $a^{2}-a$
B. $(a-1)^{2}$
C. $\frac{1}{2}\left(a^{2}-1\right)$
D. $\left(a-\frac{\sqrt{2}}{2}\right)^{2}$
4. D.
$y=a^{2}+a(\sin x+\cos x)+\sin x \cos x$, let $\sin x+\cos x=t$, then $t \in[-\sqrt{2}, \sqrt{2}], a>2$. $y=a^{2}+a t+\frac{1}{2}\left(t^{2}-1\right)=\frac{1}{2} t^{2}+a t+a^{2}-1$, the axis of symmetry is $t=-a$, and $-\sqrt{2}>-a$, so when $t=-\sqrt{2}$, $y_{\text {min }}=\left(a-\frac{\sqrt{2}}{2}\right)^{2}$.