agentscope-runtime 0.2.0b2__py3-none-any.whl → 1.0.0b1__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 (183) 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 +474 -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/utils/__init__.py +0 -0
  20. agentscope_runtime/common/utils/lazy_loader.py +57 -0
  21. agentscope_runtime/engine/__init__.py +25 -18
  22. agentscope_runtime/engine/app/agent_app.py +161 -91
  23. agentscope_runtime/engine/app/base_app.py +4 -118
  24. agentscope_runtime/engine/constant.py +8 -0
  25. agentscope_runtime/engine/deployers/__init__.py +8 -0
  26. agentscope_runtime/engine/deployers/adapter/__init__.py +2 -0
  27. agentscope_runtime/engine/deployers/adapter/a2a/a2a_adapter_utils.py +0 -21
  28. agentscope_runtime/engine/deployers/adapter/a2a/a2a_protocol_adapter.py +28 -9
  29. agentscope_runtime/engine/deployers/adapter/responses/__init__.py +2 -0
  30. agentscope_runtime/engine/deployers/adapter/responses/response_api_adapter_utils.py +5 -2
  31. agentscope_runtime/engine/deployers/adapter/responses/response_api_protocol_adapter.py +1 -1
  32. agentscope_runtime/engine/deployers/agentrun_deployer.py +2541 -0
  33. agentscope_runtime/engine/deployers/cli_fc_deploy.py +1 -1
  34. agentscope_runtime/engine/deployers/kubernetes_deployer.py +9 -21
  35. agentscope_runtime/engine/deployers/local_deployer.py +47 -74
  36. agentscope_runtime/engine/deployers/modelstudio_deployer.py +216 -50
  37. agentscope_runtime/engine/deployers/utils/app_runner_utils.py +29 -0
  38. agentscope_runtime/engine/deployers/utils/detached_app.py +510 -0
  39. agentscope_runtime/engine/deployers/utils/docker_image_utils/__init__.py +1 -1
  40. agentscope_runtime/engine/deployers/utils/docker_image_utils/dockerfile_generator.py +1 -1
  41. agentscope_runtime/engine/deployers/utils/docker_image_utils/{runner_image_factory.py → image_factory.py} +121 -61
  42. agentscope_runtime/engine/deployers/utils/package.py +693 -0
  43. agentscope_runtime/engine/deployers/utils/service_utils/__init__.py +0 -5
  44. agentscope_runtime/engine/deployers/utils/service_utils/fastapi_factory.py +256 -282
  45. agentscope_runtime/engine/deployers/utils/service_utils/fastapi_templates.py +2 -4
  46. agentscope_runtime/engine/deployers/utils/service_utils/process_manager.py +23 -1
  47. agentscope_runtime/engine/deployers/utils/templates/app_main.py.j2 +84 -0
  48. agentscope_runtime/engine/deployers/utils/templates/runner_main.py.j2 +95 -0
  49. agentscope_runtime/engine/deployers/utils/{service_utils → templates}/standalone_main.py.j2 +0 -45
  50. agentscope_runtime/engine/deployers/utils/wheel_packager.py +119 -18
  51. agentscope_runtime/engine/helpers/runner.py +40 -0
  52. agentscope_runtime/engine/runner.py +170 -130
  53. agentscope_runtime/engine/schemas/agent_schemas.py +114 -3
  54. agentscope_runtime/engine/schemas/modelstudio_llm.py +4 -2
  55. agentscope_runtime/engine/schemas/oai_llm.py +23 -23
  56. agentscope_runtime/engine/schemas/response_api.py +65 -0
  57. agentscope_runtime/engine/schemas/session.py +24 -0
  58. agentscope_runtime/engine/services/__init__.py +0 -9
  59. agentscope_runtime/engine/services/agent_state/__init__.py +16 -0
  60. agentscope_runtime/engine/services/agent_state/redis_state_service.py +113 -0
  61. agentscope_runtime/engine/services/agent_state/state_service.py +179 -0
  62. agentscope_runtime/engine/services/memory/__init__.py +24 -0
  63. agentscope_runtime/engine/services/{mem0_memory_service.py → memory/mem0_memory_service.py} +17 -13
  64. agentscope_runtime/engine/services/{memory_service.py → memory/memory_service.py} +28 -7
  65. agentscope_runtime/engine/services/{redis_memory_service.py → memory/redis_memory_service.py} +1 -1
  66. agentscope_runtime/engine/services/{reme_personal_memory_service.py → memory/reme_personal_memory_service.py} +9 -6
  67. agentscope_runtime/engine/services/{reme_task_memory_service.py → memory/reme_task_memory_service.py} +2 -2
  68. agentscope_runtime/engine/services/{tablestore_memory_service.py → memory/tablestore_memory_service.py} +12 -18
  69. agentscope_runtime/engine/services/sandbox/__init__.py +13 -0
  70. agentscope_runtime/engine/services/{sandbox_service.py → sandbox/sandbox_service.py} +86 -71
  71. agentscope_runtime/engine/services/session_history/__init__.py +23 -0
  72. agentscope_runtime/engine/services/{redis_session_history_service.py → session_history/redis_session_history_service.py} +3 -2
  73. agentscope_runtime/engine/services/{session_history_service.py → session_history/session_history_service.py} +44 -34
  74. agentscope_runtime/engine/services/{tablestore_session_history_service.py → session_history/tablestore_session_history_service.py} +14 -19
  75. agentscope_runtime/engine/services/utils/tablestore_service_utils.py +2 -2
  76. agentscope_runtime/engine/tracing/base.py +10 -9
  77. agentscope_runtime/engine/tracing/message_util.py +1 -1
  78. agentscope_runtime/engine/tracing/tracing_util.py +7 -2
  79. agentscope_runtime/sandbox/__init__.py +10 -2
  80. agentscope_runtime/sandbox/box/agentbay/__init__.py +4 -0
  81. agentscope_runtime/sandbox/box/agentbay/agentbay_sandbox.py +559 -0
  82. agentscope_runtime/sandbox/box/base/base_sandbox.py +12 -0
  83. agentscope_runtime/sandbox/box/browser/browser_sandbox.py +115 -11
  84. agentscope_runtime/sandbox/box/cloud/__init__.py +4 -0
  85. agentscope_runtime/sandbox/box/cloud/cloud_sandbox.py +254 -0
  86. agentscope_runtime/sandbox/box/filesystem/filesystem_sandbox.py +66 -0
  87. agentscope_runtime/sandbox/box/gui/gui_sandbox.py +42 -0
  88. agentscope_runtime/sandbox/box/mobile/__init__.py +4 -0
  89. agentscope_runtime/sandbox/box/mobile/box/__init__.py +0 -0
  90. agentscope_runtime/sandbox/box/mobile/mobile_sandbox.py +216 -0
  91. agentscope_runtime/sandbox/box/training_box/training_box.py +2 -2
  92. agentscope_runtime/sandbox/client/http_client.py +1 -0
  93. agentscope_runtime/sandbox/enums.py +2 -0
  94. agentscope_runtime/sandbox/manager/sandbox_manager.py +18 -2
  95. agentscope_runtime/sandbox/manager/server/app.py +12 -0
  96. agentscope_runtime/sandbox/manager/server/config.py +19 -0
  97. agentscope_runtime/sandbox/model/manager_config.py +79 -2
  98. agentscope_runtime/sandbox/utils.py +0 -18
  99. agentscope_runtime/tools/RAGs/__init__.py +0 -0
  100. agentscope_runtime/tools/RAGs/modelstudio_rag.py +377 -0
  101. agentscope_runtime/tools/RAGs/modelstudio_rag_lite.py +219 -0
  102. agentscope_runtime/tools/__init__.py +119 -0
  103. agentscope_runtime/tools/_constants.py +18 -0
  104. agentscope_runtime/tools/alipay/__init__.py +4 -0
  105. agentscope_runtime/tools/alipay/base.py +334 -0
  106. agentscope_runtime/tools/alipay/payment.py +835 -0
  107. agentscope_runtime/tools/alipay/subscribe.py +551 -0
  108. agentscope_runtime/tools/base.py +264 -0
  109. agentscope_runtime/tools/cli/__init__.py +0 -0
  110. agentscope_runtime/tools/cli/modelstudio_mcp_server.py +78 -0
  111. agentscope_runtime/tools/generations/__init__.py +75 -0
  112. agentscope_runtime/tools/generations/async_image_to_video.py +350 -0
  113. agentscope_runtime/tools/generations/async_image_to_video_wan25.py +366 -0
  114. agentscope_runtime/tools/generations/async_speech_to_video.py +422 -0
  115. agentscope_runtime/tools/generations/async_text_to_video.py +320 -0
  116. agentscope_runtime/tools/generations/async_text_to_video_wan25.py +334 -0
  117. agentscope_runtime/tools/generations/image_edit.py +208 -0
  118. agentscope_runtime/tools/generations/image_edit_wan25.py +193 -0
  119. agentscope_runtime/tools/generations/image_generation.py +202 -0
  120. agentscope_runtime/tools/generations/image_generation_wan25.py +201 -0
  121. agentscope_runtime/tools/generations/image_style_repaint.py +208 -0
  122. agentscope_runtime/tools/generations/image_to_video.py +233 -0
  123. agentscope_runtime/tools/generations/qwen_image_edit.py +205 -0
  124. agentscope_runtime/tools/generations/qwen_image_generation.py +214 -0
  125. agentscope_runtime/tools/generations/qwen_text_to_speech.py +154 -0
  126. agentscope_runtime/tools/generations/speech_to_text.py +260 -0
  127. agentscope_runtime/tools/generations/speech_to_video.py +314 -0
  128. agentscope_runtime/tools/generations/text_to_video.py +221 -0
  129. agentscope_runtime/tools/mcp_wrapper.py +215 -0
  130. agentscope_runtime/tools/realtime_clients/__init__.py +13 -0
  131. agentscope_runtime/tools/realtime_clients/asr_client.py +27 -0
  132. agentscope_runtime/tools/realtime_clients/azure_asr_client.py +195 -0
  133. agentscope_runtime/tools/realtime_clients/azure_tts_client.py +383 -0
  134. agentscope_runtime/tools/realtime_clients/modelstudio_asr_client.py +151 -0
  135. agentscope_runtime/tools/realtime_clients/modelstudio_tts_client.py +199 -0
  136. agentscope_runtime/tools/realtime_clients/realtime_tool.py +55 -0
  137. agentscope_runtime/tools/realtime_clients/tts_client.py +33 -0
  138. agentscope_runtime/tools/searches/__init__.py +3 -0
  139. agentscope_runtime/tools/searches/modelstudio_search.py +877 -0
  140. agentscope_runtime/tools/searches/modelstudio_search_lite.py +310 -0
  141. agentscope_runtime/tools/utils/__init__.py +0 -0
  142. agentscope_runtime/tools/utils/api_key_util.py +45 -0
  143. agentscope_runtime/tools/utils/crypto_utils.py +99 -0
  144. agentscope_runtime/tools/utils/mcp_util.py +35 -0
  145. agentscope_runtime/version.py +1 -1
  146. {agentscope_runtime-0.2.0b2.dist-info → agentscope_runtime-1.0.0b1.dist-info}/METADATA +234 -165
  147. agentscope_runtime-1.0.0b1.dist-info/RECORD +240 -0
  148. {agentscope_runtime-0.2.0b2.dist-info → agentscope_runtime-1.0.0b1.dist-info}/entry_points.txt +1 -0
  149. agentscope_runtime/engine/agents/__init__.py +0 -2
  150. agentscope_runtime/engine/agents/agentscope_agent.py +0 -488
  151. agentscope_runtime/engine/agents/agno_agent.py +0 -220
  152. agentscope_runtime/engine/agents/autogen_agent.py +0 -250
  153. agentscope_runtime/engine/agents/base_agent.py +0 -29
  154. agentscope_runtime/engine/agents/langgraph_agent.py +0 -59
  155. agentscope_runtime/engine/agents/utils.py +0 -53
  156. agentscope_runtime/engine/deployers/utils/package_project_utils.py +0 -1163
  157. agentscope_runtime/engine/deployers/utils/service_utils/service_config.py +0 -75
  158. agentscope_runtime/engine/deployers/utils/service_utils/service_factory.py +0 -220
  159. agentscope_runtime/engine/helpers/helper.py +0 -179
  160. agentscope_runtime/engine/schemas/context.py +0 -54
  161. agentscope_runtime/engine/services/context_manager.py +0 -164
  162. agentscope_runtime/engine/services/environment_manager.py +0 -50
  163. agentscope_runtime/engine/services/manager.py +0 -174
  164. agentscope_runtime/engine/services/rag_service.py +0 -195
  165. agentscope_runtime/engine/services/tablestore_rag_service.py +0 -143
  166. agentscope_runtime/sandbox/tools/__init__.py +0 -12
  167. agentscope_runtime/sandbox/tools/base/__init__.py +0 -8
  168. agentscope_runtime/sandbox/tools/base/tool.py +0 -52
  169. agentscope_runtime/sandbox/tools/browser/__init__.py +0 -57
  170. agentscope_runtime/sandbox/tools/browser/tool.py +0 -597
  171. agentscope_runtime/sandbox/tools/filesystem/__init__.py +0 -32
  172. agentscope_runtime/sandbox/tools/filesystem/tool.py +0 -319
  173. agentscope_runtime/sandbox/tools/function_tool.py +0 -321
  174. agentscope_runtime/sandbox/tools/gui/__init__.py +0 -7
  175. agentscope_runtime/sandbox/tools/gui/tool.py +0 -77
  176. agentscope_runtime/sandbox/tools/mcp_tool.py +0 -195
  177. agentscope_runtime/sandbox/tools/sandbox_tool.py +0 -104
  178. agentscope_runtime/sandbox/tools/tool.py +0 -238
  179. agentscope_runtime/sandbox/tools/utils.py +0 -68
  180. agentscope_runtime-0.2.0b2.dist-info/RECORD +0 -183
  181. {agentscope_runtime-0.2.0b2.dist-info → agentscope_runtime-1.0.0b1.dist-info}/WHEEL +0 -0
  182. {agentscope_runtime-0.2.0b2.dist-info → agentscope_runtime-1.0.0b1.dist-info}/licenses/LICENSE +0 -0
  183. {agentscope_runtime-0.2.0b2.dist-info → agentscope_runtime-1.0.0b1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,835 @@
1
+ # -*- coding: utf-8 -*-
2
+ # mypy: disable-error-code="no-redef"
3
+
4
+ import logging
5
+ from datetime import datetime
6
+ from typing import Any, Optional, Type
7
+
8
+ from pydantic import BaseModel, Field
9
+
10
+ from .base import (
11
+ AP_RETURN_URL,
12
+ AP_NOTIFY_URL,
13
+ X_AGENT_CHANNEL,
14
+ _create_alipay_client,
15
+ AgentExtendParams,
16
+ )
17
+ from ..base import Tool
18
+
19
+ try:
20
+ from alipay.aop.api.request.AlipayTradeWapPayRequest import (
21
+ AlipayTradeWapPayRequest,
22
+ )
23
+ from alipay.aop.api.request.AlipayTradePagePayRequest import (
24
+ AlipayTradePagePayRequest,
25
+ )
26
+ from alipay.aop.api.request.AlipayTradeQueryRequest import (
27
+ AlipayTradeQueryRequest,
28
+ )
29
+ from alipay.aop.api.request.AlipayTradeRefundRequest import (
30
+ AlipayTradeRefundRequest,
31
+ )
32
+ from alipay.aop.api.request.AlipayTradeFastpayRefundQueryRequest import (
33
+ AlipayTradeFastpayRefundQueryRequest,
34
+ )
35
+ from alipay.aop.api.domain.AlipayTradePagePayModel import (
36
+ AlipayTradePagePayModel,
37
+ )
38
+ from alipay.aop.api.domain.AlipayTradeWapPayModel import (
39
+ AlipayTradeWapPayModel,
40
+ )
41
+ from alipay.aop.api.domain.AlipayTradeQueryModel import (
42
+ AlipayTradeQueryModel,
43
+ )
44
+ from alipay.aop.api.domain.AlipayTradeRefundModel import (
45
+ AlipayTradeRefundModel,
46
+ )
47
+ from alipay.aop.api.domain.AlipayTradeFastpayRefundQueryModel import (
48
+ AlipayTradeFastpayRefundQueryModel,
49
+ )
50
+ from alipay.aop.api.response.AlipayTradeQueryResponse import (
51
+ AlipayTradeQueryResponse,
52
+ )
53
+ from alipay.aop.api.response.AlipayTradeRefundResponse import (
54
+ AlipayTradeRefundResponse,
55
+ )
56
+ from alipay.aop.api.response.AlipayTradeFastpayRefundQueryResponse import (
57
+ AlipayTradeFastpayRefundQueryResponse,
58
+ )
59
+
60
+ ALIPAY_SDK_AVAILABLE = True
61
+ except ImportError:
62
+ ALIPAY_SDK_AVAILABLE = False
63
+ AlipayTradeWapPayRequest: Optional[Type[Any]] = None
64
+ AlipayTradePagePayRequest: Optional[Type[Any]] = None
65
+ AlipayTradeQueryRequest: Optional[Type[Any]] = None
66
+ AlipayTradeRefundRequest: Optional[Type[Any]] = None
67
+ AlipayTradeFastpayRefundQueryRequest: Optional[Type[Any]] = None
68
+ AlipayTradePagePayModel: Optional[Type[Any]] = None
69
+ AlipayTradeWapPayModel: Optional[Type[Any]] = None
70
+ AlipayTradeQueryModel: Optional[Type[Any]] = None
71
+ AlipayTradeRefundModel: Optional[Type[Any]] = None
72
+ AlipayTradeFastpayRefundQueryModel: Optional[Type[Any]] = None
73
+ AlipayTradeQueryResponse: Optional[Type[Any]] = None
74
+ AlipayTradeRefundResponse: Optional[Type[Any]] = None
75
+ AlipayTradeFastpayRefundQueryResponse: Optional[Type[Any]] = None
76
+
77
+
78
+ logger = logging.getLogger(__name__)
79
+
80
+
81
+ class MobilePaymentInput(BaseModel):
82
+ """Mobile Alipay payment input schema."""
83
+
84
+ out_trade_no: str = Field(
85
+ ...,
86
+ description="创建订单参数-商户订单号",
87
+ )
88
+ order_title: str = Field(
89
+ ...,
90
+ description="该订单的订单标题",
91
+ )
92
+ total_amount: float = Field(
93
+ ...,
94
+ gt=0,
95
+ description="该订单的支付金额,以元为单位",
96
+ )
97
+
98
+
99
+ class WebPagePaymentInput(BaseModel):
100
+ """Web page Alipay payment input schema."""
101
+
102
+ out_trade_no: str = Field(
103
+ ...,
104
+ description="创建订单参数-商户订单号",
105
+ )
106
+ order_title: str = Field(
107
+ ...,
108
+ description="该订单的订单标题",
109
+ )
110
+ total_amount: float = Field(
111
+ ...,
112
+ gt=0,
113
+ description="该订单的支付金额,以元为单位",
114
+ )
115
+
116
+
117
+ class PaymentQueryInput(BaseModel):
118
+ """Payment query input schema."""
119
+
120
+ out_trade_no: str = Field(
121
+ ...,
122
+ description="商户订单号",
123
+ )
124
+
125
+
126
+ class PaymentRefundInput(BaseModel):
127
+ """Payment refund input schema."""
128
+
129
+ out_trade_no: str = Field(
130
+ ...,
131
+ description="商户订单号",
132
+ )
133
+ refund_amount: float = Field(
134
+ ...,
135
+ gt=0,
136
+ description="退款金额",
137
+ )
138
+ refund_reason: Optional[str] = Field(
139
+ default=None,
140
+ description="退款原因",
141
+ )
142
+ out_request_no: Optional[str] = Field(
143
+ default=None,
144
+ description="退款请求号",
145
+ )
146
+
147
+
148
+ class RefundQueryInput(BaseModel):
149
+ """Refund query input schema."""
150
+
151
+ out_trade_no: str = Field(
152
+ ...,
153
+ description="商户订单号",
154
+ )
155
+ out_request_no: str = Field(
156
+ ...,
157
+ description="退款请求号",
158
+ )
159
+
160
+
161
+ class PaymentOutput(BaseModel):
162
+ """Payment operation output schema."""
163
+
164
+ result: str = Field(
165
+ ...,
166
+ description="包含链接的 markdown 文本," "你要将文本插入对话内容中。",
167
+ )
168
+
169
+
170
+ class MobileAlipayPayment(Tool[MobilePaymentInput, PaymentOutput]):
171
+ """
172
+ Mobile Alipay Payment Component
173
+
174
+ This component is used to create Alipay payment orders suitable for
175
+ mobile clients. The generated payment link can be opened in a mobile
176
+ browser to redirect users to the Alipay application for payment or
177
+ complete payment directly in the browser.
178
+
179
+ Key features:
180
+ - Suitable for mobile websites and mobile applications
181
+ - Supports in-app payment and in-browser payment via Alipay
182
+ - Uses the QUICK_WAP_WAY product code
183
+ - Returns a ready-to-use payment link
184
+
185
+ Input type: MobilePaymentInput
186
+ Output type: PaymentOutput
187
+
188
+ Usage scenarios:
189
+ - Mobile website payment
190
+ - Embedded mobile App payment
191
+
192
+ ---
193
+ 手机端支付宝支付组件
194
+
195
+ 该组件用于创建适合手机端的支付宝支付订单。生成的支付链接可以在手机浏览器中打开,
196
+ 用户可以跳转到支付宝应用完成支付,或者直接在浏览器中进行支付操作。
197
+
198
+ 主要特点:
199
+ - 适用于移动网站和移动应用
200
+ - 支持支付宝应用内支付和浏览器内支付
201
+ - 使用QUICK_WAP_WAY产品码
202
+ - 返回可直接使用的支付链接
203
+
204
+ 输入参数类型:MobilePaymentInput
205
+ 输出参数类型:PaymentOutput
206
+
207
+ 使用场景:
208
+ - 移动端网站支付
209
+ - 手机App内嵌支付
210
+ """
211
+
212
+ name: str = "alipay_mobile_payment"
213
+ description: str = (
214
+ "创建一笔支付宝订单,返回带有支付链接的 Markdown 文本,"
215
+ "该链接在手机浏览器中打开后可跳转到支付宝或直接在浏览器中支付。"
216
+ "本工具适用于移动网站或移动 App。"
217
+ )
218
+
219
+ async def _arun(
220
+ self,
221
+ args: MobilePaymentInput,
222
+ **kwargs: Any,
223
+ ) -> PaymentOutput:
224
+ """
225
+ Create a mobile Alipay payment order.
226
+
227
+ This method is used to create an Alipay payment order suitable for
228
+ mobile browsers. The generated payment link can be opened in a
229
+ mobile browser, and the user can complete the payment either in the
230
+ Alipay app or within the browser.
231
+
232
+ Args:
233
+ args (MobilePaymentInput): Object containing payment parameters
234
+ - out_trade_no: Merchant order number
235
+ - order_title: Order title
236
+ - total_amount: Payment amount (in yuan)
237
+ **kwargs: Additional keyword arguments
238
+
239
+ Returns:
240
+ PaymentOutput: Markdown text output containing the payment link
241
+
242
+ Raises:
243
+ ValueError: If configuration parameters are incorrect
244
+ ImportError: If Alipay SDK is not available
245
+ Exception: For any other error during order creation
246
+
247
+ ---
248
+ 创建手机支付宝支付订单
249
+
250
+ 该方法用于创建适用于手机浏览器的支付宝支付订单。生成的支付链接可以在手机浏览器中
251
+ 打开,用户可以跳转到支付宝应用或直接在浏览器中完成支付。
252
+
253
+ Args:
254
+ args (MobilePaymentInput): 包含支付参数的输入对象
255
+ - out_trade_no: 商户订单号
256
+ - order_title: 订单标题
257
+ - total_amount: 支付金额(元)
258
+ **kwargs: 额外的关键字参数
259
+
260
+ Returns:
261
+ PaymentOutput: 包含支付链接的Markdown文本输出
262
+
263
+ Raises:
264
+ ValueError: 当配置参数错误时
265
+ ImportError: 当支付宝SDK不可用时
266
+ Exception: 当创建订单过程中发生其他错误时
267
+ """
268
+ try:
269
+ # Create an Alipay client instance
270
+ alipay_client = _create_alipay_client()
271
+
272
+ # Create the mobile payment model and set parameters
273
+ model = AlipayTradeWapPayModel()
274
+ model.out_trade_no = args.out_trade_no # Merchant order number
275
+ model.total_amount = str(args.total_amount) # Amount as string
276
+ model.subject = args.order_title # Order title
277
+ model.product_code = "QUICK_WAP_WAY" # Fixed product code
278
+
279
+ # Use custom extend parameters
280
+ extend_params = AgentExtendParams()
281
+ extend_params.request_channel_source = X_AGENT_CHANNEL
282
+ model.extend_params = extend_params
283
+
284
+ # Create the mobile payment request
285
+ request = AlipayTradeWapPayRequest(biz_model=model)
286
+
287
+ # Set callback URL if configured
288
+ if AP_RETURN_URL:
289
+ request.return_url = AP_RETURN_URL
290
+ if AP_NOTIFY_URL:
291
+ request.notify_url = AP_NOTIFY_URL
292
+
293
+ # Execute the request to get the payment link
294
+ response = alipay_client.page_execute(request, http_method="GET")
295
+ return PaymentOutput(
296
+ result=f"支付链接: [点击完成支付]({response})",
297
+ )
298
+
299
+ except (ValueError, ImportError) as e:
300
+ # 配置或SDK错误,直接抛出
301
+ logger.error(f"移动支付配置或SDK错误: {str(e)}")
302
+ raise
303
+ except Exception as e:
304
+ # 其他异常,包装后抛出
305
+ error_msg = f"创建手机支付订单失败: {str(e)}"
306
+ logger.error(f"移动支付执行异常: {error_msg}")
307
+ raise RuntimeError(error_msg) from e
308
+
309
+
310
+ class WebPageAlipayPayment(Tool[WebPagePaymentInput, PaymentOutput]):
311
+ """
312
+ 电脑网页端支付宝支付组件
313
+
314
+ 该组件用于创建适合电脑端浏览器的支付宝支付订单。生成的支付链接在电脑浏览器中
315
+ 打开后会展示支付二维码,用户可以使用支付宝App扫码完成支付。
316
+
317
+ 主要特点:
318
+ - 适用于桌面端网站和电脑客户端
319
+ - 支持二维码扫码支付
320
+ - 使用FAST_INSTANT_TRADE_PAY产品码
321
+ - 返回可直接使用的支付链接
322
+
323
+ 输入参数类型:WebPagePaymentInput
324
+ 输出参数类型:PaymentOutput
325
+
326
+ 使用场景:
327
+ - 电脑端网站支付
328
+ - 桌面应用内嵌支付
329
+ - 需要二维码支付的场景
330
+ """
331
+
332
+ name: str = "alipay_webpage_payment"
333
+ description: str = (
334
+ "创建一笔支付宝订单,返回带有支付链接的 Markdown 文本,"
335
+ "该链接在电脑浏览器中打开后会展示支付二维码,用户可扫码支付。"
336
+ "本工具适用于桌面网站或电脑客户端。"
337
+ )
338
+
339
+ async def _arun(
340
+ self,
341
+ args: WebPagePaymentInput,
342
+ **kwargs: Any,
343
+ ) -> PaymentOutput:
344
+ """
345
+ 创建网页版支付宝支付订单
346
+
347
+ 该方法用于创建适用于电脑浏览器的支付宝支付订单。生成的支付链接在电脑浏览器中
348
+ 打开后会展示二维码,用户可以使用支付宝扫码完成支付。
349
+
350
+ Args:
351
+ args (WebPagePaymentInput): 包含支付参数的输入对象
352
+ - out_trade_no: 商户订单号
353
+ - order_title: 订单标题
354
+ - total_amount: 支付金额(元)
355
+ **kwargs: 额外的关键字参数
356
+
357
+ Returns:
358
+ PaymentOutput: 包含支付链接的Markdown文本输出
359
+
360
+ Raises:
361
+ ValueError: 当配置参数错误时
362
+ ImportError: 当支付宝SDK不可用时
363
+ Exception: 当创建订单过程中发生其他错误时
364
+ """
365
+ try:
366
+ # 创建支付宝客户端实例
367
+ alipay_client = _create_alipay_client()
368
+
369
+ # 创建电脑网站支付模型并设置参数
370
+ model = AlipayTradePagePayModel()
371
+ model.out_trade_no = args.out_trade_no # 商户订单号
372
+ model.total_amount = str(
373
+ args.total_amount,
374
+ ) # 支付金额(转换为字符串)
375
+ model.subject = args.order_title # 订单标题
376
+ model.product_code = "FAST_INSTANT_TRADE_PAY" # 产品码,固定值
377
+
378
+ # 使用自定义的扩展参数类
379
+ extend_params = AgentExtendParams()
380
+ extend_params.request_channel_source = X_AGENT_CHANNEL
381
+ model.extend_params = extend_params
382
+
383
+ # 创建电脑网站支付请求
384
+ request = AlipayTradePagePayRequest(biz_model=model)
385
+
386
+ # 设置回调地址(如果配置了环境变量)
387
+ if AP_RETURN_URL:
388
+ request.return_url = AP_RETURN_URL
389
+ if AP_NOTIFY_URL:
390
+ request.notify_url = AP_NOTIFY_URL
391
+
392
+ # 执行请求获取支付链接
393
+ response = alipay_client.page_execute(request, http_method="GET")
394
+ return PaymentOutput(
395
+ result=f"网页支付链接: [点击完成支付]({response})",
396
+ )
397
+
398
+ except (ValueError, ImportError) as e:
399
+ # Configuration or SDK error
400
+ logger.error(
401
+ f"Mobile payment configuration or SDK error: {str(e)}",
402
+ )
403
+ raise
404
+ except Exception as e:
405
+ # Wrap and raise other exceptions
406
+ error_msg = f"Failed to create mobile payment order: {str(e)}"
407
+ logger.error(f"Mobile payment execution exception: {error_msg}")
408
+ raise RuntimeError(error_msg) from e
409
+
410
+
411
+ class AlipayPaymentQuery(Tool[PaymentQueryInput, PaymentOutput]):
412
+ """
413
+ Alipay Transaction Query Component
414
+
415
+ This component is used to query the current status of an existing
416
+ Alipay transaction order. It can obtain the payment status, transaction
417
+ amount, Alipay transaction number, and other details.
418
+
419
+ Key features:
420
+ - Supports querying by merchant order number
421
+ - Returns detailed transaction status information
422
+ - Supports real-time queries
423
+ - Includes error handling and logging
424
+
425
+ Input type: PaymentQueryInput
426
+ Output type: PaymentOutput
427
+
428
+ Usage scenarios:
429
+ - Query payment status of an order
430
+ - Verify payment results
431
+ - Synchronize order status
432
+ - Confirm status after payment failure
433
+
434
+ ---
435
+ 支付宝交易查询组件
436
+
437
+ 该组件用于查询已创建的支付宝交易订单的当前状态。可以获取订单的支付状态、
438
+ 交易金额、支付宝交易号等详细信息。
439
+
440
+ 主要特点:
441
+ - 支持通过商户订单号查询
442
+ - 返回详细的交易状态信息
443
+ - 支持实时查询
444
+ - 错误处理和日志记录
445
+
446
+ 输入参数类型:PaymentQueryInput
447
+ 输出参数类型:PaymentOutput
448
+
449
+ 使用场景:
450
+ - 查询订单支付状态
451
+ - 验证支付结果
452
+ - 订单状态同步
453
+ - 支付失败后的状态确认
454
+ """
455
+
456
+ name: str = "alipay_query_payment"
457
+ description: str = "查询一笔支付宝订单,并返回带有订单信息的文本。"
458
+
459
+ async def _arun(
460
+ self,
461
+ args: PaymentQueryInput,
462
+ **kwargs: Any,
463
+ ) -> PaymentOutput:
464
+ """
465
+ Query Alipay transaction order status.
466
+
467
+ This method queries an existing Alipay order's current status,
468
+ including payment status, amount, and Alipay transaction number.
469
+
470
+ Args:
471
+ args (PaymentQueryInput): Object containing query parameters
472
+ - out_trade_no: Merchant order number
473
+ **kwargs: Additional keyword arguments
474
+
475
+ Returns:
476
+ PaymentOutput: Text output containing query result information
477
+
478
+ Raises:
479
+ ValueError: If configuration parameters are incorrect
480
+ ImportError: If Alipay SDK is unavailable
481
+ Exception: For any other query errors
482
+
483
+ ---
484
+ 查询支付宝交易订单状态
485
+
486
+ 该方法用于查询已创建的支付宝交易订单的当前状态,包括交易状态、金额、
487
+ 支付宝交易号等信息。
488
+
489
+ Args:
490
+ args (PaymentQueryInput): 包含查询参数的输入对象
491
+ - out_trade_no: 商户订单号
492
+ **kwargs: 额外的关键字参数
493
+
494
+ Returns:
495
+ PaymentOutput: 包含查询结果信息的文本输出
496
+
497
+ Raises:
498
+ ValueError: 当配置参数错误时
499
+ ImportError: 当支付宝SDK不可用时
500
+ Exception: 当查询过程中发生其他错误时
501
+ """
502
+ try:
503
+ # Create an Alipay client instance
504
+ alipay_client = _create_alipay_client()
505
+
506
+ # Create transaction query model
507
+ model = AlipayTradeQueryModel()
508
+ model.out_trade_no = args.out_trade_no # Merchant order number
509
+
510
+ # Set custom extended parameters
511
+ extend_params = AgentExtendParams()
512
+ extend_params.request_channel_source = X_AGENT_CHANNEL
513
+ model.extend_params = extend_params
514
+
515
+ # Create transaction query request
516
+ request = AlipayTradeQueryRequest(biz_model=model)
517
+
518
+ # Execute query request
519
+ response_content = alipay_client.execute(request)
520
+ response = AlipayTradeQueryResponse()
521
+ response.parse_response_content(response_content)
522
+
523
+ # Handle response results
524
+ if response.is_success(): # Query success
525
+ result = (
526
+ f"交易状态: {response.trade_status}, "
527
+ f"交易金额: {response.total_amount}, "
528
+ f"支付宝交易号: {response.trade_no}"
529
+ )
530
+ return PaymentOutput(result=result)
531
+ else: # Query failed
532
+ return PaymentOutput(
533
+ result=f"交易查询失败. 错误信息: {response.msg}",
534
+ )
535
+
536
+ except (ValueError, ImportError) as e:
537
+ # Configuration or SDK error
538
+ logger.error(f"Order query configuration or SDK error: {str(e)}")
539
+ raise
540
+ except Exception as e:
541
+ # Other exceptions with wrapped error message
542
+ error_msg = f"Order query failed: {str(e)}"
543
+ logger.error(f"Order query execution exception: {error_msg}")
544
+ raise RuntimeError(error_msg) from e
545
+
546
+
547
+ class AlipayPaymentRefund(Tool[PaymentRefundInput, PaymentOutput]):
548
+ """
549
+ Alipay Transaction Refund Component
550
+
551
+ This component initiates a refund request for a successfully paid
552
+ Alipay transaction. It supports full and partial refunds as well as
553
+ custom refund reasons.
554
+
555
+ Key features:
556
+ - Supports full and partial refunds
557
+ - Allows specifying refund reasons
558
+ - Idempotent refund requests when repeated
559
+
560
+ Input type: PaymentRefundInput
561
+ Output type: PaymentOutput
562
+
563
+ Usage scenarios:
564
+ - Customer-initiated refund
565
+ - Order cancellation refund
566
+ - After-sales refund processing
567
+ - System-initiated automatic refund
568
+
569
+ ---
570
+ 支付宝交易退款组件
571
+
572
+ 该组件用于对已成功支付的订单发起退款申请。支持全额退款和部分退款,
573
+ 可以指定退款原因和退款请求号。
574
+
575
+ 主要特点:
576
+ - 支持全额和部分退款
577
+ - 支持自定义退款原因
578
+
579
+ 输入参数类型:PaymentRefundInput
580
+ 输出参数类型:PaymentOutput
581
+
582
+ 使用场景:
583
+ - 用户申请退款
584
+ - 订单取消退款
585
+ - 售后处理
586
+ - 系统自动退款
587
+ """
588
+
589
+ name: str = "alipay_refund_payment"
590
+ description: str = "对交易发起退款,并返回退款状态和退款金额"
591
+
592
+ async def _arun(
593
+ self,
594
+ args: PaymentRefundInput,
595
+ **kwargs: Any,
596
+ ) -> PaymentOutput:
597
+ """
598
+ Initiate a refund request for an Alipay transaction.
599
+
600
+ This method initiates a refund request for an already paid Alipay
601
+ order. It supports both partial and full refunds, allows specifying
602
+ refund reason, and uses an idempotency key.
603
+
604
+ Args:
605
+ args (PaymentRefundInput): Object containing refund parameters
606
+ - out_trade_no: Merchant order number
607
+ - refund_amount: Refund amount (yuan)
608
+ - refund_reason: Refund reason (optional)
609
+ - out_request_no: Refund request number (optional;
610
+ generated if not provided)
611
+ **kwargs: Additional keyword arguments
612
+
613
+ Returns:
614
+ PaymentOutput: Text output containing refund result
615
+
616
+ Raises:
617
+ ValueError: If configuration parameters are incorrect
618
+ ImportError: If Alipay SDK is unavailable
619
+ Exception: For any other refund errors
620
+
621
+ ---
622
+ 对支付宝交易订单发起退款
623
+
624
+ 该方法用于对已成功支付的订单发起退款申请。支持部分退款和全额退款,
625
+ 可以指定退款原因和退款请求号。
626
+
627
+ Args:
628
+ args (PaymentRefundInput): 包含退款参数的输入对象
629
+ - out_trade_no: 商户订单号
630
+ - refund_amount: 退款金额(元)
631
+ - refund_reason: 退款原因(可选)
632
+ - out_request_no: 退款请求号(可选,不提供则自动生成)
633
+ **kwargs: 额外的关键字参数
634
+
635
+ Returns:
636
+ PaymentOutput: 包含退款结果信息的文本输出
637
+
638
+ Raises:
639
+ ValueError: 当配置参数错误时
640
+ ImportError: 当支付宝SDK不可用时
641
+ Exception: 当退款过程中发生其他错误时
642
+ """
643
+ out_request_no = args.out_request_no
644
+ if not out_request_no:
645
+ timestamp = int(datetime.now().timestamp())
646
+ out_request_no = f"{args.out_trade_no}_refund_{timestamp}"
647
+
648
+ try:
649
+ # Create an Alipay client instance
650
+ alipay_client = _create_alipay_client()
651
+
652
+ # Create refund model
653
+ model = AlipayTradeRefundModel()
654
+ model.out_trade_no = args.out_trade_no
655
+ model.refund_amount = str(args.refund_amount)
656
+ model.refund_reason = args.refund_reason
657
+ model.out_request_no = out_request_no
658
+
659
+ # Set custom extended parameters
660
+ extend_params = AgentExtendParams()
661
+ extend_params.request_channel_source = X_AGENT_CHANNEL
662
+ model.extend_params = extend_params
663
+
664
+ # Create refund request
665
+ request = AlipayTradeRefundRequest(biz_model=model)
666
+
667
+ # Execute refund request
668
+ response_content = alipay_client.execute(request)
669
+ response = AlipayTradeRefundResponse()
670
+ response.parse_response_content(response_content)
671
+
672
+ if response.is_success():
673
+ if response.fund_change == "Y":
674
+ result = f"退款结果: 退款成功, 退款交易: {response.trade_no}"
675
+ else:
676
+ result = f"退款结果: 重复请求退款幂等成功, " f"退款交易: {response.trade_no}"
677
+ return PaymentOutput(result=result)
678
+ else:
679
+ return PaymentOutput(
680
+ result=f"退款执行失败. 错误信息: {response.msg}",
681
+ )
682
+
683
+ except (ValueError, ImportError) as e:
684
+ # Configuration or SDK error
685
+ logger.error(f"Refund configuration or SDK error: {str(e)}")
686
+ raise
687
+ except Exception as e:
688
+ # Other exceptions with wrapped error message
689
+ error_msg = f"Refund failed: {str(e)}"
690
+ logger.error(f"Refund execution exception: {error_msg}")
691
+ raise RuntimeError(error_msg) from e
692
+
693
+
694
+ class AlipayRefundQuery(Tool[RefundQueryInput, PaymentOutput]):
695
+ """
696
+ Alipay Refund Query Component
697
+
698
+ This component queries the current status of a refund request that has
699
+ been initiated. It can determine if the refund was successful, refund
700
+ amount, and refund status.
701
+
702
+ Key features:
703
+ - Supports querying by merchant order number and refund request number
704
+ - Returns detailed refund status information
705
+
706
+ Input type: RefundQueryInput
707
+ Output type: PaymentOutput
708
+
709
+ Usage scenarios:
710
+ - Query refund processing status
711
+ - Verify refund results
712
+ - Customer support inquiries
713
+
714
+ ---
715
+ 支付宝退款查询组件
716
+
717
+ 该组件用于查询已发起的退款申请的当前状态。可以获取退款是否成功、
718
+ 退款金额、退款状态等详细信息。
719
+
720
+ 主要特点:
721
+ - 支持通过商户订单号和退款请求号查询
722
+ - 返回详细的退款状态信息
723
+
724
+ 输入参数类型:RefundQueryInput
725
+ 输出参数类型:PaymentOutput
726
+
727
+ 使用场景:
728
+ - 查询退款处理状态
729
+ - 验证退款结果
730
+ - 客服查询退款情况
731
+ """
732
+
733
+ name: str = "alipay_query_refund"
734
+ description: str = "查询一笔支付宝退款,并返回退款状态和退款金额"
735
+
736
+ async def _arun(
737
+ self,
738
+ args: RefundQueryInput,
739
+ **kwargs: Any,
740
+ ) -> PaymentOutput:
741
+ """
742
+ Query Alipay refund status.
743
+
744
+ This method queries the current status of a refund request,
745
+ including whether it was successful, the refunded amount,
746
+ and refund status code.
747
+
748
+ Args:
749
+ args (RefundQueryInput): Object containing query parameters
750
+ - out_trade_no: Merchant order number
751
+ - out_request_no: Refund request number
752
+ **kwargs: Additional keyword arguments
753
+
754
+ Returns:
755
+ PaymentOutput: Text output containing refund status result
756
+
757
+ Raises:
758
+ ValueError: If configuration parameters are incorrect
759
+ ImportError: If Alipay SDK is unavailable
760
+ Exception: For any other query errors
761
+
762
+ ---
763
+ 查询支付宝退款状态
764
+
765
+ 该方法用于查询已发起的退款申请的当前状态,包括退款是否成功、
766
+ 退款金额、退款状态等信息。
767
+
768
+ Args:
769
+ args (RefundQueryInput): 包含查询参数的输入对象
770
+ - out_trade_no: 商户订单号
771
+ - out_request_no: 退款请求号
772
+ **kwargs: 额外的关键字参数
773
+
774
+ Returns:
775
+ PaymentOutput: 包含退款查询结果信息的文本输出
776
+
777
+ Raises:
778
+ ValueError: 当配置参数错误时
779
+ ImportError: 当支付宝SDK不可用时
780
+ Exception: 当查询过程中发生其他错误时
781
+ """
782
+ try:
783
+ # Create an Alipay client instance
784
+ alipay_client = _create_alipay_client()
785
+
786
+ # Create fastpay refund query model
787
+ model = AlipayTradeFastpayRefundQueryModel()
788
+ model.out_trade_no = args.out_trade_no # Merchant order number
789
+ model.out_request_no = args.out_request_no # Refund request number
790
+
791
+ # Set custom extended parameters
792
+ extend_params = AgentExtendParams()
793
+ extend_params.request_channel_source = X_AGENT_CHANNEL
794
+ model.extend_params = extend_params
795
+
796
+ # Create refund query request
797
+ request = AlipayTradeFastpayRefundQueryRequest(biz_model=model)
798
+
799
+ # Execute refund query
800
+ response_content = alipay_client.execute(request)
801
+ response = AlipayTradeFastpayRefundQueryResponse()
802
+ response.parse_response_content(response_content)
803
+
804
+ # Process response
805
+ if response.is_success(): # Query success
806
+ if response.refund_status == "REFUND_SUCCESS":
807
+ # Refund succeeded
808
+ result = (
809
+ f"查询到退款成功, 退款交易: {response.trade_no}, "
810
+ f"退款金额: {response.refund_amount}, "
811
+ f"退款状态: {response.refund_status}"
812
+ )
813
+ return PaymentOutput(result=result)
814
+ else:
815
+ # Refund not successful
816
+ return PaymentOutput(
817
+ result=(
818
+ f"未查询到退款成功. " f"退款状态: {response.refund_status}"
819
+ ),
820
+ )
821
+ else:
822
+ # Query failed
823
+ return PaymentOutput(
824
+ result=f"退款查询失败. 错误信息: {response.msg}",
825
+ )
826
+
827
+ except (ValueError, ImportError) as e:
828
+ # Configuration or SDK error
829
+ logger.error(f"Refund query configuration or SDK error: {str(e)}")
830
+ raise
831
+ except Exception as e:
832
+ # Other exceptions with wrapped error message
833
+ error_msg = f"Refund query failed: {str(e)}"
834
+ logger.error(f"Refund query execution exception: {error_msg}")
835
+ raise RuntimeError(error_msg) from e