From a5de5f6d437d73ab57ec6699ad0746035ae4d7d6 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 9 Dec 2025 17:01:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=AE=BE=E7=BD=AEdev?= =?UTF-8?q?ice=E7=9A=84=E9=BB=98=E8=AE=A4agent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/live-agent-api/api/v1/devices.py | 21 ++++++++++++++++++ .../live-agent-api/services/device_service.py | 22 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/main/live-agent-api/api/v1/devices.py b/main/live-agent-api/api/v1/devices.py index b0f24c0bd..870ed6474 100644 --- a/main/live-agent-api/api/v1/devices.py +++ b/main/live-agent-api/api/v1/devices.py @@ -120,3 +120,24 @@ async def get_device_bound_agents( 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()) + + diff --git a/main/live-agent-api/services/device_service.py b/main/live-agent-api/services/device_service.py index 8c4260491..237a55dc7 100644 --- a/main/live-agent-api/services/device_service.py +++ b/main/live-agent-api/services/device_service.py @@ -141,6 +141,28 @@ class DeviceService: 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( self, db: AsyncSession,