SafeAgent: Safeguarding LLM Agents via an Automated Risk Simulator