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,551 @@
1
+ # -*- coding: utf-8 -*-
2
+ # pylint:disable=protected-access, line-too-long
3
+ # mypy: disable-error-code="no-redef"
4
+
5
+ import logging
6
+ import os
7
+ from typing import Any, Optional, Type
8
+
9
+ from pydantic import BaseModel, Field
10
+
11
+ from .base import (
12
+ X_AGENT_CHANNEL,
13
+ _create_alipay_client,
14
+ )
15
+ from ..base import Tool
16
+
17
+
18
+ try:
19
+ from alipay.aop.api.request.AlipayAipaySubscribeStatusCheckRequest import (
20
+ AlipayAipaySubscribeStatusCheckRequest,
21
+ )
22
+ from alipay.aop.api.request.AlipayAipaySubscribePackageInitializeRequest import ( # noqa: E501
23
+ AlipayAipaySubscribePackageInitializeRequest,
24
+ )
25
+ from alipay.aop.api.request.AlipayAipaySubscribeTimesSaveRequest import (
26
+ AlipayAipaySubscribeTimesSaveRequest,
27
+ )
28
+ from alipay.aop.api.response.AlipayAipaySubscribeStatusCheckResponse import ( # noqa: E501
29
+ AlipayAipaySubscribeStatusCheckResponse,
30
+ )
31
+ from alipay.aop.api.response.AlipayAipaySubscribePackageInitializeResponse import ( # noqa: E501
32
+ AlipayAipaySubscribePackageInitializeResponse,
33
+ )
34
+ from alipay.aop.api.response.AlipayAipaySubscribeTimesSaveResponse import (
35
+ AlipayAipaySubscribeTimesSaveResponse,
36
+ )
37
+
38
+ ALIPAY_SDK_AVAILABLE = True
39
+ except ImportError:
40
+ ALIPAY_SDK_AVAILABLE = False
41
+ AlipayAipaySubscribeStatusCheckRequest: Optional[Type[Any]] = None
42
+ AlipayAipaySubscribePackageInitializeRequest: Optional[Type[Any]] = None
43
+ AlipayAipaySubscribeTimesSaveRequest: Optional[Type[Any]] = None
44
+ AlipayAipaySubscribeStatusCheckResponse: Optional[Type[Any]] = None
45
+ AlipayAipaySubscribePackageInitializeResponse: Optional[Type[Any]] = None
46
+ AlipayAipaySubscribeTimesSaveResponse: Optional[Type[Any]] = None
47
+
48
+
49
+ logger = logging.getLogger(__name__)
50
+
51
+ # Subscription plan ID - Set by developer in Alipay subscription management
52
+ SUBSCRIBE_PLAN_ID = os.getenv("SUBSCRIBE_PLAN_ID", "")
53
+ # AI agent name - Used to identify the AI agent
54
+ X_AGENT_NAME = os.getenv("X_AGENT_NAME", "")
55
+ # Subscription usage count - Number of uses deducted after service, default
56
+ # is 1 if not set
57
+ USE_TIMES = int(os.getenv("USE_TIMES", "1"))
58
+
59
+
60
+ class SubscribeStatusCheckInput(BaseModel):
61
+ """subscribe status check input schema."""
62
+
63
+ uuid: str = Field(
64
+ ...,
65
+ description="账户ID",
66
+ )
67
+
68
+
69
+ class SubscribeStatusOutput(BaseModel):
70
+ """subscribe status check output schema."""
71
+
72
+ subscribe_flag: bool = Field(
73
+ ...,
74
+ description="是否订阅,已订阅为true,否则为false",
75
+ )
76
+ subscribe_package: Optional[str] = Field(
77
+ None,
78
+ description="订阅剩余套餐描述",
79
+ )
80
+
81
+
82
+ class SubscribePackageInitializeInput(BaseModel):
83
+ """subscribe package initialize input schema."""
84
+
85
+ uuid: str = Field(
86
+ ...,
87
+ description="账户ID",
88
+ )
89
+
90
+
91
+ class SubscribePackageInitializeOutput(BaseModel):
92
+ """subscribe package initialize output schema."""
93
+
94
+ subscribe_url: Optional[str] = Field(
95
+ None,
96
+ description="订阅链接",
97
+ )
98
+
99
+
100
+ class SubscribeTimesSaveInput(BaseModel):
101
+ """subscribe times save input schema."""
102
+
103
+ uuid: str = Field(
104
+ ...,
105
+ description="账户ID",
106
+ )
107
+ out_request_no: str = Field(
108
+ ...,
109
+ description="外部订单号,用来计次幂等,防止重复扣减订阅次数",
110
+ )
111
+
112
+
113
+ class SubscribeTimesSaveOutput(BaseModel):
114
+ """subscribe times save output schema."""
115
+
116
+ success: bool = Field(
117
+ ...,
118
+ description="计次服务调用是否成功",
119
+ )
120
+
121
+
122
+ class SubscribeCheckOrInitializeInput(BaseModel):
123
+ """subscribe check or initialize input schema."""
124
+
125
+ uuid: str = Field(
126
+ ...,
127
+ description="账户ID",
128
+ )
129
+
130
+
131
+ class SubscribeCheckOrInitializeOutput(BaseModel):
132
+ """subscribe check or initialize output schema."""
133
+
134
+ subscribe_flag: bool = Field(
135
+ ...,
136
+ description="是否订阅,已订阅为true,否则为false",
137
+ )
138
+ subscribe_url: Optional[str] = Field(
139
+ None,
140
+ description="订阅链接,如果未订阅则返回链接",
141
+ )
142
+
143
+
144
+ class AlipaySubscribeStatusCheck(
145
+ Tool[SubscribeStatusCheckInput, SubscribeStatusOutput],
146
+ ):
147
+ """
148
+ Alipay Subscription Status Check Component
149
+
150
+ This component checks whether the user is an active subscriber and returns
151
+ subscription details if valid. Information such as validity period,
152
+ remaining times, etc.
153
+
154
+ Features:
155
+ - Query subscription status of an AI agent
156
+ - Returns detailed membership package info
157
+
158
+ Input type: SubscribeStatusCheckInput
159
+ Output type: SubscribeStatusOutput
160
+
161
+ Usage scenarios:
162
+ - AI Agent subscription payment scenario
163
+
164
+ ---
165
+ 支付宝订阅状态检查组件
166
+
167
+ 功能:
168
+ - 判断用户是否为有效会员
169
+ - 返回有效会员的版本信息
170
+ - 包括有效期、剩余次数等
171
+
172
+ 主要特点:
173
+ - 智能体订阅状态查询
174
+
175
+ 输入参数类型: SubscribeStatusCheckInput
176
+ 输出参数类型: SubscribeStatusOutput
177
+
178
+ 使用场景:
179
+ 智能体订阅付费场景
180
+
181
+ """
182
+
183
+ name: str = "query-alipay-subscription-status"
184
+ description: str = "查询用户订阅状态及套餐详情"
185
+
186
+ async def _arun(
187
+ self,
188
+ args: SubscribeStatusCheckInput,
189
+ **kwargs: Any,
190
+ ) -> SubscribeStatusOutput:
191
+ """Check subscription status."""
192
+ try:
193
+ if not SUBSCRIBE_PLAN_ID:
194
+ raise ValueError(
195
+ "Subscription configuration error: Please set the "
196
+ "SUBSCRIBE_PLAN_ID environment variable",
197
+ )
198
+ # Create Alipay client instance
199
+ alipay_client = _create_alipay_client()
200
+
201
+ # Create subscription status check request
202
+ request = AlipayAipaySubscribeStatusCheckRequest()
203
+ biz_content = {
204
+ "uuid": args.uuid,
205
+ "plan_id": SUBSCRIBE_PLAN_ID,
206
+ "channel": X_AGENT_CHANNEL,
207
+ }
208
+ request.biz_content = biz_content
209
+ response_content = alipay_client.execute(request)
210
+ response = AlipayAipaySubscribeStatusCheckResponse()
211
+ response.parse_response_content(response_content)
212
+ if response.is_success:
213
+ is_subscribed = response.data.member_status == "VALID"
214
+ subscribe_package_desc = None
215
+
216
+ if is_subscribed and hasattr(
217
+ response.data,
218
+ "subscribe_member_info_d_t_o",
219
+ ):
220
+ info = response.data.subscribe_member_info_d_t_o
221
+ package_type = info.subscribe_package_type
222
+
223
+ if package_type == "byCount":
224
+ # Count-based subscription: subscribed X times,
225
+ # Y times remaining
226
+ total_times = info.subscribe_times
227
+ left_times = info.left_times
228
+ subscribe_package_desc = (
229
+ f"订阅{total_times}次,还剩{left_times}次"
230
+ )
231
+ elif package_type == "byTime":
232
+ # Time-based subscription: expires after expiration
233
+ # date
234
+ expired_date = info.expired_date
235
+ subscribe_package_desc = f"{expired_date}后过期"
236
+ return SubscribeStatusOutput(
237
+ subscribe_flag=is_subscribed,
238
+ subscribe_package=subscribe_package_desc,
239
+ )
240
+ else:
241
+ error_msg = (
242
+ f"Subscription check API returned an error: "
243
+ f"{response.sub_code or response.code} - "
244
+ f"{response.sub_msg or response.msg}"
245
+ )
246
+ logger.error(error_msg)
247
+ return SubscribeStatusOutput(
248
+ subscribe_flag=False,
249
+ subscribe_package=None,
250
+ )
251
+
252
+ except ImportError:
253
+ logger.error(
254
+ "Please install the official Alipay SDK: pip install "
255
+ "alipay-sdk-python",
256
+ )
257
+ return SubscribeStatusOutput(
258
+ subscribe_flag=False,
259
+ subscribe_package=None,
260
+ )
261
+ except Exception as e:
262
+ logger.error(f"Failed to check subscription status: {str(e)}")
263
+ return SubscribeStatusOutput(
264
+ subscribe_flag=False,
265
+ subscribe_package=None,
266
+ )
267
+
268
+
269
+ class AlipaySubscribePackageInitialize(
270
+ Tool[
271
+ SubscribePackageInitializeInput,
272
+ SubscribePackageInitializeOutput,
273
+ ],
274
+ ):
275
+ """
276
+ Alipay Subscription Package Initialize Component
277
+
278
+ This component returns a purchase link for the subscription package
279
+ along with pricing configuration info.
280
+
281
+ Features:
282
+ - AI Agent subscription initialization
283
+
284
+ Input type: SubscribePackageInitializeInput
285
+ Output type: SubscribePackageInitializeOutput
286
+
287
+ Usage scenarios:
288
+ - AI Agent subscription payment scenario
289
+
290
+ ---
291
+ 支付宝订阅开通组件
292
+
293
+ 功能:
294
+ - 返回订阅套餐的购买链接
295
+ - 订阅计划的定价配置信息
296
+
297
+ 主要特点:
298
+ - 智能体订阅开通
299
+
300
+ 输入参数类型: SubscribePackageInitializeInput
301
+ 输出参数类型: SubscribePackageInitializeOutput
302
+
303
+ 使用场景:
304
+ 智能体订阅付费场景
305
+
306
+ """
307
+
308
+ name: str = "initialize-alipay-subscription-order"
309
+ description: str = "用户发起订阅付费,返回订阅链接"
310
+
311
+ async def _arun(
312
+ self,
313
+ args: SubscribePackageInitializeInput,
314
+ **kwargs: Any,
315
+ ) -> SubscribePackageInitializeOutput:
316
+ """Initialize subscription package."""
317
+ try:
318
+ if not SUBSCRIBE_PLAN_ID or not X_AGENT_NAME:
319
+ raise ValueError(
320
+ "Subscription config error: set SUBSCRIBE_PLAN_ID and "
321
+ "X_AGENT_NAME env variables.",
322
+ )
323
+ # Create Alipay client instance
324
+ alipay_client = _create_alipay_client()
325
+
326
+ # Create subscription initialize request
327
+ request = AlipayAipaySubscribePackageInitializeRequest()
328
+ biz_content = {
329
+ "uuid": args.uuid,
330
+ "plan_id": SUBSCRIBE_PLAN_ID,
331
+ "channel": X_AGENT_CHANNEL,
332
+ "agent_name": X_AGENT_NAME,
333
+ }
334
+ request.biz_content = biz_content
335
+ response_content = alipay_client.execute(request)
336
+ response = AlipayAipaySubscribePackageInitializeResponse()
337
+ response.parse_response_content(response_content)
338
+ if response.is_success:
339
+ return SubscribePackageInitializeOutput(
340
+ subscribe_url=response.data.subscribe_url,
341
+ )
342
+ else:
343
+ error_msg = (
344
+ f"Subscription init API error:: "
345
+ f"{response.sub_code or response.code} - "
346
+ f"{response.sub_msg or response.msg}"
347
+ )
348
+ logger.error(error_msg)
349
+ return SubscribePackageInitializeOutput(subscribe_url=None)
350
+
351
+ except ImportError:
352
+ logger.error(
353
+ "Alipay SDK not installed: pip install alipay-sdk-python",
354
+ )
355
+ return SubscribePackageInitializeOutput(subscribe_url=None)
356
+ except Exception as e:
357
+ logger.error(f"Subscription init failed: {str(e)}")
358
+ return SubscribePackageInitializeOutput(subscribe_url=None)
359
+
360
+
361
+ class AlipaySubscribeTimesSave(
362
+ Tool[
363
+ SubscribeTimesSaveInput,
364
+ SubscribeTimesSaveOutput,
365
+ ],
366
+ ):
367
+ """
368
+ Alipay Subscription Times Save Component
369
+
370
+ This component records the usage count for count-based subscription
371
+ billing models.
372
+
373
+ Features:
374
+ - AI Agent subscription usage count tracking
375
+
376
+ Input type: SubscribeTimesSaveInput
377
+ Output type: SubscribeTimesSaveOutput
378
+
379
+ Usage scenarios:
380
+ - Count-based subscription scenario
381
+
382
+ ---
383
+ 支付宝订阅计次组件
384
+
385
+ 功能:
386
+ - 针对按次付费的计费模式,记录会员用户消耗的使用次数。
387
+
388
+ 主要特点:
389
+ - 智能体订阅计次
390
+
391
+ 输入参数类型: SubscribeTimesSaveInput
392
+ 输出参数类型: SubscribeTimesSaveOutput
393
+
394
+ 使用场景:
395
+ 智能体订阅计次场景
396
+
397
+ """
398
+
399
+ name: str = "times-alipay-subscription-consume"
400
+ description: str = "用户使用服务后,记录用户使用消耗的次数"
401
+
402
+ async def _arun(
403
+ self,
404
+ args: SubscribeTimesSaveInput,
405
+ **kwargs: Any,
406
+ ) -> SubscribeTimesSaveOutput:
407
+ """Save subscription usage times."""
408
+ try:
409
+ if not SUBSCRIBE_PLAN_ID:
410
+ raise ValueError(
411
+ "Subscription configuration error: Please set the "
412
+ "SUBSCRIBE_PLAN_ID environment variable",
413
+ )
414
+ # Create Alipay client instance
415
+ alipay_client = _create_alipay_client()
416
+
417
+ # Create subscription times save request
418
+ request = AlipayAipaySubscribeTimesSaveRequest()
419
+ biz_content = {
420
+ "uuid": args.uuid,
421
+ "plan_id": SUBSCRIBE_PLAN_ID,
422
+ "use_times": USE_TIMES,
423
+ "channel": X_AGENT_CHANNEL,
424
+ "out_request_no": args.out_request_no,
425
+ }
426
+ request.biz_content = biz_content
427
+ response_content = alipay_client.execute(request)
428
+ response = AlipayAipaySubscribeTimesSaveResponse()
429
+ response.parse_response_content(response_content)
430
+ if response.is_success:
431
+ return SubscribeTimesSaveOutput(
432
+ success=response.data.count_success,
433
+ )
434
+ else:
435
+ error_msg = (
436
+ f"Times save API error:"
437
+ f"{response.sub_code or response.code} - "
438
+ f"{response.sub_msg or response.msg}"
439
+ )
440
+ logger.error(error_msg)
441
+ return SubscribeTimesSaveOutput(success=False)
442
+
443
+ except ImportError:
444
+ logger.error(
445
+ "Alipay SDK not installed: pip install alipay-sdk-python",
446
+ )
447
+ return SubscribeTimesSaveOutput(success=False)
448
+ except Exception as e:
449
+ logger.error(f"Subscription times save failed: {str(e)}")
450
+ return SubscribeTimesSaveOutput(success=False)
451
+
452
+
453
+ class AlipaySubscribeCheckOrInitialize(
454
+ Tool[
455
+ SubscribeCheckOrInitializeInput,
456
+ SubscribeCheckOrInitializeOutput,
457
+ ],
458
+ ):
459
+ """
460
+ Alipay Subscription Check or Initialize Component
461
+
462
+ This component checks subscription status for count-based billing mode
463
+ and initializes subscription if not already active.
464
+
465
+ Features:
466
+ - Verify user subscription status and return state if active
467
+ - If not active, return subscription link
468
+
469
+ Input type: SubscribeCheckOrInitializeInput
470
+ Output type: SubscribeCheckOrInitializeOutput
471
+
472
+ Usage scenarios:
473
+ - Count-based subscription validation or initialization
474
+
475
+ ---
476
+ 支付宝订阅检查或初始化组件
477
+
478
+ 功能:
479
+ - 针对按次付费的计费模式,进行订阅状态检查或初始化。
480
+
481
+ 主要特点:
482
+ - 提供校验用户状态能力,如果已订阅直接返回状态,如果未订阅返回相关链接
483
+
484
+ 输入参数类型: SubscribeCheckOrInitializeInput
485
+ 输出参数类型: SubscribeCheckOrInitializeOutput
486
+
487
+ 使用场景:
488
+ 智能体订阅检查或初始化场景
489
+
490
+ """
491
+
492
+ name: str = "alipay_subscribe_check_or_initialize"
493
+ description: str = "检查用户订阅状态,如果已订阅则返回状态,如果未订阅则返回订阅链接"
494
+
495
+ async def _arun(
496
+ self,
497
+ args: SubscribeCheckOrInitializeInput,
498
+ **kwargs: Any,
499
+ ) -> SubscribeCheckOrInitializeOutput:
500
+ """Check subscription status or initialize if not subscribed."""
501
+ try:
502
+ if not SUBSCRIBE_PLAN_ID or not X_AGENT_NAME:
503
+ raise ValueError(
504
+ "Subscription config error: set SUBSCRIBE_PLAN_ID and "
505
+ "X_AGENT_NAME env variables.",
506
+ )
507
+ # First, check subscription status
508
+ status_check = AlipaySubscribeStatusCheck()
509
+ status_input = SubscribeStatusCheckInput(
510
+ uuid=args.uuid,
511
+ plan_id=SUBSCRIBE_PLAN_ID,
512
+ channel=X_AGENT_CHANNEL,
513
+ )
514
+ status_output = await status_check._arun(status_input)
515
+
516
+ # If subscribed, return status
517
+ if status_output.subscribe_flag:
518
+ return SubscribeCheckOrInitializeOutput(
519
+ subscribe_flag=True,
520
+ subscribe_url=None,
521
+ )
522
+
523
+ # If not subscribed, initialize
524
+ init_component = AlipaySubscribePackageInitialize()
525
+ init_input = SubscribePackageInitializeInput(
526
+ uuid=args.uuid,
527
+ plan_id=SUBSCRIBE_PLAN_ID,
528
+ channel=X_AGENT_CHANNEL,
529
+ agent_name=X_AGENT_NAME,
530
+ )
531
+ init_result = await init_component._arun(init_input)
532
+
533
+ return SubscribeCheckOrInitializeOutput(
534
+ subscribe_flag=False,
535
+ subscribe_url=init_result.subscribe_url,
536
+ )
537
+
538
+ except ImportError:
539
+ logger.error(
540
+ "Alipay SDK not installed: pip install alipay-sdk-python",
541
+ )
542
+ return SubscribeCheckOrInitializeOutput(
543
+ subscribe_flag=False,
544
+ subscribe_url=None,
545
+ )
546
+ except Exception as e:
547
+ logger.error(f"Subscription check or init failed: {str(e)}")
548
+ return SubscribeCheckOrInitializeOutput(
549
+ subscribe_flag=False,
550
+ subscribe_url=None,
551
+ )