agentscope-runtime 1.0.4a1__py3-none-any.whl → 1.0.5__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 (79) hide show
  1. agentscope_runtime/adapters/agentscope/stream.py +2 -8
  2. agentscope_runtime/adapters/langgraph/stream.py +120 -70
  3. agentscope_runtime/adapters/ms_agent_framework/__init__.py +0 -0
  4. agentscope_runtime/adapters/ms_agent_framework/message.py +205 -0
  5. agentscope_runtime/adapters/ms_agent_framework/stream.py +418 -0
  6. agentscope_runtime/adapters/utils.py +6 -0
  7. agentscope_runtime/cli/commands/deploy.py +836 -1
  8. agentscope_runtime/cli/commands/stop.py +16 -0
  9. agentscope_runtime/common/container_clients/__init__.py +52 -0
  10. agentscope_runtime/common/container_clients/agentrun_client.py +6 -4
  11. agentscope_runtime/common/container_clients/boxlite_client.py +442 -0
  12. agentscope_runtime/common/container_clients/docker_client.py +0 -20
  13. agentscope_runtime/common/container_clients/fc_client.py +6 -4
  14. agentscope_runtime/common/container_clients/gvisor_client.py +38 -0
  15. agentscope_runtime/common/container_clients/knative_client.py +467 -0
  16. agentscope_runtime/common/utils/deprecation.py +164 -0
  17. agentscope_runtime/engine/__init__.py +4 -0
  18. agentscope_runtime/engine/app/agent_app.py +16 -4
  19. agentscope_runtime/engine/constant.py +1 -0
  20. agentscope_runtime/engine/deployers/__init__.py +34 -11
  21. agentscope_runtime/engine/deployers/adapter/__init__.py +8 -0
  22. agentscope_runtime/engine/deployers/adapter/a2a/__init__.py +26 -51
  23. agentscope_runtime/engine/deployers/adapter/a2a/a2a_protocol_adapter.py +23 -13
  24. agentscope_runtime/engine/deployers/adapter/a2a/a2a_registry.py +4 -201
  25. agentscope_runtime/engine/deployers/adapter/a2a/nacos_a2a_registry.py +152 -25
  26. agentscope_runtime/engine/deployers/adapter/agui/__init__.py +8 -0
  27. agentscope_runtime/engine/deployers/adapter/agui/agui_adapter_utils.py +652 -0
  28. agentscope_runtime/engine/deployers/adapter/agui/agui_protocol_adapter.py +225 -0
  29. agentscope_runtime/engine/deployers/agentrun_deployer.py +2 -2
  30. agentscope_runtime/engine/deployers/fc_deployer.py +1506 -0
  31. agentscope_runtime/engine/deployers/knative_deployer.py +290 -0
  32. agentscope_runtime/engine/deployers/pai_deployer.py +2335 -0
  33. agentscope_runtime/engine/deployers/utils/net_utils.py +37 -0
  34. agentscope_runtime/engine/deployers/utils/oss_utils.py +38 -0
  35. agentscope_runtime/engine/deployers/utils/package.py +46 -42
  36. agentscope_runtime/engine/helpers/agent_api_client.py +372 -0
  37. agentscope_runtime/engine/runner.py +13 -0
  38. agentscope_runtime/engine/schemas/agent_schemas.py +9 -3
  39. agentscope_runtime/engine/services/agent_state/__init__.py +7 -0
  40. agentscope_runtime/engine/services/memory/__init__.py +7 -0
  41. agentscope_runtime/engine/services/memory/redis_memory_service.py +15 -16
  42. agentscope_runtime/engine/services/session_history/__init__.py +7 -0
  43. agentscope_runtime/engine/tracing/local_logging_handler.py +2 -3
  44. agentscope_runtime/engine/tracing/wrapper.py +18 -4
  45. agentscope_runtime/sandbox/__init__.py +14 -6
  46. agentscope_runtime/sandbox/box/base/__init__.py +2 -2
  47. agentscope_runtime/sandbox/box/base/base_sandbox.py +51 -1
  48. agentscope_runtime/sandbox/box/browser/__init__.py +2 -2
  49. agentscope_runtime/sandbox/box/browser/browser_sandbox.py +198 -2
  50. agentscope_runtime/sandbox/box/filesystem/__init__.py +2 -2
  51. agentscope_runtime/sandbox/box/filesystem/filesystem_sandbox.py +99 -2
  52. agentscope_runtime/sandbox/box/gui/__init__.py +2 -2
  53. agentscope_runtime/sandbox/box/gui/gui_sandbox.py +117 -1
  54. agentscope_runtime/sandbox/box/mobile/__init__.py +2 -2
  55. agentscope_runtime/sandbox/box/mobile/mobile_sandbox.py +247 -100
  56. agentscope_runtime/sandbox/box/sandbox.py +102 -65
  57. agentscope_runtime/sandbox/box/shared/routers/generic.py +36 -29
  58. agentscope_runtime/sandbox/client/__init__.py +6 -1
  59. agentscope_runtime/sandbox/client/async_http_client.py +339 -0
  60. agentscope_runtime/sandbox/client/base.py +74 -0
  61. agentscope_runtime/sandbox/client/http_client.py +108 -329
  62. agentscope_runtime/sandbox/enums.py +7 -0
  63. agentscope_runtime/sandbox/manager/sandbox_manager.py +275 -29
  64. agentscope_runtime/sandbox/manager/server/app.py +7 -1
  65. agentscope_runtime/sandbox/manager/server/config.py +3 -1
  66. agentscope_runtime/sandbox/model/manager_config.py +11 -9
  67. agentscope_runtime/tools/modelstudio_memory/__init__.py +106 -0
  68. agentscope_runtime/tools/modelstudio_memory/base.py +220 -0
  69. agentscope_runtime/tools/modelstudio_memory/config.py +86 -0
  70. agentscope_runtime/tools/modelstudio_memory/core.py +594 -0
  71. agentscope_runtime/tools/modelstudio_memory/exceptions.py +60 -0
  72. agentscope_runtime/tools/modelstudio_memory/schemas.py +253 -0
  73. agentscope_runtime/version.py +1 -1
  74. {agentscope_runtime-1.0.4a1.dist-info → agentscope_runtime-1.0.5.dist-info}/METADATA +186 -73
  75. {agentscope_runtime-1.0.4a1.dist-info → agentscope_runtime-1.0.5.dist-info}/RECORD +79 -55
  76. {agentscope_runtime-1.0.4a1.dist-info → agentscope_runtime-1.0.5.dist-info}/WHEEL +0 -0
  77. {agentscope_runtime-1.0.4a1.dist-info → agentscope_runtime-1.0.5.dist-info}/entry_points.txt +0 -0
  78. {agentscope_runtime-1.0.4a1.dist-info → agentscope_runtime-1.0.5.dist-info}/licenses/LICENSE +0 -0
  79. {agentscope_runtime-1.0.4a1.dist-info → agentscope_runtime-1.0.5.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,290 @@
1
+ # -*- coding: utf-8 -*-
2
+ import logging
3
+ import os
4
+ from typing import Optional, Dict, List, Union, Any
5
+
6
+ from pydantic import BaseModel, Field
7
+ from .utils.docker_image_utils import (
8
+ ImageFactory,
9
+ RegistryConfig,
10
+ )
11
+ from .adapter.protocol_adapter import ProtocolAdapter
12
+ from .base import DeployManager
13
+ from ...common.container_clients.knative_client import (
14
+ KnativeClient,
15
+ )
16
+
17
+ logger = logging.getLogger(__name__)
18
+
19
+
20
+ class K8sConfig(BaseModel):
21
+ # Kubernetes settings
22
+ k8s_namespace: Optional[str] = Field(
23
+ "agentscope-runtime",
24
+ description="Kubernetes namespace to deploy KService. ",
25
+ )
26
+ kubeconfig_path: Optional[str] = Field(
27
+ None,
28
+ description="Path to kubeconfig file. If not set, will try "
29
+ "in-cluster config or default kubeconfig.",
30
+ )
31
+
32
+
33
+ class BuildConfig(BaseModel):
34
+ """Build configuration"""
35
+
36
+ build_context_dir: str = "/tmp/k8s_build"
37
+ dockerfile_template: str = None
38
+ build_timeout: int = 600 # 10 minutes
39
+ push_timeout: int = 300 # 5 minutes
40
+ cleanup_after_build: bool = True
41
+
42
+
43
+ class KnativeDeployManager(DeployManager):
44
+ """
45
+ Deploy an AgentScope runner as a Knative Service.
46
+ Requires a Kubernetes cluster with Knative Serving installed.
47
+ """
48
+
49
+ def __init__(
50
+ self,
51
+ kube_config: K8sConfig = None,
52
+ registry_config: RegistryConfig = RegistryConfig(),
53
+ build_context_dir: str = "/tmp/k8s_build",
54
+ ):
55
+ """
56
+ Initialize the Knative deployer.
57
+ """
58
+ super().__init__()
59
+ self.kubeconfig = kube_config
60
+ self.registry_config = registry_config
61
+ self.image_factory = ImageFactory()
62
+ self.build_context_dir = build_context_dir
63
+ self._deployed_resources = {}
64
+ self._built_images = {}
65
+
66
+ self.knative_client = KnativeClient(
67
+ config=self.kubeconfig,
68
+ image_registry=self.registry_config.get_full_url(),
69
+ )
70
+
71
+ async def deploy(
72
+ self,
73
+ app=None,
74
+ runner=None,
75
+ stream: bool = True,
76
+ protocol_adapters: Optional[list[ProtocolAdapter]] = None,
77
+ requirements: Optional[Union[str, List[str]]] = None,
78
+ extra_packages: Optional[List[str]] = None,
79
+ base_image: str = "python:3.9-slim",
80
+ environment: Dict = None,
81
+ runtime_config: Dict = None,
82
+ annotations: Dict = None,
83
+ labels: Dict = None,
84
+ port: int = 8080,
85
+ mount_dir: str = None,
86
+ image_name: str = "agent_llm",
87
+ image_tag: str = "latest",
88
+ push_to_registry: bool = False,
89
+ **kwargs,
90
+ ) -> Dict[str, Any]:
91
+ """
92
+ Deploy the runner as a Knative Service.
93
+
94
+ Args:
95
+ app: Agent app to be deployed
96
+ runner: Complete Runner object with agent, environment_manager,
97
+ context_manager
98
+ stream: Enable streaming responses
99
+ protocol_adapters: protocol adapters
100
+ requirements: PyPI dependencies (following _agent_engines.py
101
+ pattern)
102
+ extra_packages: User code directory/file path
103
+ base_image: Docker base image
104
+ port: Container port
105
+ environment: Environment variables dict
106
+ mount_dir: Mount directory
107
+ runtime_config: K8s runtime configuration
108
+ annotations: knative service annotations
109
+ labels: knative service labels
110
+ # Backward compatibility
111
+ image_name: Image name
112
+ image_tag: Image tag
113
+ push_to_registry: Push to registry
114
+ **kwargs: Additional arguments
115
+
116
+ Returns:
117
+ Dict containing deploy_id, url, resource_name
118
+
119
+ Raises:
120
+ RuntimeError: If kservice fails
121
+
122
+ """
123
+ created_resources = []
124
+ deploy_id = self.deploy_id
125
+ try:
126
+ logger.info(f"Starting Knative Service {deploy_id}")
127
+
128
+ # Step 1: Build image with proper error handling
129
+ logger.info("Building runner image...")
130
+ try:
131
+ built_image_name = self.image_factory.build_image(
132
+ app=app,
133
+ runner=runner,
134
+ base_image=base_image,
135
+ build_context_dir=self.build_context_dir,
136
+ registry_config=self.registry_config,
137
+ image_name=image_name,
138
+ image_tag=image_tag,
139
+ push_to_registry=push_to_registry,
140
+ port=port,
141
+ protocol_adapters=protocol_adapters,
142
+ **kwargs,
143
+ )
144
+ if not built_image_name:
145
+ raise RuntimeError(
146
+ "Image build failed - no image name returned",
147
+ )
148
+
149
+ created_resources.append(f"image:{built_image_name}")
150
+ self._built_images[deploy_id] = built_image_name
151
+ logger.info(f"Image built successfully: {built_image_name}")
152
+ except Exception as e:
153
+ logger.error(f"Image build failed: {e}")
154
+ raise RuntimeError(f"Failed to build image: {e}") from e
155
+
156
+ if mount_dir:
157
+ if not os.path.isabs(mount_dir):
158
+ mount_dir = os.path.abspath(mount_dir)
159
+
160
+ volume_bindings = {
161
+ mount_dir: {
162
+ "bind": mount_dir,
163
+ "mode": "rw",
164
+ },
165
+ }
166
+ else:
167
+ volume_bindings = {}
168
+
169
+ resource_name = self.get_resource_name(deploy_id)
170
+
171
+ logger.info(f"Building Knative Service for {deploy_id}")
172
+
173
+ # Create Knative Service
174
+ name, url = self.knative_client.create_kservice(
175
+ name=resource_name,
176
+ image=built_image_name,
177
+ ports=[port],
178
+ volumes=volume_bindings,
179
+ environment=environment,
180
+ runtime_config=runtime_config or {},
181
+ annotations=annotations or {},
182
+ labels=labels or {},
183
+ )
184
+ if not url:
185
+ import traceback
186
+
187
+ raise RuntimeError(
188
+ f"Failed to create resource: "
189
+ f"{resource_name}, {traceback.format_exc()}",
190
+ )
191
+
192
+ logger.info(f"Knative Service url {url} successful")
193
+ self._deployed_resources[deploy_id] = {
194
+ "resource_name": name,
195
+ "config": {
196
+ "runner": runner.__class__.__name__,
197
+ "extra_packages": extra_packages,
198
+ "requirements": requirements, # New format
199
+ "base_image": base_image,
200
+ "port": port,
201
+ "environment": environment,
202
+ "runtime_config": runtime_config,
203
+ "stream": stream,
204
+ "protocol_adapters": protocol_adapters,
205
+ **kwargs,
206
+ },
207
+ }
208
+ return {
209
+ "deploy_id": deploy_id,
210
+ "resource_name": resource_name,
211
+ "url": url,
212
+ }
213
+
214
+ except Exception as e:
215
+ import traceback
216
+
217
+ logger.error(f"Knative Service {deploy_id} failed: {e}")
218
+ # Enhanced rollback with better error handling
219
+ raise RuntimeError(
220
+ f"Knative Service failed: {e}, {traceback.format_exc()}",
221
+ ) from e
222
+
223
+ @staticmethod
224
+ def get_resource_name(deploy_id: str) -> str:
225
+ return f"agent-{deploy_id[:8]}"
226
+
227
+ async def stop(
228
+ self,
229
+ deploy_id: str,
230
+ **kwargs,
231
+ ) -> Dict[str, Any]:
232
+ """Stop Knative Service.
233
+
234
+ Args:
235
+ deploy_id: Deployment identifier
236
+ **kwargs: Additional parameters
237
+
238
+ Returns:
239
+ Dict with success status, message, and details
240
+ """
241
+
242
+ resource_name = self.get_resource_name(deploy_id)
243
+ try:
244
+ # Try to remove the KService
245
+ success = self.knative_client.delete_kservice(resource_name)
246
+
247
+ if success:
248
+ return {
249
+ "success": True,
250
+ "message": f"Knative deployment {resource_name} "
251
+ f"removed",
252
+ "details": {
253
+ "deploy_id": deploy_id,
254
+ "resource_name": resource_name,
255
+ },
256
+ }
257
+ else:
258
+ return {
259
+ "success": False,
260
+ "message": f"Knative deployment {resource_name} not "
261
+ f"found (may already be deleted), Please check the "
262
+ f"detail in cluster",
263
+ "details": {
264
+ "deploy_id": deploy_id,
265
+ "resource_name": resource_name,
266
+ },
267
+ }
268
+ except Exception as e:
269
+ logger.error(
270
+ f"Failed to remove Knative service {resource_name}: {e}",
271
+ )
272
+ return {
273
+ "success": False,
274
+ "message": f"Failed to remove Knative service: {e}",
275
+ "details": {
276
+ "deploy_id": deploy_id,
277
+ "resource_name": resource_name,
278
+ "error": str(e),
279
+ },
280
+ }
281
+
282
+ def get_status(self) -> str:
283
+ """Get KService status"""
284
+ if self.deploy_id not in self._deployed_resources:
285
+ return "not_found"
286
+
287
+ resources = self._deployed_resources[self.deploy_id]
288
+ kservice_name = resources["resource_name"]
289
+
290
+ return self.knative_client.get_kservice_status(kservice_name)