agentscope-runtime 0.2.0b1__py3-none-any.whl → 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. agentscope_runtime/adapters/__init__.py +0 -0
  2. agentscope_runtime/adapters/agentscope/__init__.py +0 -0
  3. agentscope_runtime/adapters/agentscope/long_term_memory/__init__.py +6 -0
  4. agentscope_runtime/adapters/agentscope/long_term_memory/_long_term_memory_adapter.py +258 -0
  5. agentscope_runtime/adapters/agentscope/memory/__init__.py +6 -0
  6. agentscope_runtime/adapters/agentscope/memory/_memory_adapter.py +152 -0
  7. agentscope_runtime/adapters/agentscope/message.py +535 -0
  8. agentscope_runtime/adapters/agentscope/stream.py +506 -0
  9. agentscope_runtime/adapters/agentscope/tool/__init__.py +9 -0
  10. agentscope_runtime/adapters/agentscope/tool/sandbox_tool.py +69 -0
  11. agentscope_runtime/adapters/agentscope/tool/tool.py +233 -0
  12. agentscope_runtime/adapters/autogen/__init__.py +0 -0
  13. agentscope_runtime/adapters/autogen/tool/__init__.py +7 -0
  14. agentscope_runtime/adapters/autogen/tool/tool.py +211 -0
  15. agentscope_runtime/adapters/text/__init__.py +0 -0
  16. agentscope_runtime/adapters/text/stream.py +29 -0
  17. agentscope_runtime/common/collections/redis_mapping.py +4 -1
  18. agentscope_runtime/common/container_clients/fc_client.py +855 -0
  19. agentscope_runtime/common/container_clients/kubernetes_client.py +6 -13
  20. agentscope_runtime/common/utils/__init__.py +0 -0
  21. agentscope_runtime/common/utils/lazy_loader.py +57 -0
  22. agentscope_runtime/engine/__init__.py +25 -18
  23. agentscope_runtime/engine/app/agent_app.py +161 -91
  24. agentscope_runtime/engine/app/base_app.py +4 -118
  25. agentscope_runtime/engine/constant.py +8 -0
  26. agentscope_runtime/engine/deployers/__init__.py +8 -0
  27. agentscope_runtime/engine/deployers/adapter/__init__.py +2 -0
  28. agentscope_runtime/engine/deployers/adapter/a2a/a2a_adapter_utils.py +0 -21
  29. agentscope_runtime/engine/deployers/adapter/a2a/a2a_protocol_adapter.py +28 -9
  30. agentscope_runtime/engine/deployers/adapter/responses/__init__.py +2 -0
  31. agentscope_runtime/engine/deployers/adapter/responses/response_api_adapter_utils.py +5 -2
  32. agentscope_runtime/engine/deployers/adapter/responses/response_api_protocol_adapter.py +1 -1
  33. agentscope_runtime/engine/deployers/agentrun_deployer.py +2541 -0
  34. agentscope_runtime/engine/deployers/cli_fc_deploy.py +1 -1
  35. agentscope_runtime/engine/deployers/kubernetes_deployer.py +9 -21
  36. agentscope_runtime/engine/deployers/local_deployer.py +47 -74
  37. agentscope_runtime/engine/deployers/modelstudio_deployer.py +216 -50
  38. agentscope_runtime/engine/deployers/utils/app_runner_utils.py +29 -0
  39. agentscope_runtime/engine/deployers/utils/detached_app.py +510 -0
  40. agentscope_runtime/engine/deployers/utils/docker_image_utils/__init__.py +1 -1
  41. agentscope_runtime/engine/deployers/utils/docker_image_utils/dockerfile_generator.py +1 -1
  42. agentscope_runtime/engine/deployers/utils/docker_image_utils/{runner_image_factory.py → image_factory.py} +121 -61
  43. agentscope_runtime/engine/deployers/utils/package.py +693 -0
  44. agentscope_runtime/engine/deployers/utils/service_utils/__init__.py +0 -5
  45. agentscope_runtime/engine/deployers/utils/service_utils/fastapi_factory.py +301 -282
  46. agentscope_runtime/engine/deployers/utils/service_utils/fastapi_templates.py +2 -4
  47. agentscope_runtime/engine/deployers/utils/service_utils/process_manager.py +23 -1
  48. agentscope_runtime/engine/deployers/utils/templates/app_main.py.j2 +84 -0
  49. agentscope_runtime/engine/deployers/utils/templates/runner_main.py.j2 +95 -0
  50. agentscope_runtime/engine/deployers/utils/{service_utils → templates}/standalone_main.py.j2 +0 -45
  51. agentscope_runtime/engine/deployers/utils/wheel_packager.py +119 -18
  52. agentscope_runtime/engine/helpers/runner.py +40 -0
  53. agentscope_runtime/engine/runner.py +171 -130
  54. agentscope_runtime/engine/schemas/agent_schemas.py +114 -3
  55. agentscope_runtime/engine/schemas/modelstudio_llm.py +4 -2
  56. agentscope_runtime/engine/schemas/oai_llm.py +23 -23
  57. agentscope_runtime/engine/schemas/response_api.py +65 -0
  58. agentscope_runtime/engine/schemas/session.py +24 -0
  59. agentscope_runtime/engine/services/__init__.py +0 -9
  60. agentscope_runtime/engine/services/agent_state/__init__.py +16 -0
  61. agentscope_runtime/engine/services/agent_state/redis_state_service.py +113 -0
  62. agentscope_runtime/engine/services/agent_state/state_service.py +179 -0
  63. agentscope_runtime/engine/services/memory/__init__.py +24 -0
  64. agentscope_runtime/engine/services/{mem0_memory_service.py → memory/mem0_memory_service.py} +17 -13
  65. agentscope_runtime/engine/services/{memory_service.py → memory/memory_service.py} +28 -7
  66. agentscope_runtime/engine/services/{redis_memory_service.py → memory/redis_memory_service.py} +1 -1
  67. agentscope_runtime/engine/services/{reme_personal_memory_service.py → memory/reme_personal_memory_service.py} +9 -6
  68. agentscope_runtime/engine/services/{reme_task_memory_service.py → memory/reme_task_memory_service.py} +2 -2
  69. agentscope_runtime/engine/services/{tablestore_memory_service.py → memory/tablestore_memory_service.py} +16 -19
  70. agentscope_runtime/engine/services/sandbox/__init__.py +13 -0
  71. agentscope_runtime/engine/services/{sandbox_service.py → sandbox/sandbox_service.py} +86 -71
  72. agentscope_runtime/engine/services/session_history/__init__.py +23 -0
  73. agentscope_runtime/engine/services/{redis_session_history_service.py → session_history/redis_session_history_service.py} +3 -2
  74. agentscope_runtime/engine/services/{session_history_service.py → session_history/session_history_service.py} +44 -34
  75. agentscope_runtime/engine/services/{tablestore_session_history_service.py → session_history/tablestore_session_history_service.py} +14 -19
  76. agentscope_runtime/engine/services/utils/tablestore_service_utils.py +2 -2
  77. agentscope_runtime/engine/tracing/base.py +10 -9
  78. agentscope_runtime/engine/tracing/message_util.py +1 -1
  79. agentscope_runtime/engine/tracing/tracing_util.py +7 -2
  80. agentscope_runtime/engine/tracing/wrapper.py +49 -31
  81. agentscope_runtime/sandbox/__init__.py +10 -2
  82. agentscope_runtime/sandbox/box/agentbay/__init__.py +4 -0
  83. agentscope_runtime/sandbox/box/agentbay/agentbay_sandbox.py +559 -0
  84. agentscope_runtime/sandbox/box/base/base_sandbox.py +12 -0
  85. agentscope_runtime/sandbox/box/browser/browser_sandbox.py +115 -11
  86. agentscope_runtime/sandbox/box/cloud/__init__.py +4 -0
  87. agentscope_runtime/sandbox/box/cloud/cloud_sandbox.py +254 -0
  88. agentscope_runtime/sandbox/box/filesystem/filesystem_sandbox.py +66 -0
  89. agentscope_runtime/sandbox/box/gui/gui_sandbox.py +42 -0
  90. agentscope_runtime/sandbox/box/mobile/__init__.py +4 -0
  91. agentscope_runtime/sandbox/box/mobile/box/__init__.py +0 -0
  92. agentscope_runtime/sandbox/box/mobile/mobile_sandbox.py +216 -0
  93. agentscope_runtime/sandbox/box/training_box/training_box.py +2 -44
  94. agentscope_runtime/sandbox/client/http_client.py +1 -0
  95. agentscope_runtime/sandbox/enums.py +2 -1
  96. agentscope_runtime/sandbox/manager/sandbox_manager.py +15 -2
  97. agentscope_runtime/sandbox/manager/server/app.py +12 -0
  98. agentscope_runtime/sandbox/manager/server/config.py +19 -0
  99. agentscope_runtime/sandbox/model/manager_config.py +79 -2
  100. agentscope_runtime/sandbox/utils.py +0 -18
  101. agentscope_runtime/tools/RAGs/__init__.py +0 -0
  102. agentscope_runtime/tools/RAGs/modelstudio_rag.py +377 -0
  103. agentscope_runtime/tools/RAGs/modelstudio_rag_lite.py +219 -0
  104. agentscope_runtime/tools/__init__.py +119 -0
  105. agentscope_runtime/tools/_constants.py +18 -0
  106. agentscope_runtime/tools/alipay/__init__.py +4 -0
  107. agentscope_runtime/tools/alipay/base.py +334 -0
  108. agentscope_runtime/tools/alipay/payment.py +835 -0
  109. agentscope_runtime/tools/alipay/subscribe.py +551 -0
  110. agentscope_runtime/tools/base.py +264 -0
  111. agentscope_runtime/tools/cli/__init__.py +0 -0
  112. agentscope_runtime/tools/cli/modelstudio_mcp_server.py +78 -0
  113. agentscope_runtime/tools/generations/__init__.py +75 -0
  114. agentscope_runtime/tools/generations/async_image_to_video.py +350 -0
  115. agentscope_runtime/tools/generations/async_image_to_video_wan25.py +366 -0
  116. agentscope_runtime/tools/generations/async_speech_to_video.py +422 -0
  117. agentscope_runtime/tools/generations/async_text_to_video.py +320 -0
  118. agentscope_runtime/tools/generations/async_text_to_video_wan25.py +334 -0
  119. agentscope_runtime/tools/generations/image_edit.py +208 -0
  120. agentscope_runtime/tools/generations/image_edit_wan25.py +193 -0
  121. agentscope_runtime/tools/generations/image_generation.py +202 -0
  122. agentscope_runtime/tools/generations/image_generation_wan25.py +201 -0
  123. agentscope_runtime/tools/generations/image_style_repaint.py +208 -0
  124. agentscope_runtime/tools/generations/image_to_video.py +233 -0
  125. agentscope_runtime/tools/generations/qwen_image_edit.py +205 -0
  126. agentscope_runtime/tools/generations/qwen_image_generation.py +214 -0
  127. agentscope_runtime/tools/generations/qwen_text_to_speech.py +154 -0
  128. agentscope_runtime/tools/generations/speech_to_text.py +260 -0
  129. agentscope_runtime/tools/generations/speech_to_video.py +314 -0
  130. agentscope_runtime/tools/generations/text_to_video.py +221 -0
  131. agentscope_runtime/tools/mcp_wrapper.py +215 -0
  132. agentscope_runtime/tools/realtime_clients/__init__.py +13 -0
  133. agentscope_runtime/tools/realtime_clients/asr_client.py +27 -0
  134. agentscope_runtime/tools/realtime_clients/azure_asr_client.py +195 -0
  135. agentscope_runtime/tools/realtime_clients/azure_tts_client.py +383 -0
  136. agentscope_runtime/tools/realtime_clients/modelstudio_asr_client.py +151 -0
  137. agentscope_runtime/tools/realtime_clients/modelstudio_tts_client.py +199 -0
  138. agentscope_runtime/tools/realtime_clients/realtime_tool.py +55 -0
  139. agentscope_runtime/tools/realtime_clients/tts_client.py +33 -0
  140. agentscope_runtime/tools/searches/__init__.py +3 -0
  141. agentscope_runtime/tools/searches/modelstudio_search.py +877 -0
  142. agentscope_runtime/tools/searches/modelstudio_search_lite.py +310 -0
  143. agentscope_runtime/tools/utils/__init__.py +0 -0
  144. agentscope_runtime/tools/utils/api_key_util.py +45 -0
  145. agentscope_runtime/tools/utils/crypto_utils.py +99 -0
  146. agentscope_runtime/tools/utils/mcp_util.py +35 -0
  147. agentscope_runtime/version.py +1 -1
  148. {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/METADATA +244 -168
  149. agentscope_runtime-1.0.0.dist-info/RECORD +240 -0
  150. {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/entry_points.txt +1 -0
  151. agentscope_runtime/engine/agents/__init__.py +0 -2
  152. agentscope_runtime/engine/agents/agentscope_agent.py +0 -488
  153. agentscope_runtime/engine/agents/agno_agent.py +0 -222
  154. agentscope_runtime/engine/agents/autogen_agent.py +0 -250
  155. agentscope_runtime/engine/agents/base_agent.py +0 -29
  156. agentscope_runtime/engine/agents/langgraph_agent.py +0 -59
  157. agentscope_runtime/engine/agents/utils.py +0 -53
  158. agentscope_runtime/engine/deployers/utils/package_project_utils.py +0 -1163
  159. agentscope_runtime/engine/deployers/utils/service_utils/service_config.py +0 -75
  160. agentscope_runtime/engine/deployers/utils/service_utils/service_factory.py +0 -220
  161. agentscope_runtime/engine/helpers/helper.py +0 -179
  162. agentscope_runtime/engine/schemas/context.py +0 -54
  163. agentscope_runtime/engine/services/context_manager.py +0 -164
  164. agentscope_runtime/engine/services/environment_manager.py +0 -50
  165. agentscope_runtime/engine/services/manager.py +0 -174
  166. agentscope_runtime/engine/services/rag_service.py +0 -195
  167. agentscope_runtime/engine/services/tablestore_rag_service.py +0 -143
  168. agentscope_runtime/sandbox/tools/__init__.py +0 -12
  169. agentscope_runtime/sandbox/tools/base/__init__.py +0 -8
  170. agentscope_runtime/sandbox/tools/base/tool.py +0 -52
  171. agentscope_runtime/sandbox/tools/browser/__init__.py +0 -57
  172. agentscope_runtime/sandbox/tools/browser/tool.py +0 -597
  173. agentscope_runtime/sandbox/tools/filesystem/__init__.py +0 -32
  174. agentscope_runtime/sandbox/tools/filesystem/tool.py +0 -319
  175. agentscope_runtime/sandbox/tools/function_tool.py +0 -321
  176. agentscope_runtime/sandbox/tools/gui/__init__.py +0 -7
  177. agentscope_runtime/sandbox/tools/gui/tool.py +0 -77
  178. agentscope_runtime/sandbox/tools/mcp_tool.py +0 -195
  179. agentscope_runtime/sandbox/tools/sandbox_tool.py +0 -104
  180. agentscope_runtime/sandbox/tools/tool.py +0 -238
  181. agentscope_runtime/sandbox/tools/utils.py +0 -68
  182. agentscope_runtime-0.2.0b1.dist-info/RECORD +0 -183
  183. {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/WHEEL +0 -0
  184. {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/licenses/LICENSE +0 -0
  185. {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,119 @@
1
+ # -*- coding: utf-8 -*-
2
+ from typing import Dict, Type, List
3
+
4
+ from pydantic import BaseModel, Field
5
+
6
+ from .base import Tool
7
+ from .generations.qwen_image_edit import (
8
+ QwenImageEdit,
9
+ )
10
+ from .generations.qwen_image_generation import (
11
+ QwenImageGen,
12
+ )
13
+ from .generations.qwen_text_to_speech import (
14
+ QwenTextToSpeech,
15
+ )
16
+ from .generations.text_to_video import TextToVideo
17
+ from .generations.image_to_video import (
18
+ ImageToVideo,
19
+ )
20
+ from .generations.speech_to_video import (
21
+ SpeechToVideo,
22
+ )
23
+ from .searches.modelstudio_search_lite import (
24
+ ModelstudioSearchLite,
25
+ )
26
+ from .generations.image_generation import (
27
+ ImageGeneration,
28
+ )
29
+ from .generations.image_edit import ImageEdit
30
+ from .generations.image_style_repaint import (
31
+ ImageStyleRepaint,
32
+ )
33
+ from .generations.speech_to_text import (
34
+ SpeechToText,
35
+ )
36
+
37
+ from .generations.async_text_to_video import (
38
+ TextToVideoSubmit,
39
+ TextToVideoFetch,
40
+ )
41
+ from .generations.async_image_to_video import (
42
+ ImageToVideoSubmit,
43
+ ImageToVideoFetch,
44
+ )
45
+ from .generations.async_speech_to_video import (
46
+ SpeechToVideoSubmit,
47
+ SpeechToVideoFetch,
48
+ )
49
+ from .generations.async_image_to_video_wan25 import (
50
+ ImageToVideoWan25Fetch,
51
+ ImageToVideoWan25Submit,
52
+ )
53
+ from .generations.async_text_to_video_wan25 import (
54
+ TextToVideoWan25Submit,
55
+ TextToVideoWan25Fetch,
56
+ )
57
+ from .generations.image_edit_wan25 import (
58
+ ImageEditWan25,
59
+ )
60
+ from .generations.image_generation_wan25 import (
61
+ ImageGenerationWan25,
62
+ )
63
+
64
+
65
+ class McpServerMeta(BaseModel):
66
+ instructions: str = Field(
67
+ ...,
68
+ description="服务描述",
69
+ )
70
+ components: List[Type[Tool]] = Field(
71
+ ...,
72
+ description="组件列表",
73
+ )
74
+
75
+
76
+ mcp_server_metas: Dict[str, McpServerMeta] = {
77
+ "modelstudio_wan_image": McpServerMeta(
78
+ instructions="基于通义万相大模型的智能图像生成服务,提供高质量的图像处理和编辑功能",
79
+ components=[ImageGeneration, ImageEdit, ImageStyleRepaint],
80
+ ),
81
+ "modelstudio_wan_video": McpServerMeta(
82
+ instructions="基于通义万相大模型提供AI视频生成服务,支持文本到视频、图像到视频和语音到视频的多模态生成功能",
83
+ components=[
84
+ TextToVideoSubmit,
85
+ TextToVideoFetch,
86
+ ImageToVideoSubmit,
87
+ ImageToVideoFetch,
88
+ SpeechToVideoSubmit,
89
+ SpeechToVideoFetch,
90
+ ],
91
+ ),
92
+ "modelstudio_wan25_media": McpServerMeta(
93
+ instructions="基于通义万相大模型2.5版本提供的图像和视频生成服务",
94
+ components=[
95
+ ImageGenerationWan25,
96
+ ImageEditWan25,
97
+ TextToVideoWan25Submit,
98
+ TextToVideoWan25Fetch,
99
+ ImageToVideoWan25Submit,
100
+ ImageToVideoWan25Fetch,
101
+ ],
102
+ ),
103
+ "modelstudio_qwen_image": McpServerMeta(
104
+ instructions="基于通义千问大模型的智能图像生成服务,提供高质量的图像处理和编辑功能",
105
+ components=[QwenImageGen, QwenImageEdit],
106
+ ),
107
+ "modelstudio_web_search": McpServerMeta(
108
+ instructions="提供实时互联网搜索服务,提供准确及时的信息检索功能",
109
+ components=[ModelstudioSearchLite],
110
+ ),
111
+ "modelstudio_speech_to_text": McpServerMeta(
112
+ instructions="录音文件的语音识别服务,支持多种音频格式的语音转文字功能",
113
+ components=[SpeechToText],
114
+ ),
115
+ "modelstudio_qwen_text_to_speech": McpServerMeta(
116
+ instructions="基于通义千问大模型的语音合成服务,支持多种语言语音合成功能",
117
+ components=[QwenTextToSpeech],
118
+ ),
119
+ }
@@ -0,0 +1,18 @@
1
+ # -*- coding: utf-8 -*-
2
+ import os
3
+
4
+ BASE_URL = os.getenv(
5
+ "BASE_URL",
6
+ "https://dashscope.aliyuncs.com/compatible-mode/v1",
7
+ )
8
+ DASHSCOPE_HTTP_BASE_URL = os.getenv(
9
+ "DASHSCOPE_HTTP_BASE_URL",
10
+ "https://dashscope.aliyuncs.com/api/v1",
11
+ )
12
+ DASHSCOPE_WEBSOCKET_BASE_URL = os.getenv(
13
+ "DASHSCOPE_WEBSOCKET_BASE_URL",
14
+ "wss://dashscope.aliyuncs.com/api-ws/v1",
15
+ )
16
+ DASHSCOPE_API_KEY = os.getenv("DASHSCOPE_API_KEY")
17
+ NLP_API_KEY = os.getenv("NLPGATEWAY_APIKEY_FOR_RAG")
18
+ DEFAULT_SYSTEM = "You are a helpful assistant."
@@ -0,0 +1,4 @@
1
+ # -*- coding: utf-8 -*-
2
+ from .base import *
3
+ from .payment import *
4
+ from .subscribe import *
@@ -0,0 +1,334 @@
1
+ # -*- coding: utf-8 -*-
2
+ # mypy: disable-error-code="no-redef"
3
+ # pylint:disable=unused-import, line-too-long
4
+
5
+ """
6
+ Alipay Payment Base Module
7
+
8
+ This module provides unified import, configuration checking, and client
9
+ creation for the Alipay SDK. All Alipay-related components should use the
10
+ foundational functionality provided by this module.
11
+
12
+ ---
13
+ 支付宝支付基础模块
14
+
15
+ 该模块提供支付宝SDK的统一导入、配置检查和客户端创建功能。
16
+ 所有支付宝相关的组件都应该使用此模块提供的基础功能。
17
+ """
18
+
19
+
20
+ import os
21
+ import logging
22
+ from typing import Optional, Any, Type, Dict
23
+
24
+ from dotenv import load_dotenv
25
+ from ..utils.crypto_utils import ensure_pkcs1_format
26
+
27
+ try:
28
+ from alipay.aop.api.DefaultAlipayClient import (
29
+ DefaultAlipayClient,
30
+ )
31
+ from alipay.aop.api.AlipayClientConfig import (
32
+ AlipayClientConfig,
33
+ )
34
+ from alipay.aop.api.domain.ExtendParams import (
35
+ ExtendParams,
36
+ )
37
+
38
+ ALIPAY_SDK_AVAILABLE = True
39
+ except ImportError:
40
+ ALIPAY_SDK_AVAILABLE = False
41
+ DefaultAlipayClient: Optional[Type[Any]] = None
42
+ AlipayClientConfig: Optional[Type[Any]] = None
43
+ ExtendParams: Optional[Type[Any]] = None
44
+
45
+ logger = logging.getLogger(__name__)
46
+
47
+ load_dotenv()
48
+
49
+
50
+ # Alipay environment configuration - controls whether to use production or
51
+ # sandbox environment
52
+ AP_CURRENT_ENV = os.getenv("AP_CURRENT_ENV", "production")
53
+
54
+ # Application ID (APPID) applied by the merchant on the Alipay open platform.
55
+ ALIPAY_APP_ID = os.getenv("ALIPAY_APP_ID", "")
56
+ # Merchant's private key applied via Alipay open platform.
57
+ ALIPAY_PRIVATE_KEY = os.getenv("ALIPAY_PRIVATE_KEY", "")
58
+ # Alipay public key used to verify server-side data signatures, obtained
59
+ # from the open platform. Required.
60
+ ALIPAY_PUBLIC_KEY = os.getenv("ALIPAY_PUBLIC_KEY", "")
61
+ # Synchronous return URL - page address to jump to after user completes
62
+ # payment. Optional.
63
+ AP_RETURN_URL = os.getenv("AP_RETURN_URL", "")
64
+ # Asynchronous notification URL - callback address for Alipay to notify
65
+ # payment results. Optional.
66
+ AP_NOTIFY_URL = os.getenv("AP_NOTIFY_URL", "")
67
+ # AI agent channel source - used to identify the source of AI agents
68
+ X_AGENT_CHANNEL = "bailian_adk_1.0.0"
69
+
70
+
71
+ # Unified Alipay SDK import and availability check
72
+
73
+
74
+ class AgentExtendParams(
75
+ ExtendParams if ALIPAY_SDK_AVAILABLE else object, # type: ignore[misc]
76
+ ):
77
+ """
78
+ AI Agent Extended Parameters Class, inheriting from Alipay SDK's
79
+ ExtendParams. Adds support for request_channel_source parameter to
80
+ identify AI agent source.
81
+
82
+ ---
83
+ 智能体扩展参数类,继承支付宝SDK的ExtendParams
84
+ 添加request_channel_source参数支持,用于标识AI智能体来源
85
+ """
86
+
87
+ def __init__(self) -> None:
88
+ if ALIPAY_SDK_AVAILABLE:
89
+ super().__init__()
90
+ self._request_channel_source = None
91
+
92
+ @property
93
+ def request_channel_source(self) -> Optional[str]:
94
+ return self._request_channel_source
95
+
96
+ @request_channel_source.setter
97
+ def request_channel_source(self, value: Optional[str]) -> None:
98
+ self._request_channel_source = value
99
+
100
+ def to_alipay_dict(self) -> Dict[str, Any]:
101
+ """
102
+ Override parent method to add request_channel_source to the
103
+ serialized result.
104
+
105
+ ---
106
+ 重写父类方法,添加request_channel_source到序列化结果中
107
+ """
108
+ if ALIPAY_SDK_AVAILABLE:
109
+ params = super().to_alipay_dict()
110
+ else:
111
+ params = {}
112
+
113
+ if self.request_channel_source:
114
+ params["request_channel_source"] = self.request_channel_source
115
+ return params
116
+
117
+ @staticmethod
118
+ def from_alipay_dict(
119
+ d: Optional[Dict[str, Any]],
120
+ ) -> Optional["AgentExtendParams"]:
121
+ """
122
+ Override parent static method to support deserialization of
123
+ request_channel_source.
124
+
125
+ ---
126
+ 重写父类静态方法,支持request_channel_source的反序列化
127
+ """
128
+ if not d:
129
+ return None
130
+
131
+ # Create instance
132
+ agent_params = AgentExtendParams()
133
+
134
+ # If SDK is available, let parent handle standard attributes first
135
+ if ALIPAY_SDK_AVAILABLE:
136
+ parent_obj = ExtendParams.from_alipay_dict(d)
137
+ if parent_obj:
138
+ # Copy attributes from parent object
139
+ agent_params.__dict__.update(parent_obj.__dict__)
140
+
141
+ # Handle custom attributes
142
+ if "request_channel_source" in d:
143
+ agent_params.request_channel_source = d["request_channel_source"]
144
+
145
+ return agent_params
146
+
147
+
148
+ def get_alipay_gateway_url() -> str:
149
+ """
150
+ Get Alipay gateway URL based on environment variables.
151
+
152
+ Returns:
153
+ str: Alipay gateway URL for the corresponding environment
154
+ - Sandbox: https://openapi-sandbox.dl.alipaydev.com/gateway.do
155
+ - Production: https://openapi.alipay.com/gateway.do
156
+
157
+ ---
158
+ 根据环境变量获取支付宝网关地址
159
+
160
+ Returns:
161
+ str: 对应环境的支付宝网关地址
162
+ - 沙箱环境: https://openapi-sandbox.dl.alipaydev.com/gateway.do
163
+ - 生产环境: https://openapi.alipay.com/gateway.do
164
+ """
165
+ return (
166
+ "https://openapi-sandbox.dl.alipaydev.com/gateway.do"
167
+ if AP_CURRENT_ENV == "sandbox"
168
+ else "https://openapi.alipay.com/gateway.do"
169
+ )
170
+
171
+
172
+ def _check_config_and_sdk() -> None:
173
+ """
174
+ Check whether Alipay configuration and SDK are available.
175
+
176
+ This function verifies:
177
+ 1. Required environment variables are set (ALIPAY_APP_ID,
178
+ ALIPAY_PRIVATE_KEY, ALIPAY_PUBLIC_KEY)
179
+ 2. Alipay SDK is successfully imported
180
+
181
+ Raises:
182
+ ValueError: If required environment variables are not set.
183
+ ImportError: If Alipay SDK is not installed or failed to import.
184
+
185
+ ---
186
+ 检查支付宝配置和SDK是否可用
187
+
188
+ 该函数会验证以下内容:
189
+ 1. 检查必需的环境变量是否设置(ALIPAY_APP_ID、ALIPAY_PRIVATE_KEY、ALIPAY_PUBLIC_KEY)
190
+ 2. 检查支付宝SDK是否成功导入
191
+
192
+ Raises:
193
+ ValueError: 当必需的环境变量未设置时抛出
194
+ ImportError: 当支付宝SDK未安装或导入失败时抛出
195
+ """
196
+ # Check required environment variables
197
+ if not ALIPAY_APP_ID or not ALIPAY_PRIVATE_KEY or not ALIPAY_PUBLIC_KEY:
198
+ raise ValueError(
199
+ "Payment configuration error: Please set ALIPAY_APP_ID, "
200
+ "ALIPAY_PRIVATE_KEY, and ALIPAY_PUBLIC_KEY environment variables.",
201
+ )
202
+
203
+ # Check whether Alipay official SDK is available
204
+ if not ALIPAY_SDK_AVAILABLE:
205
+ raise ImportError(
206
+ "Please install the official Alipay SDK: pip install "
207
+ "alipay-sdk-python",
208
+ )
209
+
210
+
211
+ class AgentAlipayClient(DefaultAlipayClient):
212
+ """
213
+ AI Agent Alipay Client, inheriting from DefaultAlipayClient and
214
+ overriding relevant methods.
215
+
216
+ ---
217
+ 智能体支付宝客户端,继承DefaultAlipayClient并重写相关方法
218
+ """
219
+
220
+ def _DefaultAlipayClient__get_common_params(
221
+ self,
222
+ params: Dict[str, Any],
223
+ ) -> Dict[str, Any]:
224
+ """
225
+ Override the parent private method to add AI agent identifier
226
+ parameter to common_params.
227
+
228
+ Args:
229
+ params: Request parameters
230
+
231
+ Returns:
232
+ dict: common_params containing AI agent identifier
233
+
234
+ ---
235
+ 重写父类的私有方法,在common_params中添加AI智能体标识参数
236
+
237
+ Args:
238
+ params: 请求参数
239
+
240
+ Returns:
241
+ dict: 包含AI智能体标识的common_params
242
+ """
243
+ # Call the parent private method
244
+ common_params = super()._DefaultAlipayClient__get_common_params(params)
245
+ common_params["x_agent_source"] = X_AGENT_CHANNEL
246
+ return common_params
247
+
248
+ def _DefaultAlipayClient__remove_common_params(
249
+ self,
250
+ params: Dict[str, Any],
251
+ ) -> Dict[str, Any]:
252
+ """
253
+ Override the parent private method to retain our custom parameters.
254
+
255
+ Args:
256
+ params: Request parameter dictionary
257
+
258
+ ---
259
+ 重写父类的私有方法,保留我们的自定义参数
260
+
261
+ Args:
262
+ params: 请求参数字典
263
+ """
264
+ if not params:
265
+ return params
266
+
267
+ # Import the COMMON_PARAM_KEYS constant from parent
268
+ from alipay.aop.api.constant.ParamConstants import COMMON_PARAM_KEYS
269
+
270
+ # Create a new constant set excluding our custom parameters
271
+ keys_to_remove = COMMON_PARAM_KEYS.copy()
272
+ keys_to_remove.discard("x_agent_source")
273
+
274
+ for k in keys_to_remove:
275
+ if k in params:
276
+ params.pop(k)
277
+
278
+ return params
279
+
280
+
281
+ def _create_alipay_client() -> Any:
282
+ """
283
+ Create an Alipay client instance.
284
+
285
+ This function performs:
286
+ 1. Validates configuration and SDK availability
287
+ 2. Loads key configuration from environment variables
288
+ 3. Creates an Alipay client configuration object
289
+ 4. Initializes and returns an Alipay client instance
290
+
291
+ Returns:
292
+ Any: Configured Alipay client instance (DefaultAlipayClient)
293
+
294
+ Raises:
295
+ ValueError: If environment variable configuration is incorrect
296
+ ImportError: If Alipay SDK is unavailable
297
+
298
+ ---
299
+ 创建支付宝客户端实例
300
+
301
+ 该函数会执行以下操作:
302
+ 1. 验证配置和SDK可用性
303
+ 2. 读取环境变量中的密钥配置
304
+ 3. 创建支付宝客户端配置对象
305
+ 4. 初始化并返回支付宝客户端实例
306
+
307
+ Returns:
308
+ Any: 配置完成的支付宝客户端实例 (DefaultAlipayClient)
309
+
310
+ Raises:
311
+ ValueError: 当环境变量配置错误时
312
+ ImportError: 当支付宝SDK不可用时
313
+ """
314
+ logger.info("Creating Alipay client instance...")
315
+ logger.info(f"Current Alipay environment: {AP_CURRENT_ENV}")
316
+ # Validate configuration and SDK availability
317
+ _check_config_and_sdk()
318
+
319
+ # Ensure private key is in PKCS#1 format for compatibility with
320
+ # alipay-sdk-python
321
+ private_key = ensure_pkcs1_format(ALIPAY_PRIVATE_KEY)
322
+ public_key = ALIPAY_PUBLIC_KEY
323
+
324
+ # Create Alipay client configuration object
325
+ alipay_client_config = AlipayClientConfig()
326
+ gateway_url = get_alipay_gateway_url()
327
+ alipay_client_config.server_url = gateway_url
328
+ alipay_client_config.app_id = ALIPAY_APP_ID # App ID
329
+ alipay_client_config.app_private_key = private_key # App private key
330
+ alipay_client_config.alipay_public_key = public_key # Alipay public key
331
+ alipay_client_config.sign_type = "RSA2" # Signature algorithm type
332
+
333
+ # Create and return Alipay client instance
334
+ return AgentAlipayClient(alipay_client_config=alipay_client_config)