feat: 增加设置device的默认agent

This commit is contained in:
leo 2025-12-09 17:01:46 +08:00
parent dd24d445c1
commit a5de5f6d43
2 changed files with 43 additions and 0 deletions

View file

@ -120,3 +120,24 @@ async def get_device_bound_agents(
return success_response(data=result.model_dump()) return success_response(data=result.model_dump())
@router.put("/{device_id}/default-agent", summary="Set default agent for device")
async def set_default_agent(
device_id: str,
request: DeviceAddAgentRequest,
current_user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db)
):
"""
Set the default agent for this device.
The agent must already be bound to the device.
"""
device = await device_service.set_default_agent(
db=db,
owner_id=current_user_id,
device_id=device_id,
agent_id=request.agent_id
)
return success_response(data=device.model_dump())

View file

@ -141,6 +141,28 @@ class DeviceService:
return await self._build_device_with_bindings(db, device) return await self._build_device_with_bindings(db, device)
async def set_default_agent(
self,
db: AsyncSession,
owner_id: str,
device_id: str,
agent_id: str
) -> DeviceWithBindingsResponse:
"""Set an agent as default for device"""
device = await Device.get_by_id(db, device_id)
if not device:
raise NotFoundException("Device not found")
if device.owner_id != owner_id:
raise BadRequestException("Device does not belong to you")
# Verify binding exists
binding = await AgentDeviceBinding.get_binding(db, device_id, agent_id)
if not binding:
raise NotFoundException("Agent is not bound to this device")
await AgentDeviceBinding.set_default(db, device_id, agent_id)
return await self._build_device_with_bindings(db, device)
async def get_device_bound_agents( async def get_device_bound_agents(
self, self,
db: AsyncSession, db: AsyncSession,