feat: add endpoint to delete board agents and implement related service logic
This commit is contained in:
@@ -550,6 +550,23 @@ async def update_agent_soul(
|
||||
return OkResponse()
|
||||
|
||||
|
||||
@router.delete("/boards/{board_id}/agents/{agent_id}", response_model=OkResponse)
|
||||
async def delete_board_agent(
|
||||
agent_id: str,
|
||||
board: Board = BOARD_DEP,
|
||||
session: AsyncSession = SESSION_DEP,
|
||||
agent_ctx: AgentAuthContext = AGENT_CTX_DEP,
|
||||
) -> OkResponse:
|
||||
"""Delete a board agent as the board lead."""
|
||||
_guard_board_access(agent_ctx, board)
|
||||
_require_board_lead(agent_ctx)
|
||||
service = AgentLifecycleService(session)
|
||||
return await service.delete_agent_as_lead(
|
||||
agent_id=agent_id,
|
||||
actor_agent=agent_ctx.agent,
|
||||
)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/boards/{board_id}/gateway/main/ask-user",
|
||||
response_model=GatewayMainAskUserResponse,
|
||||
|
||||
@@ -1658,7 +1658,38 @@ class AgentLifecycleService(OpenClawDBService):
|
||||
if agent is None:
|
||||
return OkResponse()
|
||||
await self.require_agent_access(agent=agent, ctx=ctx, write=True)
|
||||
return await self._delete_agent_record(agent=agent)
|
||||
|
||||
async def delete_agent_as_lead(
|
||||
self,
|
||||
*,
|
||||
agent_id: str,
|
||||
actor_agent: Agent,
|
||||
) -> OkResponse:
|
||||
"""Delete a board-scoped agent as the board lead."""
|
||||
self.logger.log(TRACE_LEVEL, "agent.delete.lead.start agent_id=%s", agent_id)
|
||||
lead = OpenClawAuthorizationPolicy.require_board_lead_actor(
|
||||
actor_agent=actor_agent,
|
||||
detail="Only board leads can delete agents",
|
||||
)
|
||||
agent = await Agent.objects.by_id(agent_id).first(self.session)
|
||||
if agent is None:
|
||||
return OkResponse()
|
||||
if agent.board_id is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Board leads cannot delete gateway main agents",
|
||||
)
|
||||
board = await self.require_board(lead.board_id)
|
||||
OpenClawAuthorizationPolicy.require_board_agent_target(target=agent, board=board)
|
||||
if agent.is_board_lead:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Board leads cannot delete lead agents",
|
||||
)
|
||||
return await self._delete_agent_record(agent=agent)
|
||||
|
||||
async def _delete_agent_record(self, *, agent: Agent) -> OkResponse:
|
||||
gateway: Gateway | None = None
|
||||
client_config: GatewayClientConfig | None = None
|
||||
workspace_path: str | None = None
|
||||
@@ -1772,5 +1803,5 @@ class AgentLifecycleService(OpenClawDBService):
|
||||
)
|
||||
except (OSError, OpenClawGatewayError, ValueError):
|
||||
pass
|
||||
self.logger.info("agent.delete.success agent_id=%s", agent_id)
|
||||
self.logger.info("agent.delete.success agent_id=%s", agent.id)
|
||||
return OkResponse()
|
||||
|
||||
Reference in New Issue
Block a user