From ded8d1ca93c43142214be1067ba03cba1564f577 Mon Sep 17 00:00:00 2001 From: leo Date: Sat, 6 Dec 2025 10:16:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E7=BB=91=E5=AE=9A=E7=9A=84agent=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/live-agent-api/api/v1/devices.py | 15 ++++++++++ main/live-agent-api/repositories/device.py | 3 -- main/live-agent-api/schemas/device.py | 6 ++++ .../live-agent-api/services/device_service.py | 30 ++++++++++++++++++- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/main/live-agent-api/api/v1/devices.py b/main/live-agent-api/api/v1/devices.py index ad20c6c18..b0f24c0bd 100644 --- a/main/live-agent-api/api/v1/devices.py +++ b/main/live-agent-api/api/v1/devices.py @@ -105,3 +105,18 @@ async def remove_agent_from_device( return success_response(data=device.model_dump()) +@router.get("/{device_id}/agents", summary="Get device's bound agents") +async def get_device_bound_agents( + device_id: str, + current_user_id: str = Depends(get_current_user_id), + db: AsyncSession = Depends(get_db) +): + """Get all agents bound to a specific device""" + result = await device_service.get_device_bound_agents( + db=db, + owner_id=current_user_id, + device_id=device_id + ) + return success_response(data=result.model_dump()) + + diff --git a/main/live-agent-api/repositories/device.py b/main/live-agent-api/repositories/device.py index 2e61aebc4..113f2fe9a 100644 --- a/main/live-agent-api/repositories/device.py +++ b/main/live-agent-api/repositories/device.py @@ -18,9 +18,6 @@ class DeviceModel(Base): # Device unique identifier (external) device_id: Mapped[str] = mapped_column(String(50), unique=True, nullable=False) - # Device serial number - sn: Mapped[str] = mapped_column(String(100), unique=True, nullable=False) - # Owner user ID (null if unbound) owner_id: Mapped[str | None] = mapped_column( String(50), diff --git a/main/live-agent-api/schemas/device.py b/main/live-agent-api/schemas/device.py index 6f0c32c96..643e4697c 100644 --- a/main/live-agent-api/schemas/device.py +++ b/main/live-agent-api/schemas/device.py @@ -52,3 +52,9 @@ class DeviceListResponse(BaseModel): """Device list response""" devices: List[DeviceWithBindingsResponse] + +class DeviceBoundAgentsResponse(BaseModel): + """Response for device's bound agents""" + device_id: str + agents: List[AgentBindingResponse] = [] + diff --git a/main/live-agent-api/services/device_service.py b/main/live-agent-api/services/device_service.py index 9ef1d2248..8c4260491 100644 --- a/main/live-agent-api/services/device_service.py +++ b/main/live-agent-api/services/device_service.py @@ -8,7 +8,8 @@ from schemas.device import ( DeviceResponse, DeviceWithBindingsResponse, DeviceListResponse, - AgentBindingResponse + AgentBindingResponse, + DeviceBoundAgentsResponse ) @@ -140,6 +141,33 @@ class DeviceService: return await self._build_device_with_bindings(db, device) + async def get_device_bound_agents( + self, + db: AsyncSession, + owner_id: str, + device_id: str + ) -> DeviceBoundAgentsResponse: + """Get all agents bound to a 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") + + bindings = await AgentDeviceBinding.get_bindings_by_device(db, device_id) + + return DeviceBoundAgentsResponse( + device_id=device_id, + agents=[ + AgentBindingResponse( + agent_id=b.agent_id, + is_default=b.is_default, + created_at=b.created_at + ) + for b in bindings + ] + ) + async def _build_device_with_bindings( self, db: AsyncSession,