Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(AutoMod): add ``moderation id`` to moderation message (#10728)

Bently5b12e02c4eae
diff --git a/autogpt_platform/backend/backend/server/v2/AutoMod/manager.py b/autogpt_platform/backend/backend/server/v2/AutoMod/manager.py
index 4030b6563f..540c7489c8 100644
--- a/autogpt_platform/backend/backend/server/v2/AutoMod/manager.py
+++ b/autogpt_platform/backend/backend/server/v2/AutoMod/manager.py
@@ -83,7 +83,7 @@ class AutoModManager:
f"Moderating inputs for graph execution {graph_exec.graph_exec_id}"
)
try:
- moderation_passed = await self._moderate_content(
+ moderation_passed, content_id = await self._moderate_content(
content,
{
"user_id": graph_exec.user_id,
@@ -107,6 +107,7 @@ class AutoModManager:
user_id=graph_exec.user_id,
graph_exec_id=graph_exec.graph_exec_id,
moderation_type="input",
+ content_id=content_id,
)
return None
@@ -167,7 +168,7 @@ class AutoModManager:
# Run moderation
logger.warning(f"Moderating outputs for graph execution {graph_exec_id}")
try:
- moderation_passed = await self._moderate_content(
+ moderation_passed, content_id = await self._moderate_content(
content,
{
"user_id": user_id,
@@ -189,6 +190,7 @@ class AutoModManager:
user_id=user_id,
graph_exec_id=graph_exec_id,
moderation_type="output",
+ content_id=content_id,
)
return None
@@ -275,12 +277,15 @@ class AutoModManager:
]
)
- async def _moderate_content(self, content: str, metadata: dict[str, Any]) -> bool:
+ async def _moderate_content(
+ self, content: str, metadata: dict[str, Any]
+ ) -> tuple[bool, str | None]:
"""Moderate content using AutoMod API
Returns:
- True: Content approved or timeout occurred
- False: Content rejected by moderation
+ Tuple of (approval_status, content_id)
+ - approval_status: True if approved or timeout occurred, False if rejected
+ - content_id: Reference ID from moderation API, or None if not available
Raises:
asyncio.TimeoutError: When moderation times out (should be bypassed)
@@ -298,12 +303,12 @@ class AutoModManager:
logger.debug(
f"Content approved for {metadata.get('graph_exec_id', 'unknown')}"
)
- return True
+ return True, response.content_id
else:
reasons = [r.reason for r in response.moderation_results if r.reason]
error_msg = f"Content rejected by AutoMod: {'; '.join(reasons)}"
logger.warning(f"Content rejected: {error_msg}")
- return False
+ return False, response.content_id
except asyncio.TimeoutError:
# Re-raise timeout to be handled by calling methods
@@ -313,7 +318,7 @@ class AutoModManager:
raise
except Exception as e:
logger.error(f"AutoMod moderation error: {e}")
- return self.config.fail_open
+ return self.config.fail_open, None
async def _make_request(self, request_data: AutoModRequest) -> AutoModResponse:
"""Make HTTP request to AutoMod API using the standard request utility"""
diff --git a/autogpt_platform/backend/backend/server/v2/AutoMod/models.py b/autogpt_platform/backend/backend/server/v2/AutoMod/models.py
index 4ccb2c1bd9..23bd9fe87f 100644
--- a/autogpt_platform/backend/backend/server/v2/AutoMod/models.py
+++ b/autogpt_platform/backend/backend/server/v2/AutoMod/models.py
@@ -26,6 +26,9 @@ class AutoModResponse(BaseModel):
"""Response model for AutoMod API"""
success: bool = Field(..., description="Whether the request was successful")
+ content_id: str = Field(
+ ..., description="Unique reference ID for this moderation request"
+ )
status: str = Field(
..., description="Overall status: 'approved', 'rejected', 'flagged', 'pending'"
)
diff --git a/autogpt_platform/backend/backend/util/exceptions.py b/autogpt_platform/backend/backend/util/exceptions.py
index 8888650a82..3a1efa0e5d 100644
--- a/autogpt_platform/backend/backend/util/exceptions.py
+++ b/autogpt_platform/backend/backend/util/exceptions.py
@@ -40,6 +40,7 @@ class ModerationError(ValueError):
message: str
graph_exec_id: str
moderation_type: str
+ content_id: str | None
def __init__(
self,
@@ -47,16 +48,20 @@ class ModerationError(ValueError):
user_id: str,
graph_exec_id: str,
moderation_type: str = "content",
+ content_id: str | None = None,
):
super().__init__(message)
- self.args = (message, user_id, graph_exec_id, moderation_type)
+ self.args = (message, user_id, graph_exec_id, moderation_type, content_id)
self.message = message
self.user_id = user_id
self.graph_exec_id = graph_exec_id
self.moderation_type = moderation_type
+ self.content_id = content_id
def __str__(self):
"""Used to display the error message in the frontend, because we str() the error when sending the execution update"""
+ if self.content_id:
+ return f"{self.message} (Moderation ID: {self.content_id})"
return self.message