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.
- agentscope_runtime/adapters/__init__.py +0 -0
- agentscope_runtime/adapters/agentscope/__init__.py +0 -0
- agentscope_runtime/adapters/agentscope/long_term_memory/__init__.py +6 -0
- agentscope_runtime/adapters/agentscope/long_term_memory/_long_term_memory_adapter.py +258 -0
- agentscope_runtime/adapters/agentscope/memory/__init__.py +6 -0
- agentscope_runtime/adapters/agentscope/memory/_memory_adapter.py +152 -0
- agentscope_runtime/adapters/agentscope/message.py +535 -0
- agentscope_runtime/adapters/agentscope/stream.py +506 -0
- agentscope_runtime/adapters/agentscope/tool/__init__.py +9 -0
- agentscope_runtime/adapters/agentscope/tool/sandbox_tool.py +69 -0
- agentscope_runtime/adapters/agentscope/tool/tool.py +233 -0
- agentscope_runtime/adapters/autogen/__init__.py +0 -0
- agentscope_runtime/adapters/autogen/tool/__init__.py +7 -0
- agentscope_runtime/adapters/autogen/tool/tool.py +211 -0
- agentscope_runtime/adapters/text/__init__.py +0 -0
- agentscope_runtime/adapters/text/stream.py +29 -0
- agentscope_runtime/common/collections/redis_mapping.py +4 -1
- agentscope_runtime/common/container_clients/fc_client.py +855 -0
- agentscope_runtime/common/container_clients/kubernetes_client.py +6 -13
- agentscope_runtime/common/utils/__init__.py +0 -0
- agentscope_runtime/common/utils/lazy_loader.py +57 -0
- agentscope_runtime/engine/__init__.py +25 -18
- agentscope_runtime/engine/app/agent_app.py +161 -91
- agentscope_runtime/engine/app/base_app.py +4 -118
- agentscope_runtime/engine/constant.py +8 -0
- agentscope_runtime/engine/deployers/__init__.py +8 -0
- agentscope_runtime/engine/deployers/adapter/__init__.py +2 -0
- agentscope_runtime/engine/deployers/adapter/a2a/a2a_adapter_utils.py +0 -21
- agentscope_runtime/engine/deployers/adapter/a2a/a2a_protocol_adapter.py +28 -9
- agentscope_runtime/engine/deployers/adapter/responses/__init__.py +2 -0
- agentscope_runtime/engine/deployers/adapter/responses/response_api_adapter_utils.py +5 -2
- agentscope_runtime/engine/deployers/adapter/responses/response_api_protocol_adapter.py +1 -1
- agentscope_runtime/engine/deployers/agentrun_deployer.py +2541 -0
- agentscope_runtime/engine/deployers/cli_fc_deploy.py +1 -1
- agentscope_runtime/engine/deployers/kubernetes_deployer.py +9 -21
- agentscope_runtime/engine/deployers/local_deployer.py +47 -74
- agentscope_runtime/engine/deployers/modelstudio_deployer.py +216 -50
- agentscope_runtime/engine/deployers/utils/app_runner_utils.py +29 -0
- agentscope_runtime/engine/deployers/utils/detached_app.py +510 -0
- agentscope_runtime/engine/deployers/utils/docker_image_utils/__init__.py +1 -1
- agentscope_runtime/engine/deployers/utils/docker_image_utils/dockerfile_generator.py +1 -1
- agentscope_runtime/engine/deployers/utils/docker_image_utils/{runner_image_factory.py → image_factory.py} +121 -61
- agentscope_runtime/engine/deployers/utils/package.py +693 -0
- agentscope_runtime/engine/deployers/utils/service_utils/__init__.py +0 -5
- agentscope_runtime/engine/deployers/utils/service_utils/fastapi_factory.py +301 -282
- agentscope_runtime/engine/deployers/utils/service_utils/fastapi_templates.py +2 -4
- agentscope_runtime/engine/deployers/utils/service_utils/process_manager.py +23 -1
- agentscope_runtime/engine/deployers/utils/templates/app_main.py.j2 +84 -0
- agentscope_runtime/engine/deployers/utils/templates/runner_main.py.j2 +95 -0
- agentscope_runtime/engine/deployers/utils/{service_utils → templates}/standalone_main.py.j2 +0 -45
- agentscope_runtime/engine/deployers/utils/wheel_packager.py +119 -18
- agentscope_runtime/engine/helpers/runner.py +40 -0
- agentscope_runtime/engine/runner.py +171 -130
- agentscope_runtime/engine/schemas/agent_schemas.py +114 -3
- agentscope_runtime/engine/schemas/modelstudio_llm.py +4 -2
- agentscope_runtime/engine/schemas/oai_llm.py +23 -23
- agentscope_runtime/engine/schemas/response_api.py +65 -0
- agentscope_runtime/engine/schemas/session.py +24 -0
- agentscope_runtime/engine/services/__init__.py +0 -9
- agentscope_runtime/engine/services/agent_state/__init__.py +16 -0
- agentscope_runtime/engine/services/agent_state/redis_state_service.py +113 -0
- agentscope_runtime/engine/services/agent_state/state_service.py +179 -0
- agentscope_runtime/engine/services/memory/__init__.py +24 -0
- agentscope_runtime/engine/services/{mem0_memory_service.py → memory/mem0_memory_service.py} +17 -13
- agentscope_runtime/engine/services/{memory_service.py → memory/memory_service.py} +28 -7
- agentscope_runtime/engine/services/{redis_memory_service.py → memory/redis_memory_service.py} +1 -1
- agentscope_runtime/engine/services/{reme_personal_memory_service.py → memory/reme_personal_memory_service.py} +9 -6
- agentscope_runtime/engine/services/{reme_task_memory_service.py → memory/reme_task_memory_service.py} +2 -2
- agentscope_runtime/engine/services/{tablestore_memory_service.py → memory/tablestore_memory_service.py} +16 -19
- agentscope_runtime/engine/services/sandbox/__init__.py +13 -0
- agentscope_runtime/engine/services/{sandbox_service.py → sandbox/sandbox_service.py} +86 -71
- agentscope_runtime/engine/services/session_history/__init__.py +23 -0
- agentscope_runtime/engine/services/{redis_session_history_service.py → session_history/redis_session_history_service.py} +3 -2
- agentscope_runtime/engine/services/{session_history_service.py → session_history/session_history_service.py} +44 -34
- agentscope_runtime/engine/services/{tablestore_session_history_service.py → session_history/tablestore_session_history_service.py} +14 -19
- agentscope_runtime/engine/services/utils/tablestore_service_utils.py +2 -2
- agentscope_runtime/engine/tracing/base.py +10 -9
- agentscope_runtime/engine/tracing/message_util.py +1 -1
- agentscope_runtime/engine/tracing/tracing_util.py +7 -2
- agentscope_runtime/engine/tracing/wrapper.py +49 -31
- agentscope_runtime/sandbox/__init__.py +10 -2
- agentscope_runtime/sandbox/box/agentbay/__init__.py +4 -0
- agentscope_runtime/sandbox/box/agentbay/agentbay_sandbox.py +559 -0
- agentscope_runtime/sandbox/box/base/base_sandbox.py +12 -0
- agentscope_runtime/sandbox/box/browser/browser_sandbox.py +115 -11
- agentscope_runtime/sandbox/box/cloud/__init__.py +4 -0
- agentscope_runtime/sandbox/box/cloud/cloud_sandbox.py +254 -0
- agentscope_runtime/sandbox/box/filesystem/filesystem_sandbox.py +66 -0
- agentscope_runtime/sandbox/box/gui/gui_sandbox.py +42 -0
- agentscope_runtime/sandbox/box/mobile/__init__.py +4 -0
- agentscope_runtime/sandbox/box/mobile/box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/mobile/mobile_sandbox.py +216 -0
- agentscope_runtime/sandbox/box/training_box/training_box.py +2 -44
- agentscope_runtime/sandbox/client/http_client.py +1 -0
- agentscope_runtime/sandbox/enums.py +2 -1
- agentscope_runtime/sandbox/manager/sandbox_manager.py +15 -2
- agentscope_runtime/sandbox/manager/server/app.py +12 -0
- agentscope_runtime/sandbox/manager/server/config.py +19 -0
- agentscope_runtime/sandbox/model/manager_config.py +79 -2
- agentscope_runtime/sandbox/utils.py +0 -18
- agentscope_runtime/tools/RAGs/__init__.py +0 -0
- agentscope_runtime/tools/RAGs/modelstudio_rag.py +377 -0
- agentscope_runtime/tools/RAGs/modelstudio_rag_lite.py +219 -0
- agentscope_runtime/tools/__init__.py +119 -0
- agentscope_runtime/tools/_constants.py +18 -0
- agentscope_runtime/tools/alipay/__init__.py +4 -0
- agentscope_runtime/tools/alipay/base.py +334 -0
- agentscope_runtime/tools/alipay/payment.py +835 -0
- agentscope_runtime/tools/alipay/subscribe.py +551 -0
- agentscope_runtime/tools/base.py +264 -0
- agentscope_runtime/tools/cli/__init__.py +0 -0
- agentscope_runtime/tools/cli/modelstudio_mcp_server.py +78 -0
- agentscope_runtime/tools/generations/__init__.py +75 -0
- agentscope_runtime/tools/generations/async_image_to_video.py +350 -0
- agentscope_runtime/tools/generations/async_image_to_video_wan25.py +366 -0
- agentscope_runtime/tools/generations/async_speech_to_video.py +422 -0
- agentscope_runtime/tools/generations/async_text_to_video.py +320 -0
- agentscope_runtime/tools/generations/async_text_to_video_wan25.py +334 -0
- agentscope_runtime/tools/generations/image_edit.py +208 -0
- agentscope_runtime/tools/generations/image_edit_wan25.py +193 -0
- agentscope_runtime/tools/generations/image_generation.py +202 -0
- agentscope_runtime/tools/generations/image_generation_wan25.py +201 -0
- agentscope_runtime/tools/generations/image_style_repaint.py +208 -0
- agentscope_runtime/tools/generations/image_to_video.py +233 -0
- agentscope_runtime/tools/generations/qwen_image_edit.py +205 -0
- agentscope_runtime/tools/generations/qwen_image_generation.py +214 -0
- agentscope_runtime/tools/generations/qwen_text_to_speech.py +154 -0
- agentscope_runtime/tools/generations/speech_to_text.py +260 -0
- agentscope_runtime/tools/generations/speech_to_video.py +314 -0
- agentscope_runtime/tools/generations/text_to_video.py +221 -0
- agentscope_runtime/tools/mcp_wrapper.py +215 -0
- agentscope_runtime/tools/realtime_clients/__init__.py +13 -0
- agentscope_runtime/tools/realtime_clients/asr_client.py +27 -0
- agentscope_runtime/tools/realtime_clients/azure_asr_client.py +195 -0
- agentscope_runtime/tools/realtime_clients/azure_tts_client.py +383 -0
- agentscope_runtime/tools/realtime_clients/modelstudio_asr_client.py +151 -0
- agentscope_runtime/tools/realtime_clients/modelstudio_tts_client.py +199 -0
- agentscope_runtime/tools/realtime_clients/realtime_tool.py +55 -0
- agentscope_runtime/tools/realtime_clients/tts_client.py +33 -0
- agentscope_runtime/tools/searches/__init__.py +3 -0
- agentscope_runtime/tools/searches/modelstudio_search.py +877 -0
- agentscope_runtime/tools/searches/modelstudio_search_lite.py +310 -0
- agentscope_runtime/tools/utils/__init__.py +0 -0
- agentscope_runtime/tools/utils/api_key_util.py +45 -0
- agentscope_runtime/tools/utils/crypto_utils.py +99 -0
- agentscope_runtime/tools/utils/mcp_util.py +35 -0
- agentscope_runtime/version.py +1 -1
- {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/METADATA +244 -168
- agentscope_runtime-1.0.0.dist-info/RECORD +240 -0
- {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/entry_points.txt +1 -0
- agentscope_runtime/engine/agents/__init__.py +0 -2
- agentscope_runtime/engine/agents/agentscope_agent.py +0 -488
- agentscope_runtime/engine/agents/agno_agent.py +0 -222
- agentscope_runtime/engine/agents/autogen_agent.py +0 -250
- agentscope_runtime/engine/agents/base_agent.py +0 -29
- agentscope_runtime/engine/agents/langgraph_agent.py +0 -59
- agentscope_runtime/engine/agents/utils.py +0 -53
- agentscope_runtime/engine/deployers/utils/package_project_utils.py +0 -1163
- agentscope_runtime/engine/deployers/utils/service_utils/service_config.py +0 -75
- agentscope_runtime/engine/deployers/utils/service_utils/service_factory.py +0 -220
- agentscope_runtime/engine/helpers/helper.py +0 -179
- agentscope_runtime/engine/schemas/context.py +0 -54
- agentscope_runtime/engine/services/context_manager.py +0 -164
- agentscope_runtime/engine/services/environment_manager.py +0 -50
- agentscope_runtime/engine/services/manager.py +0 -174
- agentscope_runtime/engine/services/rag_service.py +0 -195
- agentscope_runtime/engine/services/tablestore_rag_service.py +0 -143
- agentscope_runtime/sandbox/tools/__init__.py +0 -12
- agentscope_runtime/sandbox/tools/base/__init__.py +0 -8
- agentscope_runtime/sandbox/tools/base/tool.py +0 -52
- agentscope_runtime/sandbox/tools/browser/__init__.py +0 -57
- agentscope_runtime/sandbox/tools/browser/tool.py +0 -597
- agentscope_runtime/sandbox/tools/filesystem/__init__.py +0 -32
- agentscope_runtime/sandbox/tools/filesystem/tool.py +0 -319
- agentscope_runtime/sandbox/tools/function_tool.py +0 -321
- agentscope_runtime/sandbox/tools/gui/__init__.py +0 -7
- agentscope_runtime/sandbox/tools/gui/tool.py +0 -77
- agentscope_runtime/sandbox/tools/mcp_tool.py +0 -195
- agentscope_runtime/sandbox/tools/sandbox_tool.py +0 -104
- agentscope_runtime/sandbox/tools/tool.py +0 -238
- agentscope_runtime/sandbox/tools/utils.py +0 -68
- agentscope_runtime-0.2.0b1.dist-info/RECORD +0 -183
- {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/WHEEL +0 -0
- {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/licenses/LICENSE +0 -0
- {agentscope_runtime-0.2.0b1.dist-info → agentscope_runtime-1.0.0.dist-info}/top_level.txt +0 -0
|
@@ -5,25 +5,19 @@ import copy
|
|
|
5
5
|
from enum import Enum
|
|
6
6
|
from typing import Any, Dict, List, Optional
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
raise ImportError(
|
|
20
|
-
"aliyun_tablestore is not available. "
|
|
21
|
-
"Please run pip install agentscope-runtime[aliyun_tablestore_ext]",
|
|
22
|
-
) from e
|
|
23
|
-
|
|
24
|
-
from ..schemas.agent_schemas import Message, MessageType
|
|
8
|
+
import tablestore
|
|
9
|
+
from langchain_community.embeddings import DashScopeEmbeddings
|
|
10
|
+
from langchain_core.embeddings import Embeddings
|
|
11
|
+
from tablestore import AsyncOTSClient as AsyncTablestoreClient
|
|
12
|
+
from tablestore import VectorMetricType
|
|
13
|
+
from tablestore_for_agent_memory.base.filter import Filters
|
|
14
|
+
from tablestore_for_agent_memory.knowledge.async_knowledge_store import (
|
|
15
|
+
AsyncKnowledgeStore,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
from ...schemas.agent_schemas import Message, MessageType
|
|
25
19
|
from .memory_service import MemoryService
|
|
26
|
-
from
|
|
20
|
+
from ..utils.tablestore_service_utils import (
|
|
27
21
|
convert_messages_to_tablestore_documents,
|
|
28
22
|
convert_tablestore_document_to_message,
|
|
29
23
|
get_message_metadata_names,
|
|
@@ -51,7 +45,7 @@ class TablestoreMemoryService(MemoryService):
|
|
|
51
45
|
self,
|
|
52
46
|
tablestore_client: AsyncTablestoreClient,
|
|
53
47
|
search_strategy: SearchStrategy = SearchStrategy.FULL_TEXT,
|
|
54
|
-
embedding_model: Optional[Embeddings] =
|
|
48
|
+
embedding_model: Optional[Embeddings] = None,
|
|
55
49
|
vector_dimension: int = 1536,
|
|
56
50
|
table_name: Optional[str] = "agentscope_runtime_memory",
|
|
57
51
|
search_index_schema: Optional[List[tablestore.FieldSchema]] = (
|
|
@@ -63,6 +57,9 @@ class TablestoreMemoryService(MemoryService):
|
|
|
63
57
|
vector_metric_type: VectorMetricType = VectorMetricType.VM_COSINE,
|
|
64
58
|
**kwargs: Any,
|
|
65
59
|
):
|
|
60
|
+
if embedding_model is None:
|
|
61
|
+
embedding_model = DashScopeEmbeddings()
|
|
62
|
+
|
|
66
63
|
self._search_strategy = search_strategy
|
|
67
64
|
self._embedding_model = (
|
|
68
65
|
embedding_model # the parameter is None, don't store vector.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from typing import TYPE_CHECKING
|
|
3
|
+
from ....common.utils.lazy_loader import install_lazy_loader
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from .sandbox_service import SandboxService
|
|
7
|
+
|
|
8
|
+
install_lazy_loader(
|
|
9
|
+
globals(),
|
|
10
|
+
{
|
|
11
|
+
"SandboxService": ".sandbox_service",
|
|
12
|
+
},
|
|
13
|
+
)
|
|
@@ -1,31 +1,34 @@
|
|
|
1
1
|
# -*- coding: utf-8 -*-
|
|
2
|
-
#
|
|
3
|
-
from typing import List
|
|
2
|
+
# mypy: disable-error-code="list-item"
|
|
3
|
+
from typing import List, Optional
|
|
4
4
|
|
|
5
|
-
from
|
|
6
|
-
from
|
|
7
|
-
from
|
|
8
|
-
from
|
|
9
|
-
from ...sandbox.tools.sandbox_tool import SandboxTool
|
|
10
|
-
from ...sandbox.tools.function_tool import FunctionTool
|
|
11
|
-
from ...engine.services.base import ServiceWithLifecycleManager
|
|
5
|
+
from ....sandbox.enums import SandboxType
|
|
6
|
+
from ....sandbox.manager import SandboxManager
|
|
7
|
+
from ....sandbox.registry import SandboxRegistry
|
|
8
|
+
from ....engine.services.base import ServiceWithLifecycleManager
|
|
12
9
|
|
|
13
10
|
|
|
14
11
|
class SandboxService(ServiceWithLifecycleManager):
|
|
15
12
|
def __init__(self, base_url=None, bearer_token=None):
|
|
16
|
-
self.manager_api =
|
|
17
|
-
base_url=base_url,
|
|
18
|
-
bearer_token=bearer_token,
|
|
19
|
-
)
|
|
20
|
-
|
|
13
|
+
self.manager_api = None
|
|
21
14
|
self.base_url = base_url
|
|
22
15
|
self.bearer_token = bearer_token
|
|
16
|
+
self._health = False
|
|
23
17
|
|
|
24
18
|
async def start(self) -> None:
|
|
25
|
-
|
|
19
|
+
if self.manager_api is None:
|
|
20
|
+
self.manager_api = SandboxManager(
|
|
21
|
+
base_url=self.base_url,
|
|
22
|
+
bearer_token=self.bearer_token,
|
|
23
|
+
)
|
|
24
|
+
self._health = True
|
|
26
25
|
|
|
27
26
|
async def stop(self) -> None:
|
|
28
27
|
# Release all environments
|
|
28
|
+
if self.manager_api is None:
|
|
29
|
+
self._health = False
|
|
30
|
+
return
|
|
31
|
+
|
|
29
32
|
session_keys = self.manager_api.list_session_keys()
|
|
30
33
|
|
|
31
34
|
if session_keys:
|
|
@@ -39,15 +42,16 @@ class SandboxService(ServiceWithLifecycleManager):
|
|
|
39
42
|
# Embedded mode
|
|
40
43
|
self.manager_api.cleanup()
|
|
41
44
|
|
|
45
|
+
self.manager_api = None
|
|
46
|
+
|
|
42
47
|
async def health(self) -> bool:
|
|
43
|
-
return
|
|
48
|
+
return self._health
|
|
44
49
|
|
|
45
50
|
def connect(
|
|
46
51
|
self,
|
|
47
|
-
session_id,
|
|
48
|
-
user_id,
|
|
49
|
-
|
|
50
|
-
tools=None,
|
|
52
|
+
session_id: str,
|
|
53
|
+
user_id: Optional[str] = None,
|
|
54
|
+
sandbox_types=None,
|
|
51
55
|
) -> List:
|
|
52
56
|
# Create a composite key
|
|
53
57
|
session_ctx_id = self._create_session_ctx_id(session_id, user_id)
|
|
@@ -62,49 +66,32 @@ class SandboxService(ServiceWithLifecycleManager):
|
|
|
62
66
|
# Create a new environment
|
|
63
67
|
return self._create_new_environment(
|
|
64
68
|
session_ctx_id,
|
|
65
|
-
|
|
66
|
-
tools,
|
|
69
|
+
sandbox_types,
|
|
67
70
|
)
|
|
68
71
|
|
|
69
72
|
def _create_new_environment(
|
|
70
73
|
self,
|
|
71
74
|
session_ctx_id: str,
|
|
72
|
-
|
|
73
|
-
tools=None,
|
|
75
|
+
sandbox_types: Optional[List[str]] = None,
|
|
74
76
|
):
|
|
75
|
-
if
|
|
76
|
-
|
|
77
|
-
if not isinstance(tool, (SandboxTool, FunctionTool, MCPTool)):
|
|
78
|
-
raise ValueError(
|
|
79
|
-
"tools must be instances of SandboxTool, "
|
|
80
|
-
"FunctionTool, or MCPTool",
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
if env_types is None:
|
|
84
|
-
assert (
|
|
85
|
-
tools is not None
|
|
86
|
-
), "tools must be specified when env_types is not set"
|
|
87
|
-
|
|
88
|
-
if tools:
|
|
89
|
-
tool_env_types = set()
|
|
90
|
-
for tool in tools:
|
|
91
|
-
tool_env_types.add(tool.sandbox_type)
|
|
92
|
-
if env_types is None:
|
|
93
|
-
env_types = []
|
|
94
|
-
|
|
95
|
-
env_types = set(env_types) | tool_env_types
|
|
77
|
+
if sandbox_types is None:
|
|
78
|
+
sandbox_types = [SandboxType.BASE]
|
|
96
79
|
|
|
97
80
|
sandboxes = []
|
|
98
|
-
for env_type in
|
|
81
|
+
for env_type in sandbox_types:
|
|
99
82
|
if env_type is None:
|
|
100
83
|
continue
|
|
101
84
|
|
|
102
85
|
box_type = SandboxType(env_type)
|
|
103
86
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
87
|
+
if box_type != SandboxType.AGENTBAY:
|
|
88
|
+
box_id = self.manager_api.create_from_pool(
|
|
89
|
+
sandbox_type=box_type.value,
|
|
90
|
+
meta={"session_ctx_id": session_ctx_id},
|
|
91
|
+
)
|
|
92
|
+
else:
|
|
93
|
+
box_id = None
|
|
94
|
+
|
|
108
95
|
box_cls = SandboxRegistry.get_classes_by_type(box_type)
|
|
109
96
|
|
|
110
97
|
box = box_cls(
|
|
@@ -119,32 +106,38 @@ class SandboxService(ServiceWithLifecycleManager):
|
|
|
119
106
|
# Embedded mode
|
|
120
107
|
box.manager_api = self.manager_api
|
|
121
108
|
|
|
122
|
-
# Add MCP to the sandbox
|
|
123
|
-
server_config_list = []
|
|
124
|
-
if tools:
|
|
125
|
-
for tool in tools:
|
|
126
|
-
if isinstance(tool, MCPTool) and SandboxType(
|
|
127
|
-
tool.sandbox_type,
|
|
128
|
-
) == SandboxType(box_type):
|
|
129
|
-
server_config_list.append(tool.server_configs)
|
|
130
|
-
if server_config_list:
|
|
131
|
-
server_configs = {"mcpServers": {}}
|
|
132
|
-
for server_config in server_config_list:
|
|
133
|
-
if (
|
|
134
|
-
server_config is not None
|
|
135
|
-
and "mcpServers" in server_config
|
|
136
|
-
):
|
|
137
|
-
server_configs["mcpServers"].update(
|
|
138
|
-
server_config["mcpServers"],
|
|
139
|
-
)
|
|
140
|
-
box.add_mcp_servers(server_configs, overwrite=False)
|
|
141
|
-
|
|
142
109
|
sandboxes.append(box)
|
|
143
110
|
return sandboxes
|
|
144
111
|
|
|
145
112
|
def _connect_existing_environment(self, env_ids: List[str]):
|
|
146
113
|
boxes = []
|
|
147
114
|
for env_id in env_ids:
|
|
115
|
+
# Check if this is an AgentBay session ID
|
|
116
|
+
if self._is_agentbay_session_id(env_id):
|
|
117
|
+
try:
|
|
118
|
+
from ....sandbox.box.agentbay.agentbay_sandbox import (
|
|
119
|
+
AgentbaySandbox,
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
# Connect to existing AgentBay session
|
|
123
|
+
sandbox = AgentbaySandbox(
|
|
124
|
+
sandbox_id=env_id,
|
|
125
|
+
base_url=self.base_url,
|
|
126
|
+
bearer_token=self.bearer_token,
|
|
127
|
+
sandbox_type=SandboxType.AGENTBAY,
|
|
128
|
+
)
|
|
129
|
+
boxes.append(sandbox)
|
|
130
|
+
continue
|
|
131
|
+
except Exception as e:
|
|
132
|
+
import logging
|
|
133
|
+
|
|
134
|
+
logger = logging.getLogger(__name__)
|
|
135
|
+
logger.error(
|
|
136
|
+
f"Failed to connect to AgentBay session {env_id}: {e}",
|
|
137
|
+
)
|
|
138
|
+
continue
|
|
139
|
+
|
|
140
|
+
# Standard sandbox connection
|
|
148
141
|
info = self.manager_api.get_info(env_id)
|
|
149
142
|
version = info.get("version", "")
|
|
150
143
|
|
|
@@ -174,17 +167,39 @@ class SandboxService(ServiceWithLifecycleManager):
|
|
|
174
167
|
|
|
175
168
|
return boxes
|
|
176
169
|
|
|
177
|
-
def
|
|
170
|
+
def _is_agentbay_session_id(self, session_id: str) -> bool:
|
|
171
|
+
"""
|
|
172
|
+
Check if a session ID belongs to AgentBay.
|
|
173
|
+
|
|
174
|
+
AgentBay session IDs typically start with 'session-' prefix.
|
|
175
|
+
|
|
176
|
+
Args:
|
|
177
|
+
session_id: Session ID to check
|
|
178
|
+
|
|
179
|
+
Returns:
|
|
180
|
+
True if this appears to be an AgentBay session ID
|
|
181
|
+
"""
|
|
182
|
+
return session_id.startswith("session-")
|
|
183
|
+
|
|
184
|
+
def release(self, session_id, user_id=None):
|
|
178
185
|
session_ctx_id = self._create_session_ctx_id(session_id, user_id)
|
|
179
186
|
|
|
180
187
|
env_ids = self.manager_api.get_session_mapping(session_ctx_id)
|
|
181
188
|
|
|
182
189
|
if env_ids:
|
|
183
190
|
for env_id in env_ids:
|
|
191
|
+
# Check if this is an AgentBay session
|
|
192
|
+
if self._is_agentbay_session_id(env_id):
|
|
193
|
+
# AgentBay sessions are cleaned up automatically
|
|
194
|
+
# when the sandbox object is destroyed
|
|
195
|
+
continue
|
|
196
|
+
# Standard sandbox release
|
|
184
197
|
self.manager_api.release(env_id)
|
|
185
198
|
|
|
186
199
|
return True
|
|
187
200
|
|
|
188
201
|
def _create_session_ctx_id(self, session_id, user_id):
|
|
189
202
|
# Create a composite key from session_id and user_id
|
|
203
|
+
if user_id is None:
|
|
204
|
+
return session_id
|
|
190
205
|
return f"{session_id}_{user_id}"
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from typing import TYPE_CHECKING
|
|
3
|
+
from ....common.utils.lazy_loader import install_lazy_loader
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from .session_history_service import (
|
|
7
|
+
SessionHistoryService,
|
|
8
|
+
InMemorySessionHistoryService,
|
|
9
|
+
)
|
|
10
|
+
from .redis_session_history_service import RedisSessionHistoryService
|
|
11
|
+
from .tablestore_session_history_service import (
|
|
12
|
+
TablestoreSessionHistoryService,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
install_lazy_loader(
|
|
16
|
+
globals(),
|
|
17
|
+
{
|
|
18
|
+
"SessionHistoryService": ".session_history_service",
|
|
19
|
+
"InMemorySessionHistoryService": ".session_history_service",
|
|
20
|
+
"RedisSessionHistoryService": ".redis_session_history_service",
|
|
21
|
+
"TablestoreSessionHistoryService": ".tablestore_session_history_service", # noqa
|
|
22
|
+
},
|
|
23
|
+
)
|
|
@@ -5,8 +5,9 @@ from typing import Optional, Dict, Any, List, Union
|
|
|
5
5
|
|
|
6
6
|
import redis.asyncio as aioredis
|
|
7
7
|
|
|
8
|
-
from .session_history_service import SessionHistoryService
|
|
9
|
-
from
|
|
8
|
+
from .session_history_service import SessionHistoryService
|
|
9
|
+
from ...schemas.session import Session
|
|
10
|
+
from ...schemas.agent_schemas import Message
|
|
10
11
|
|
|
11
12
|
|
|
12
13
|
class RedisSessionHistoryService(SessionHistoryService):
|
|
@@ -4,28 +4,9 @@ import uuid
|
|
|
4
4
|
from abc import abstractmethod
|
|
5
5
|
from typing import List, Dict, Optional, Union, Any
|
|
6
6
|
|
|
7
|
-
from
|
|
8
|
-
|
|
9
|
-
from .
|
|
10
|
-
from ..schemas.agent_schemas import Message
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class Session(BaseModel):
|
|
14
|
-
"""Represents a single conversation session.
|
|
15
|
-
|
|
16
|
-
A session contains the history of a conversation, including all
|
|
17
|
-
messages, and is uniquely identified by its ID.
|
|
18
|
-
|
|
19
|
-
Attributes:
|
|
20
|
-
id: The unique identifier for the session.
|
|
21
|
-
user_id: The identifier of the user who owns the session.
|
|
22
|
-
messages: A list of messages formatted for Agent response
|
|
23
|
-
|
|
24
|
-
"""
|
|
25
|
-
|
|
26
|
-
id: str
|
|
27
|
-
user_id: str
|
|
28
|
-
messages: List[Union[Message, Dict[str, Any]]] = []
|
|
7
|
+
from ..base import ServiceWithLifecycleManager
|
|
8
|
+
from ...schemas.session import Session
|
|
9
|
+
from ...schemas.agent_schemas import Message
|
|
29
10
|
|
|
30
11
|
|
|
31
12
|
class SessionHistoryService(ServiceWithLifecycleManager):
|
|
@@ -42,9 +23,6 @@ class SessionHistoryService(ServiceWithLifecycleManager):
|
|
|
42
23
|
async def stop(self) -> None:
|
|
43
24
|
pass
|
|
44
25
|
|
|
45
|
-
async def health(self) -> bool:
|
|
46
|
-
return True
|
|
47
|
-
|
|
48
26
|
@abstractmethod
|
|
49
27
|
async def create_session(
|
|
50
28
|
self,
|
|
@@ -124,13 +102,31 @@ class InMemorySessionHistoryService(SessionHistoryService):
|
|
|
124
102
|
for development, testing, and scenarios where persistence is not required.
|
|
125
103
|
|
|
126
104
|
Attributes:
|
|
127
|
-
|
|
105
|
+
_store: A dictionary holding all session objects, keyed by user ID
|
|
128
106
|
and then by session ID.
|
|
129
107
|
"""
|
|
130
108
|
|
|
131
109
|
def __init__(self) -> None:
|
|
132
110
|
"""Initializes the InMemorySessionHistoryService."""
|
|
133
|
-
self.
|
|
111
|
+
self._store: Optional[Dict[str, Dict[str, Session]]] = None
|
|
112
|
+
self._health = False
|
|
113
|
+
|
|
114
|
+
async def start(self) -> None:
|
|
115
|
+
"""Initialize the in-memory store."""
|
|
116
|
+
if self._store is None:
|
|
117
|
+
self._store = {}
|
|
118
|
+
self._health = True
|
|
119
|
+
|
|
120
|
+
async def stop(self) -> None:
|
|
121
|
+
"""Clear all in-memory data."""
|
|
122
|
+
if self._store is not None:
|
|
123
|
+
self._store.clear()
|
|
124
|
+
self._store = None
|
|
125
|
+
self._health = False
|
|
126
|
+
|
|
127
|
+
async def health(self) -> bool:
|
|
128
|
+
"""Service health check: always True."""
|
|
129
|
+
return self._health
|
|
134
130
|
|
|
135
131
|
async def create_session(
|
|
136
132
|
self,
|
|
@@ -146,13 +142,16 @@ class InMemorySessionHistoryService(SessionHistoryService):
|
|
|
146
142
|
Returns:
|
|
147
143
|
A deep copy of the newly created Session object.
|
|
148
144
|
"""
|
|
145
|
+
if self._store is None:
|
|
146
|
+
raise RuntimeError("Service not started")
|
|
147
|
+
|
|
149
148
|
session_id = (
|
|
150
149
|
session_id.strip()
|
|
151
150
|
if session_id and session_id.strip()
|
|
152
151
|
else str(uuid.uuid4())
|
|
153
152
|
)
|
|
154
153
|
session = Session(id=session_id, user_id=user_id)
|
|
155
|
-
self.
|
|
154
|
+
self._store.setdefault(user_id, {})[session_id] = session
|
|
156
155
|
return copy.deepcopy(session)
|
|
157
156
|
|
|
158
157
|
async def get_session(
|
|
@@ -169,11 +168,13 @@ class InMemorySessionHistoryService(SessionHistoryService):
|
|
|
169
168
|
Returns:
|
|
170
169
|
A deep copy of the Session object if found, otherwise None.
|
|
171
170
|
"""
|
|
171
|
+
if self._store is None:
|
|
172
|
+
raise RuntimeError("Service not started")
|
|
172
173
|
|
|
173
|
-
session = self.
|
|
174
|
+
session = self._store.get(user_id, {}).get(session_id)
|
|
174
175
|
if not session:
|
|
175
176
|
session = Session(id=session_id, user_id=user_id)
|
|
176
|
-
self.
|
|
177
|
+
self._store.setdefault(user_id, {})[session_id] = session
|
|
177
178
|
return copy.deepcopy(session) if session else None
|
|
178
179
|
|
|
179
180
|
async def delete_session(self, user_id: str, session_id: str) -> None:
|
|
@@ -185,8 +186,11 @@ class InMemorySessionHistoryService(SessionHistoryService):
|
|
|
185
186
|
user_id: The identifier for the user.
|
|
186
187
|
session_id: The identifier for the session to delete.
|
|
187
188
|
"""
|
|
188
|
-
if
|
|
189
|
-
|
|
189
|
+
if self._store is None:
|
|
190
|
+
raise RuntimeError("Service not started")
|
|
191
|
+
|
|
192
|
+
if user_id in self._store and session_id in self._store[user_id]:
|
|
193
|
+
del self._store[user_id][session_id]
|
|
190
194
|
|
|
191
195
|
async def list_sessions(self, user_id: str) -> list[Session]:
|
|
192
196
|
"""Lists all sessions for a given user.
|
|
@@ -200,7 +204,10 @@ class InMemorySessionHistoryService(SessionHistoryService):
|
|
|
200
204
|
Returns:
|
|
201
205
|
A list of Session objects belonging to the user, without history.
|
|
202
206
|
"""
|
|
203
|
-
|
|
207
|
+
if self._store is None:
|
|
208
|
+
raise RuntimeError("Service not started")
|
|
209
|
+
|
|
210
|
+
user_sessions = self._store.get(user_id, {})
|
|
204
211
|
# Return sessions without their potentially large history for
|
|
205
212
|
# efficiency.
|
|
206
213
|
sessions_without_history = []
|
|
@@ -232,6 +239,9 @@ class InMemorySessionHistoryService(SessionHistoryService):
|
|
|
232
239
|
message: The message or list of messages to append to the
|
|
233
240
|
session's history.
|
|
234
241
|
"""
|
|
242
|
+
if self._store is None:
|
|
243
|
+
raise RuntimeError("Service not started")
|
|
244
|
+
|
|
235
245
|
# Normalize to list
|
|
236
246
|
if not isinstance(message, list):
|
|
237
247
|
message = [message]
|
|
@@ -245,7 +255,7 @@ class InMemorySessionHistoryService(SessionHistoryService):
|
|
|
245
255
|
session.messages.extend(norm_message)
|
|
246
256
|
|
|
247
257
|
# update the in memory copy
|
|
248
|
-
storage_session = self.
|
|
258
|
+
storage_session = self._store.get(session.user_id, {}).get(
|
|
249
259
|
session.id,
|
|
250
260
|
)
|
|
251
261
|
if storage_session:
|
|
@@ -4,25 +4,20 @@ import asyncio
|
|
|
4
4
|
import uuid
|
|
5
5
|
from typing import Any, Dict, List, Optional, Union
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
) from e
|
|
22
|
-
|
|
23
|
-
from ..schemas.agent_schemas import Message
|
|
24
|
-
from .session_history_service import Session, SessionHistoryService
|
|
25
|
-
from .utils.tablestore_service_utils import (
|
|
7
|
+
import tablestore
|
|
8
|
+
from tablestore import AsyncOTSClient as AsyncTablestoreClient
|
|
9
|
+
from tablestore_for_agent_memory.base.base_memory_store import (
|
|
10
|
+
Session as TablestoreSession,
|
|
11
|
+
)
|
|
12
|
+
from tablestore_for_agent_memory.base.common import MetaType, Order
|
|
13
|
+
from tablestore_for_agent_memory.memory.async_memory_store import (
|
|
14
|
+
AsyncMemoryStore,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
from .session_history_service import SessionHistoryService
|
|
18
|
+
from ...schemas.agent_schemas import Message
|
|
19
|
+
from ...schemas.session import Session
|
|
20
|
+
from ..utils.tablestore_service_utils import (
|
|
26
21
|
convert_message_to_tablestore_message,
|
|
27
22
|
convert_tablestore_session_to_session,
|
|
28
23
|
tablestore_log,
|
|
@@ -17,8 +17,8 @@ from tablestore_for_agent_memory.base.base_memory_store import (
|
|
|
17
17
|
Session as TablestoreSession,
|
|
18
18
|
)
|
|
19
19
|
|
|
20
|
-
from ...schemas.agent_schemas import ContentType, MessageType
|
|
21
|
-
from
|
|
20
|
+
from ...schemas.agent_schemas import ContentType, MessageType, Message
|
|
21
|
+
from ...schemas.session import Session
|
|
22
22
|
|
|
23
23
|
|
|
24
24
|
def create_tablestore_client(
|
|
@@ -82,7 +82,6 @@ class TracerHandler(ABC):
|
|
|
82
82
|
raise NotImplementedError("Subclasses must implement on_error method")
|
|
83
83
|
|
|
84
84
|
|
|
85
|
-
# 新增基础的LogHandler类
|
|
86
85
|
class BaseLogHandler(TracerHandler):
|
|
87
86
|
"""Basic log handler implementation using Python's logging module."""
|
|
88
87
|
|
|
@@ -166,14 +165,16 @@ class BaseLogHandler(TracerHandler):
|
|
|
166
165
|
|
|
167
166
|
class Tracer:
|
|
168
167
|
"""
|
|
169
|
-
Tracer class for logging events
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
168
|
+
Tracer class for logging events.
|
|
169
|
+
|
|
170
|
+
Usage example::
|
|
171
|
+
|
|
172
|
+
with tracer.event(TraceType.LLM, payload) as event:
|
|
173
|
+
event.log("message")
|
|
174
|
+
# ...logic here...
|
|
175
|
+
end_payload = {"xxx": "value"}
|
|
176
|
+
# optional on_end call for additional payload and kwargs
|
|
177
|
+
event.on_end(end_payload, if_success=True)
|
|
177
178
|
"""
|
|
178
179
|
|
|
179
180
|
def __init__(self, handlers: List[TracerHandler]):
|
|
@@ -26,7 +26,10 @@ class TracingUtil:
|
|
|
26
26
|
"""Set request id"""
|
|
27
27
|
_user_request_id.set(value)
|
|
28
28
|
if value:
|
|
29
|
-
ctx = baggage.set_baggage(
|
|
29
|
+
ctx = baggage.set_baggage(
|
|
30
|
+
"agentscope-runtime_request_id",
|
|
31
|
+
value,
|
|
32
|
+
)
|
|
30
33
|
attach(ctx)
|
|
31
34
|
|
|
32
35
|
TracingUtil.clear_common_attributes()
|
|
@@ -48,7 +51,9 @@ class TracingUtil:
|
|
|
48
51
|
return request_id
|
|
49
52
|
|
|
50
53
|
# Fallback to baggage for cross-thread scenarios
|
|
51
|
-
request_id = baggage.get_baggage(
|
|
54
|
+
request_id = baggage.get_baggage(
|
|
55
|
+
"agentscope-runtime_request_id",
|
|
56
|
+
)
|
|
52
57
|
if request_id:
|
|
53
58
|
return request_id
|
|
54
59
|
|