@@ -493,6 +493,55 @@ def test_retrieve_error_handling(self, mock_build_client):
493493 assert result ["query" ] == "test query"
494494 assert result ["knowledge_base_name" ] == "test-kb"
495495
496+ @patch ("agentrun.knowledgebase.api.data.AgentStorageClient" )
497+ def test_retrieve_falls_back_to_frontend_endpoint (
498+ self , mock_client_class
499+ ):
500+ """测试实例域名检索失败后回退到大前端域名"""
501+ mock_config = MagicMock (spec = Config )
502+ mock_config .get_region_id .return_value = "cn-hangzhou"
503+ mock_config .get_ots_endpoint .return_value = (
504+ "https://test-instance.cn-hangzhou.ots.aliyuncs.com"
505+ )
506+ mock_config .get_access_key_id .return_value = "test-ak"
507+ mock_config .get_access_key_secret .return_value = "test-sk"
508+ mock_config .get_security_token .return_value = "test-sts"
509+
510+ instance_client = MagicMock ()
511+ instance_client .retrieve .side_effect = RuntimeError (
512+ "instance endpoint unavailable"
513+ )
514+ fallback_client = MagicMock ()
515+ fallback_client .retrieve .return_value = {
516+ "code" : "SUCCESS" ,
517+ "data" : {
518+ "retrievalResults" : [
519+ {"content" : "fallback result" , "score" : 0.8 }
520+ ]
521+ },
522+ }
523+ mock_client_class .side_effect = [instance_client , fallback_client ]
524+
525+ with patch .object (Config , "with_configs" , return_value = mock_config ):
526+ api = OTSDataAPI (
527+ "test-kb" ,
528+ provider_settings = OTSProviderSettings (
529+ ots_instance_name = "test-instance"
530+ ),
531+ )
532+ result = api .retrieve ("test query" )
533+
534+ assert "error" not in result
535+ assert result ["data" ][0 ]["content" ] == "fallback result"
536+ assert mock_client_class .call_args_list [0 ].kwargs ["ots_endpoint" ] == (
537+ "https://test-instance.cn-hangzhou.ots.aliyuncs.com"
538+ )
539+ assert mock_client_class .call_args_list [1 ].kwargs ["ots_endpoint" ] == (
540+ "http://ots-cn-hangzhou.aliyuncs.com"
541+ )
542+ instance_client .retrieve .assert_called_once ()
543+ fallback_client .retrieve .assert_called_once ()
544+
496545 @patch (
497546 "agentrun.knowledgebase.api.data.OTSDataAPI._build_agent_storage_client"
498547 )
@@ -512,6 +561,56 @@ async def test_retrieve_async_error_handling(self, mock_build_client):
512561 assert result ["error" ] is True
513562 assert "Failed to retrieve" in result ["data" ]
514563
564+ @patch ("agentrun.knowledgebase.api.data.AgentStorageClient" )
565+ @pytest .mark .asyncio
566+ async def test_retrieve_async_falls_back_to_frontend_endpoint (
567+ self , mock_client_class
568+ ):
569+ """测试异步实例域名检索失败后回退到大前端域名"""
570+ mock_config = MagicMock (spec = Config )
571+ mock_config .get_region_id .return_value = "cn-hangzhou"
572+ mock_config .get_ots_endpoint .return_value = (
573+ "https://test-instance.cn-hangzhou.ots.aliyuncs.com"
574+ )
575+ mock_config .get_access_key_id .return_value = "test-ak"
576+ mock_config .get_access_key_secret .return_value = "test-sk"
577+ mock_config .get_security_token .return_value = "test-sts"
578+
579+ instance_client = MagicMock ()
580+ instance_client .retrieve .side_effect = RuntimeError (
581+ "instance endpoint unavailable"
582+ )
583+ fallback_client = MagicMock ()
584+ fallback_client .retrieve .return_value = {
585+ "code" : "SUCCESS" ,
586+ "data" : {
587+ "retrievalResults" : [
588+ {"content" : "async fallback result" , "score" : 0.8 }
589+ ]
590+ },
591+ }
592+ mock_client_class .side_effect = [instance_client , fallback_client ]
593+
594+ with patch .object (Config , "with_configs" , return_value = mock_config ):
595+ api = OTSDataAPI (
596+ "test-kb" ,
597+ provider_settings = OTSProviderSettings (
598+ ots_instance_name = "test-instance"
599+ ),
600+ )
601+ result = await api .retrieve_async ("test query" )
602+
603+ assert "error" not in result
604+ assert result ["data" ][0 ]["content" ] == "async fallback result"
605+ assert mock_client_class .call_args_list [0 ].kwargs ["ots_endpoint" ] == (
606+ "https://test-instance.cn-hangzhou.ots.aliyuncs.com"
607+ )
608+ assert mock_client_class .call_args_list [1 ].kwargs ["ots_endpoint" ] == (
609+ "http://ots-cn-hangzhou.aliyuncs.com"
610+ )
611+ instance_client .retrieve .assert_called_once ()
612+ fallback_client .retrieve .assert_called_once ()
613+
515614 def test_retrieve_without_provider_settings (self ):
516615 """测试无 provider_settings 时检索"""
517616 api = OTSDataAPI ("test-kb" )
@@ -598,7 +697,7 @@ def test_build_client(self, mock_client_class):
598697 """测试构建客户端"""
599698 mock_config = MagicMock (spec = Config )
600699 mock_config .get_ots_endpoint .return_value = (
601- "http ://ots- cn-hangzhou.aliyuncs.com"
700+ "https ://test-instance. cn-hangzhou.ots .aliyuncs.com"
602701 )
603702 mock_config .get_access_key_id .return_value = "test-ak"
604703 mock_config .get_access_key_secret .return_value = "test-sk"
@@ -617,7 +716,7 @@ def test_build_client(self, mock_client_class):
617716 access_key_id = "test-ak" ,
618717 access_key_secret = "test-sk" ,
619718 sts_token = "test-sts" ,
620- ots_endpoint = "http ://ots- cn-hangzhou.aliyuncs.com" ,
719+ ots_endpoint = "https ://test-instance. cn-hangzhou.ots .aliyuncs.com" ,
621720 ots_instance_name = "test-instance" ,
622721 )
623722
0 commit comments