From 43085f80ac55dbbdff3aa8596d6c91724cc3ee76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B9=85=E6=B0=A2?= Date: Thu, 18 Jun 2026 18:39:27 +0800 Subject: [PATCH] Refactor ModelClient to prioritize ModelService over ModelProxy for resource retrieval. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ib937e5c1b8bb79d322e99db85b7c7e8497a40d27 Signed-off-by: 久氢 --- agentrun/model/__client_async_template.py | 17 +++++----- agentrun/model/client.py | 34 ++++++++++---------- tests/unittests/model/test_client.py | 39 +++++++++++------------ 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/agentrun/model/__client_async_template.py b/agentrun/model/__client_async_template.py index e2e2319..d82049d 100644 --- a/agentrun/model/__client_async_template.py +++ b/agentrun/model/__client_async_template.py @@ -285,24 +285,25 @@ async def get_async( ResourceNotExistError: 模型服务不存在 """ + # 优先查 ModelService,未命中再回退 ModelProxy,避免无谓的 404 error: Optional[HTTPError] = None - if backend_type == BackendType.PROXY or backend_type is None: + if backend_type == BackendType.SERVICE or backend_type is None: try: - result = await self.__control_api.get_model_proxy_async( - model_proxy_name=name, config=config + result = await self.__control_api.get_model_service_async( + model_service_name=name, config=config ) - return ModelProxy.from_inner_object(result) + return ModelService.from_inner_object(result) except HTTPError as e: error = e - if backend_type == BackendType.PROXY and error is not None: + if backend_type == BackendType.SERVICE and error is not None: raise error.to_resource_error("Model", name) from error try: - result = await self.__control_api.get_model_service_async( - model_service_name=name, config=config + result = await self.__control_api.get_model_proxy_async( + model_proxy_name=name, config=config ) - return ModelService.from_inner_object(result) + return ModelProxy.from_inner_object(result) except HTTPError as e: raise e.to_resource_error("Model", name) from e diff --git a/agentrun/model/client.py b/agentrun/model/client.py index f566ccf..449794f 100644 --- a/agentrun/model/client.py +++ b/agentrun/model/client.py @@ -512,24 +512,25 @@ async def get_async( ResourceNotExistError: 模型服务不存在 """ + # 优先查 ModelService,未命中再回退 ModelProxy,避免无谓的 404 error: Optional[HTTPError] = None - if backend_type == BackendType.PROXY or backend_type is None: + if backend_type == BackendType.SERVICE or backend_type is None: try: - result = await self.__control_api.get_model_proxy_async( - model_proxy_name=name, config=config + result = await self.__control_api.get_model_service_async( + model_service_name=name, config=config ) - return ModelProxy.from_inner_object(result) + return ModelService.from_inner_object(result) except HTTPError as e: error = e - if backend_type == BackendType.PROXY and error is not None: + if backend_type == BackendType.SERVICE and error is not None: raise error.to_resource_error("Model", name) from error try: - result = await self.__control_api.get_model_service_async( - model_service_name=name, config=config + result = await self.__control_api.get_model_proxy_async( + model_proxy_name=name, config=config ) - return ModelService.from_inner_object(result) + return ModelProxy.from_inner_object(result) except HTTPError as e: raise e.to_resource_error("Model", name) from e @@ -552,24 +553,25 @@ def get( ResourceNotExistError: 模型服务不存在 """ + # 优先查 ModelService,未命中再回退 ModelProxy,避免无谓的 404 error: Optional[HTTPError] = None - if backend_type == BackendType.PROXY or backend_type is None: + if backend_type == BackendType.SERVICE or backend_type is None: try: - result = self.__control_api.get_model_proxy( - model_proxy_name=name, config=config + result = self.__control_api.get_model_service( + model_service_name=name, config=config ) - return ModelProxy.from_inner_object(result) + return ModelService.from_inner_object(result) except HTTPError as e: error = e - if backend_type == BackendType.PROXY and error is not None: + if backend_type == BackendType.SERVICE and error is not None: raise error.to_resource_error("Model", name) from error try: - result = self.__control_api.get_model_service( - model_service_name=name, config=config + result = self.__control_api.get_model_proxy( + model_proxy_name=name, config=config ) - return ModelService.from_inner_object(result) + return ModelProxy.from_inner_object(result) except HTTPError as e: raise e.to_resource_error("Model", name) from e diff --git a/tests/unittests/model/test_client.py b/tests/unittests/model/test_client.py index 48453f3..2d2a0f8 100644 --- a/tests/unittests/model/test_client.py +++ b/tests/unittests/model/test_client.py @@ -906,13 +906,14 @@ def test_get_auto_detect(self, mock_control_api_class): mock_control_api_class.return_value = mock_control_api mock_result = MagicMock() - mock_control_api.get_model_proxy.return_value = mock_result + mock_control_api.get_model_service.return_value = mock_result client = ModelClient() result = client.get("test") - # Should try proxy first - mock_control_api.get_model_proxy.assert_called_once() + # Should try service first (ModelService is the primary resource after migration) + mock_control_api.get_model_service.assert_called_once() + mock_control_api.get_model_proxy.assert_not_called() @patch.dict( os.environ, @@ -923,27 +924,25 @@ def test_get_auto_detect(self, mock_control_api_class): }, ) @patch("agentrun.model.client.ModelControlAPI") - def test_get_auto_detect_falls_back_to_service( - self, mock_control_api_class - ): - """Test fallback to service when proxy not found""" + def test_get_auto_detect_falls_back_to_proxy(self, mock_control_api_class): + """Test fallback to proxy when service not found""" mock_control_api = MagicMock() mock_control_api_class.return_value = mock_control_api - # Proxy get fails - mock_control_api.get_model_proxy.side_effect = HTTPError( + # Service get fails + mock_control_api.get_model_service.side_effect = HTTPError( status_code=404, message="Not found" ) - # Service get succeeds + # Proxy get succeeds mock_result = MagicMock() - mock_control_api.get_model_service.return_value = mock_result + mock_control_api.get_model_proxy.return_value = mock_result client = ModelClient() result = client.get("test") - mock_control_api.get_model_proxy.assert_called_once() mock_control_api.get_model_service.assert_called_once() - assert isinstance(result, ModelService) + mock_control_api.get_model_proxy.assert_called_once() + assert isinstance(result, ModelProxy) @patch.dict( os.environ, @@ -1009,26 +1008,26 @@ async def test_get_async_service(self, mock_control_api_class): @patch("agentrun.model.client.ModelControlAPI") @pytest.mark.asyncio async def test_get_async_auto_detect_fallback(self, mock_control_api_class): - """Test fallback to service when proxy not found in async get""" + """Test fallback to proxy when service not found in async get""" mock_control_api = MagicMock() mock_control_api_class.return_value = mock_control_api - # Proxy get fails - mock_control_api.get_model_proxy_async = AsyncMock( + # Service get fails + mock_control_api.get_model_service_async = AsyncMock( side_effect=HTTPError(status_code=404, message="Not found") ) - # Service get succeeds + # Proxy get succeeds mock_result = MagicMock() - mock_control_api.get_model_service_async = AsyncMock( + mock_control_api.get_model_proxy_async = AsyncMock( return_value=mock_result ) client = ModelClient() result = await client.get_async("test") - mock_control_api.get_model_proxy_async.assert_called_once() mock_control_api.get_model_service_async.assert_called_once() - assert isinstance(result, ModelService) + mock_control_api.get_model_proxy_async.assert_called_once() + assert isinstance(result, ModelProxy) @patch.dict( os.environ,