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,233 @@
1
+ # -*- coding: utf-8 -*-
2
+ import concurrent.futures
3
+ import json
4
+ from typing import (
5
+ Any,
6
+ Dict,
7
+ Optional,
8
+ Sequence,
9
+ )
10
+
11
+ from agentscope.tool import Toolkit, ToolResponse
12
+ from agentscope.tool._registered_tool_function import (
13
+ RegisteredToolFunction,
14
+ )
15
+
16
+ from agentscope_runtime.tools.base import Tool
17
+
18
+
19
+ def agentscope_tool_adapter(
20
+ tool: Tool,
21
+ name: Optional[str] = None,
22
+ description: Optional[str] = None,
23
+ ) -> RegisteredToolFunction:
24
+ """Convert an agentscope_runtime tool to an AgentScope tool.
25
+
26
+ This function wraps agentscope_runtime tools to make them compatible
27
+ with AgentScope's toolkit system.
28
+
29
+ Args:
30
+ tool (Tool): The agentscope_runtime tool to wrap
31
+ name (str, optional): Override the tool name. Defaults to
32
+ tool.name
33
+ description (str, optional): Override the tool description.
34
+ Defaults to tool.description
35
+
36
+ Returns:
37
+ RegisteredToolFunction: The AgentScope tool function
38
+
39
+ Examples:
40
+ Basic usage with a search tool:
41
+
42
+ .. code-block:: python
43
+
44
+ from agentscope_runtime.searches.modelstudio_search
45
+ import ModelstudioSearch
46
+ from agentscope_runtime.adapters.agentscope.tool import
47
+ agentscope_tool_adapter
48
+ from agentscope.tool import Toolkit
49
+
50
+ # Create the search tool
51
+ search_tool = ModelstudioSearch()
52
+
53
+ # Convert to AgentScope tool
54
+ search_tool = agentscope_tool_adapter(search_tool)
55
+
56
+ # Add to toolkit
57
+ toolkit = Toolkit()
58
+ toolkit.tools[search_tool.name] = search_tool
59
+ """
60
+
61
+ def func_wrapper(**kwargs: Any) -> ToolResponse:
62
+ """Wrapper function that adapts tool execution to AgentScope
63
+ format."""
64
+ import asyncio
65
+
66
+ # Validate input with tool's input type
67
+ if tool.input_type:
68
+ try:
69
+ validated_input = tool.input_type.model_validate(kwargs)
70
+ except Exception as e:
71
+ return ToolResponse(
72
+ content=[
73
+ {
74
+ "type": "text",
75
+ "text": f"Input validation error: {str(e)}",
76
+ },
77
+ ],
78
+ metadata={"error": True},
79
+ )
80
+ else:
81
+ validated_input = kwargs
82
+
83
+ # Execute the tool
84
+ try:
85
+ if asyncio.iscoroutinefunction(tool.arun):
86
+ # Check if we're already in an event loop
87
+ try:
88
+
89
+ def run_async() -> Any:
90
+ return asyncio.run(tool.arun(validated_input))
91
+
92
+ with concurrent.futures.ThreadPoolExecutor() as executor:
93
+ future = executor.submit(run_async)
94
+ result = future.result()
95
+ except RuntimeError:
96
+ # No event loop running, safe to use asyncio.run
97
+ result = asyncio.run(tool.arun(validated_input))
98
+ else:
99
+ # Run sync tool
100
+ result = tool.run(validated_input)
101
+ except Exception as e:
102
+ return ToolResponse(
103
+ content=[
104
+ {
105
+ "type": "text",
106
+ "text": f"Tool execution error: {str(e)}",
107
+ },
108
+ ],
109
+ metadata={"error": True},
110
+ )
111
+
112
+ # Convert result to ToolResponse format
113
+ try:
114
+ if hasattr(result, "model_dump"):
115
+ # Pydantic model result
116
+ result_dict = result.model_dump()
117
+ content_text = json.dumps(
118
+ result_dict,
119
+ ensure_ascii=False,
120
+ indent=2,
121
+ )
122
+ else:
123
+ # Other result types
124
+ content_text = str(result)
125
+ result_dict = result
126
+
127
+ return ToolResponse(
128
+ content=[
129
+ {
130
+ "type": "text",
131
+ "text": content_text,
132
+ },
133
+ ],
134
+ metadata={"tool_result": result_dict},
135
+ )
136
+ except Exception as e:
137
+ return ToolResponse(
138
+ content=[
139
+ {
140
+ "type": "text",
141
+ "text": f"Result formatting error: {str(e)}",
142
+ },
143
+ ],
144
+ metadata={"error": True},
145
+ )
146
+
147
+ # Use provided name/description or fall back to tool defaults
148
+ tool_name = name or tool.name
149
+ tool_description = description or tool.description
150
+
151
+ # Get the tool's function schema and convert to AgentScope format
152
+ function_schema = tool.function_schema.model_dump()
153
+
154
+ # Convert from OpenAI function calling format to AgentScope format
155
+ agentscope_schema = {
156
+ "type": "function",
157
+ "function": {
158
+ "name": tool_name,
159
+ "description": tool_description,
160
+ "parameters": function_schema.get("parameters", {}),
161
+ },
162
+ }
163
+
164
+ return RegisteredToolFunction(
165
+ name=tool_name,
166
+ source="function",
167
+ mcp_name=None,
168
+ original_func=func_wrapper,
169
+ json_schema=agentscope_schema,
170
+ group="basic",
171
+ )
172
+
173
+
174
+ def agentscope_toolkit_adapter(
175
+ tools: Sequence[Tool],
176
+ name_overrides: Optional[Dict[str, str]] = None,
177
+ description_overrides: Optional[Dict[str, str]] = None,
178
+ ) -> Toolkit:
179
+ """Create an AgentScope toolkit from multiple agentscope_runtime tools.
180
+
181
+ This is a convenience function that creates a toolkit with multiple
182
+ tools converted to AgentScope tools.
183
+
184
+ Args:
185
+ tools: Sequence of agentscope_runtime tools to convert
186
+ name_overrides: Optional dict mapping tool names to override names
187
+ description_overrides: Optional dict mapping tool names to
188
+ override descriptions
189
+
190
+ Returns:
191
+ Toolkit: AgentScope toolkit with all tools as tools
192
+
193
+ Examples:
194
+ Create toolkit from multiple tools:
195
+
196
+ .. code-block:: python
197
+
198
+ from agentscope_runtime.searches.modelstudio_search
199
+ import ModelstudioSearch
200
+ from agentscope_runtime.RAGs.modelstudio_rag import
201
+ ModelstudioRag
202
+ from agentscope_runtime.adapters.agentscope.tool import
203
+ agentscope_tool_adapter
204
+ from agentscope.tool import Toolkit
205
+
206
+ # Create the search tool
207
+ search_tool = ModelstudioSearch()
208
+ rag_tool = ModelstudioRag()
209
+
210
+ # Create toolkit
211
+ toolkit = agentscope_toolkit_adapter([search_tool,
212
+ rag_tool])
213
+
214
+ # Use in agents...
215
+ """
216
+ name_overrides = name_overrides or {}
217
+ description_overrides = description_overrides or {}
218
+
219
+ toolkit = Toolkit()
220
+
221
+ for tool in tools:
222
+ name_override = name_overrides.get(tool.name)
223
+ description_override = description_overrides.get(tool.name)
224
+
225
+ adapted_tool = agentscope_tool_adapter(
226
+ tool,
227
+ name=name_override,
228
+ description=description_override,
229
+ )
230
+
231
+ toolkit.tools[tool.name] = adapted_tool
232
+
233
+ return toolkit
File without changes
@@ -0,0 +1,7 @@
1
+ # -*- coding: utf-8 -*-
2
+ from .tool import AutogenToolAdapter, create_autogen_tools
3
+
4
+ __all__ = [
5
+ "AutogenToolAdapter",
6
+ "create_autogen_tools",
7
+ ]
@@ -0,0 +1,211 @@
1
+ # -*- coding: utf-8 -*-
2
+ # pylint: disable=unused-argument, redefined-outer-name
3
+
4
+ import json
5
+ from typing import (
6
+ Any,
7
+ Dict,
8
+ List,
9
+ Optional,
10
+ Sequence,
11
+ )
12
+
13
+ try:
14
+ from autogen_core import CancellationToken
15
+ from autogen_core.tools import BaseTool
16
+ except ImportError as e:
17
+ # Create mock classes when autogen is not available
18
+ raise ImportError(
19
+ "Please install autogen-core to use this feature: "
20
+ "pip install autogen-core",
21
+ ) from e
22
+
23
+ from pydantic import BaseModel
24
+
25
+ from agentscope_runtime.tools.base import Tool
26
+
27
+
28
+ class AutogenToolAdapter(BaseTool[BaseModel, Any]):
29
+ """Adapter class that wraps agentscope_runtime tools to make them
30
+ compatible with AutoGen.
31
+
32
+ This adapter allows any tool that inherits from
33
+ agentscope_runtime.tools.base.Tool to be used as a tool in
34
+ AutoGen agents
35
+
36
+ Args:
37
+ tool (Tool): The agentscope_runtime tool to wrap
38
+ name (str, optional): Override the tool name. Defaults to
39
+ tool.name
40
+ description (str, optional): Override the tool description.
41
+ Defaults to tool.description
42
+
43
+ Examples:
44
+ Basic usage with a search tool:
45
+
46
+ .. code-block:: python
47
+
48
+ from agentscope_runtime.tools.searches.modelstudio_search
49
+ import ModelstudioSearch
50
+ from agentscope_runtime.adapters.autogen.tool import
51
+ AutogenToolAdapter
52
+ from autogen_ext.models.openai import OpenAIChatCompletionClient
53
+ from autogen_agentchat.agents import AssistantAgent
54
+ from autogen_agentchat.messages import TextMessage
55
+ from autogen_core import CancellationToken
56
+
57
+ async def main():
58
+ # Create the search tool
59
+ search_tool = ModelstudioSearch()
60
+
61
+ # Create the autogen tool adapter
62
+ search_tool = AutogenToolAdapter(search_tool)
63
+
64
+ # Create an agents with the search tool
65
+ model = OpenAIChatCompletionClient(model="gpt-4")
66
+ agents = AssistantAgent(
67
+ "assistant",
68
+ tools=[search_tool],
69
+ model_client=model,
70
+ )
71
+
72
+ # Use the agents
73
+ response = await agents.on_messages(
74
+ [TextMessage(content="What's the weather in Beijing?",
75
+ source="user")],
76
+ CancellationToken(),
77
+ )
78
+ print(response.chat_message)
79
+
80
+ asyncio.run(main())
81
+ """
82
+
83
+ def __init__(
84
+ self,
85
+ tool: Tool,
86
+ name: Optional[str] = None,
87
+ description: Optional[str] = None,
88
+ ) -> None:
89
+ """Initialize the tool tool adapter.
90
+
91
+ Args:
92
+ tool: The agentscope_runtime tool to wrap
93
+ name: Optional override for the tool name
94
+ description: Optional override for the tool description
95
+ """
96
+
97
+ self._tool = tool
98
+
99
+ # Use provided name/description or fall back to tool defaults
100
+ tool_name = name or tool.name
101
+ tool_description = description or tool.description
102
+
103
+ # Create input model from tool's input type
104
+ arg_type = tool.input_type
105
+ return_type = tool.return_type
106
+
107
+ super().__init__(arg_type, return_type, tool_name, tool_description)
108
+
109
+ async def run(
110
+ self,
111
+ args: BaseModel,
112
+ cancellation_token: CancellationToken,
113
+ ) -> Any:
114
+ """Run the tool with the provided arguments.
115
+
116
+ Args:
117
+ args: The arguments to pass to the tool
118
+ cancellation_token: Token to signal cancellation
119
+
120
+ Returns:
121
+ The result of the tool execution
122
+
123
+ Raises:
124
+ Exception: If the operation is cancelled or the tool
125
+ execution fails
126
+ """
127
+
128
+ # Run the tool
129
+ try:
130
+ result = await self._tool.arun(args)
131
+ # make sure return as string
132
+ return json.dumps(result.model_dump(), ensure_ascii=False)
133
+ except Exception as e:
134
+ # Re-raise with more context
135
+ raise RuntimeError(
136
+ f"Tool {self._tool.name} failed: {str(e)}",
137
+ ) from e
138
+
139
+
140
+ def create_autogen_tools(
141
+ tools: Sequence[Tool],
142
+ name_overrides: Optional[Dict[str, str]] = None,
143
+ description_overrides: Optional[Dict[str, str]] = None,
144
+ ) -> List[AutogenToolAdapter]:
145
+ """Create a list of tool adapters for use with AutoGen agents.
146
+
147
+ This is a convenience function that creates adapters for multiple
148
+ tools at once, similar to how tool.py provides
149
+ LanggraphNode.
150
+
151
+ Args:
152
+ tools: Sequence of agentscope_runtime tools to wrap
153
+ name_overrides: Optional dict mapping tool names to override names
154
+ description_overrides: Optional dict mapping tool names to
155
+ override descriptions
156
+
157
+
158
+ Returns:
159
+ List of Tool instances ready to use with AutoGen agents
160
+
161
+ Examples:
162
+ Create tools from multiple tools:
163
+
164
+ .. code-block:: python
165
+
166
+ from agentscope_runtime.searches.modelstudio_search
167
+ import ModelstudioSearch
168
+ from agentscope_runtime.RAGs.modelstudio_rag import
169
+ ModelstudioRag
170
+ from agentscope_runtime.adapters.autogen.tool import
171
+ AutogenToolAdapter
172
+ from autogen_ext.models.openai import OpenAIChatCompletionClient
173
+ from autogen_agentchat.agents import AssistantAgent
174
+
175
+ async def main():
176
+ # Create tools
177
+ search_tool = ModelstudioSearch()
178
+ rag_tool = ModelstudioRag()
179
+
180
+ # Create autogen tools
181
+ tools = create_autogen_tools([search_tool,
182
+ rag_tool])
183
+
184
+ # Create agents with all tools
185
+ model = OpenAIChatCompletionClient(model="gpt-4")
186
+ agents = AssistantAgent(
187
+ "assistant",
188
+ tools=tools,
189
+ model_client=model,
190
+ )
191
+
192
+ # Use the agents...
193
+
194
+ asyncio.run(main())
195
+ """
196
+ name_overrides = name_overrides or {}
197
+ description_overrides = description_overrides or {}
198
+
199
+ output_tools = []
200
+ for tool in tools:
201
+ name_override = name_overrides.get(tool.name)
202
+ description_override = description_overrides.get(tool.name)
203
+
204
+ adapted_tool = AutogenToolAdapter(
205
+ tool,
206
+ name=name_override,
207
+ description=description_override,
208
+ )
209
+ output_tools.append(adapted_tool)
210
+
211
+ return output_tools
File without changes
@@ -0,0 +1,29 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from typing import AsyncIterator
4
+
5
+ from ...engine.schemas.agent_schemas import (
6
+ Message,
7
+ MessageType,
8
+ )
9
+ from ...engine.helpers.agent_api_builder import ResponseBuilder
10
+
11
+
12
+ async def adapt_text_stream(
13
+ source_stream: AsyncIterator[str],
14
+ ) -> AsyncIterator[Message]:
15
+ rb = ResponseBuilder()
16
+ mb = rb.create_message_builder(
17
+ role="assistant",
18
+ message_type=MessageType.MESSAGE,
19
+ )
20
+ cb = mb.create_content_builder(content_type="text")
21
+
22
+ async for text_delta in source_stream:
23
+ delta_content = cb.add_text_delta(text_delta)
24
+ yield delta_content
25
+
26
+ cb.complete()
27
+ yield mb.complete()
28
+
29
+ rb.completed()
@@ -32,7 +32,7 @@ class RedisMapping(Mapping):
32
32
  def scan(self, prefix: str = ""):
33
33
  search_pattern = f"{self._get_full_key(prefix)}*"
34
34
  cursor = 0
35
- while cursor != 0:
35
+ while True:
36
36
  cursor, keys = self.client.scan(
37
37
  cursor=cursor,
38
38
  match=search_pattern,
@@ -40,3 +40,6 @@ class RedisMapping(Mapping):
40
40
  for key in keys:
41
41
  decoded_key = key.decode("utf-8")
42
42
  yield self._strip_prefix(decoded_key)
43
+
44
+ if cursor == 0:
45
+ break