Step 1: Simplify the numerator.
We know that
\[
\sin\left(x-\frac{\pi}{4}\right)
=
\frac{\sin x-\cos x}{\sqrt2}.
\]
So,
\[
\int \frac{\sin\left(x-\frac{\pi}{4}\right)}{2+\sin 2x}\,dx
=
\frac{1}{\sqrt2}
\int \frac{\sin x-\cos x}{2+\sin 2x}\,dx.
\]
Step 2: Simplify the denominator.
Since
\[
\sin 2x=2\sin x\cos x,
\]
we have
\[
2+\sin 2x=2+2\sin x\cos x.
\]
Also,
\[
(\sin x+\cos x)^2=1+2\sin x\cos x.
\]
Therefore,
\[
2+\sin 2x=1+(\sin x+\cos x)^2.
\]
Step 3: Use substitution.
Let
\[
u=\sin x+\cos x.
\]
Then,
\[
\frac{du}{dx}=\cos x-\sin x.
\]
So,
\[
du=(\cos x-\sin x)\,dx.
\]
Hence,
\[
(\sin x-\cos x)\,dx=-du.
\]
Step 4: Transform the integral.
\[
\frac{1}{\sqrt2}
\int \frac{\sin x-\cos x}{1+(\sin x+\cos x)^2}\,dx
=
-\frac{1}{\sqrt2}
\int \frac{du}{1+u^2}.
\]
\[
=
-\frac{1}{\sqrt2}\tan^{-1}u+C.
\]
Substituting back,
\[
=
-\frac{1}{\sqrt2}\tan^{-1}(\sin x+\cos x)+C.
\]
Step 5: Identify \(f(x)\).
Comparing with
\[
-\frac{1}{\sqrt2}\tan^{-1}(f(x))+C,
\]
we get
\[
f(x)=\sin x+\cos x.
\]
Now,
\[
\sqrt2\cos\left(x-\frac{\pi}{4}\right)
=
\sqrt2\left(\frac{\cos x+\sin x}{\sqrt2}\right)
=
\sin x+\cos x.
\]
Thus,
\[
f(x)=\sqrt2\cos\left(x-\frac{\pi}{4}\right).
\]
Step 6: Final conclusion.
Therefore,
\[
\boxed{\sqrt2\cos\left(x-\frac{\pi}{4}\right)}
\]