agentscope-runtime 0.1.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/__init__.py +4 -0
- agentscope_runtime/engine/__init__.py +9 -0
- agentscope_runtime/engine/agents/__init__.py +2 -0
- agentscope_runtime/engine/agents/agentscope_agent/__init__.py +6 -0
- agentscope_runtime/engine/agents/agentscope_agent/agent.py +342 -0
- agentscope_runtime/engine/agents/agentscope_agent/hooks.py +156 -0
- agentscope_runtime/engine/agents/agno_agent.py +220 -0
- agentscope_runtime/engine/agents/base_agent.py +29 -0
- agentscope_runtime/engine/agents/langgraph_agent.py +59 -0
- agentscope_runtime/engine/agents/llm_agent.py +51 -0
- agentscope_runtime/engine/deployers/__init__.py +3 -0
- agentscope_runtime/engine/deployers/adapter/__init__.py +0 -0
- agentscope_runtime/engine/deployers/adapter/a2a/__init__.py +2 -0
- agentscope_runtime/engine/deployers/adapter/a2a/a2a_adapter_utils.py +425 -0
- agentscope_runtime/engine/deployers/adapter/a2a/a2a_agent_adapter.py +69 -0
- agentscope_runtime/engine/deployers/adapter/a2a/a2a_protocol_adapter.py +60 -0
- agentscope_runtime/engine/deployers/adapter/protocol_adapter.py +24 -0
- agentscope_runtime/engine/deployers/base.py +17 -0
- agentscope_runtime/engine/deployers/local_deployer.py +586 -0
- agentscope_runtime/engine/helpers/helper.py +127 -0
- agentscope_runtime/engine/llms/__init__.py +3 -0
- agentscope_runtime/engine/llms/base_llm.py +60 -0
- agentscope_runtime/engine/llms/qwen_llm.py +47 -0
- agentscope_runtime/engine/misc/__init__.py +0 -0
- agentscope_runtime/engine/runner.py +186 -0
- agentscope_runtime/engine/schemas/__init__.py +0 -0
- agentscope_runtime/engine/schemas/agent_schemas.py +551 -0
- agentscope_runtime/engine/schemas/context.py +54 -0
- agentscope_runtime/engine/services/__init__.py +9 -0
- agentscope_runtime/engine/services/base.py +77 -0
- agentscope_runtime/engine/services/context_manager.py +129 -0
- agentscope_runtime/engine/services/environment_manager.py +50 -0
- agentscope_runtime/engine/services/manager.py +174 -0
- agentscope_runtime/engine/services/memory_service.py +270 -0
- agentscope_runtime/engine/services/sandbox_service.py +198 -0
- agentscope_runtime/engine/services/session_history_service.py +256 -0
- agentscope_runtime/engine/tracing/__init__.py +40 -0
- agentscope_runtime/engine/tracing/base.py +309 -0
- agentscope_runtime/engine/tracing/local_logging_handler.py +356 -0
- agentscope_runtime/engine/tracing/tracing_metric.py +69 -0
- agentscope_runtime/engine/tracing/wrapper.py +321 -0
- agentscope_runtime/sandbox/__init__.py +14 -0
- agentscope_runtime/sandbox/box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/base/__init__.py +0 -0
- agentscope_runtime/sandbox/box/base/base_sandbox.py +37 -0
- agentscope_runtime/sandbox/box/base/box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/browser/__init__.py +0 -0
- agentscope_runtime/sandbox/box/browser/box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/browser/browser_sandbox.py +176 -0
- agentscope_runtime/sandbox/box/dummy/__init__.py +0 -0
- agentscope_runtime/sandbox/box/dummy/dummy_sandbox.py +26 -0
- agentscope_runtime/sandbox/box/filesystem/__init__.py +0 -0
- agentscope_runtime/sandbox/box/filesystem/box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/filesystem/filesystem_sandbox.py +87 -0
- agentscope_runtime/sandbox/box/sandbox.py +115 -0
- agentscope_runtime/sandbox/box/shared/__init__.py +0 -0
- agentscope_runtime/sandbox/box/shared/app.py +44 -0
- agentscope_runtime/sandbox/box/shared/dependencies/__init__.py +5 -0
- agentscope_runtime/sandbox/box/shared/dependencies/deps.py +22 -0
- agentscope_runtime/sandbox/box/shared/routers/__init__.py +12 -0
- agentscope_runtime/sandbox/box/shared/routers/generic.py +173 -0
- agentscope_runtime/sandbox/box/shared/routers/mcp.py +207 -0
- agentscope_runtime/sandbox/box/shared/routers/mcp_utils.py +153 -0
- agentscope_runtime/sandbox/box/shared/routers/runtime_watcher.py +187 -0
- agentscope_runtime/sandbox/box/shared/routers/workspace.py +325 -0
- agentscope_runtime/sandbox/box/training_box/__init__.py +0 -0
- agentscope_runtime/sandbox/box/training_box/base.py +120 -0
- agentscope_runtime/sandbox/box/training_box/env_service.py +752 -0
- agentscope_runtime/sandbox/box/training_box/environments/__init__.py +0 -0
- agentscope_runtime/sandbox/box/training_box/environments/appworld/appworld_env.py +987 -0
- agentscope_runtime/sandbox/box/training_box/registry.py +54 -0
- agentscope_runtime/sandbox/box/training_box/src/trajectory.py +278 -0
- agentscope_runtime/sandbox/box/training_box/training_box.py +219 -0
- agentscope_runtime/sandbox/build.py +213 -0
- agentscope_runtime/sandbox/client/__init__.py +5 -0
- agentscope_runtime/sandbox/client/http_client.py +527 -0
- agentscope_runtime/sandbox/client/training_client.py +265 -0
- agentscope_runtime/sandbox/constant.py +5 -0
- agentscope_runtime/sandbox/custom/__init__.py +16 -0
- agentscope_runtime/sandbox/custom/custom_sandbox.py +40 -0
- agentscope_runtime/sandbox/custom/example.py +37 -0
- agentscope_runtime/sandbox/enums.py +68 -0
- agentscope_runtime/sandbox/manager/__init__.py +4 -0
- agentscope_runtime/sandbox/manager/collections/__init__.py +22 -0
- agentscope_runtime/sandbox/manager/collections/base_mapping.py +20 -0
- agentscope_runtime/sandbox/manager/collections/base_queue.py +25 -0
- agentscope_runtime/sandbox/manager/collections/base_set.py +25 -0
- agentscope_runtime/sandbox/manager/collections/in_memory_mapping.py +22 -0
- agentscope_runtime/sandbox/manager/collections/in_memory_queue.py +28 -0
- agentscope_runtime/sandbox/manager/collections/in_memory_set.py +27 -0
- agentscope_runtime/sandbox/manager/collections/redis_mapping.py +26 -0
- agentscope_runtime/sandbox/manager/collections/redis_queue.py +27 -0
- agentscope_runtime/sandbox/manager/collections/redis_set.py +23 -0
- agentscope_runtime/sandbox/manager/container_clients/__init__.py +8 -0
- agentscope_runtime/sandbox/manager/container_clients/base_client.py +39 -0
- agentscope_runtime/sandbox/manager/container_clients/docker_client.py +170 -0
- agentscope_runtime/sandbox/manager/sandbox_manager.py +694 -0
- agentscope_runtime/sandbox/manager/server/__init__.py +0 -0
- agentscope_runtime/sandbox/manager/server/app.py +194 -0
- agentscope_runtime/sandbox/manager/server/config.py +68 -0
- agentscope_runtime/sandbox/manager/server/models.py +17 -0
- agentscope_runtime/sandbox/manager/storage/__init__.py +10 -0
- agentscope_runtime/sandbox/manager/storage/data_storage.py +16 -0
- agentscope_runtime/sandbox/manager/storage/local_storage.py +44 -0
- agentscope_runtime/sandbox/manager/storage/oss_storage.py +89 -0
- agentscope_runtime/sandbox/manager/utils.py +78 -0
- agentscope_runtime/sandbox/mcp_server.py +192 -0
- agentscope_runtime/sandbox/model/__init__.py +12 -0
- agentscope_runtime/sandbox/model/api.py +16 -0
- agentscope_runtime/sandbox/model/container.py +72 -0
- agentscope_runtime/sandbox/model/manager_config.py +158 -0
- agentscope_runtime/sandbox/registry.py +129 -0
- agentscope_runtime/sandbox/tools/__init__.py +12 -0
- agentscope_runtime/sandbox/tools/base/__init__.py +8 -0
- agentscope_runtime/sandbox/tools/base/tool.py +52 -0
- agentscope_runtime/sandbox/tools/browser/__init__.py +57 -0
- agentscope_runtime/sandbox/tools/browser/tool.py +597 -0
- agentscope_runtime/sandbox/tools/filesystem/__init__.py +32 -0
- agentscope_runtime/sandbox/tools/filesystem/tool.py +319 -0
- agentscope_runtime/sandbox/tools/function_tool.py +321 -0
- agentscope_runtime/sandbox/tools/mcp_tool.py +191 -0
- agentscope_runtime/sandbox/tools/sandbox_tool.py +104 -0
- agentscope_runtime/sandbox/tools/tool.py +123 -0
- agentscope_runtime/sandbox/tools/utils.py +68 -0
- agentscope_runtime/version.py +2 -0
- agentscope_runtime-0.1.0.dist-info/METADATA +327 -0
- agentscope_runtime-0.1.0.dist-info/RECORD +131 -0
- agentscope_runtime-0.1.0.dist-info/WHEEL +5 -0
- agentscope_runtime-0.1.0.dist-info/entry_points.txt +4 -0
- agentscope_runtime-0.1.0.dist-info/licenses/LICENSE +202 -0
- agentscope_runtime-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# pylint: disable=too-many-branches
|
|
3
|
+
from typing import List
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
from ...sandbox.enums import SandboxType
|
|
7
|
+
from ...sandbox.manager import SandboxManager
|
|
8
|
+
from ...sandbox.registry import SandboxRegistry
|
|
9
|
+
from ...sandbox.tools.mcp_tool import MCPTool
|
|
10
|
+
from ...sandbox.tools.sandbox_tool import SandboxTool
|
|
11
|
+
from ...sandbox.tools.function_tool import FunctionTool
|
|
12
|
+
except ImportError:
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
from ...engine.services.base import ServiceWithLifecycleManager
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class SandboxService(ServiceWithLifecycleManager):
|
|
19
|
+
def __init__(self, base_url=None, bearer_token=None):
|
|
20
|
+
if SandboxManager is None:
|
|
21
|
+
raise ImportError(
|
|
22
|
+
"SandboxManager is not available. "
|
|
23
|
+
"Please install agentscope-runtime[sandbox]",
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
self.manager_api = SandboxManager(
|
|
27
|
+
base_url=base_url,
|
|
28
|
+
bearer_token=bearer_token,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
self.base_url = base_url
|
|
32
|
+
self.bearer_token = bearer_token
|
|
33
|
+
self.sandbox_type_set = set(item.value for item in SandboxType)
|
|
34
|
+
|
|
35
|
+
self.session_mapping = {}
|
|
36
|
+
|
|
37
|
+
async def start(self) -> None:
|
|
38
|
+
pass
|
|
39
|
+
|
|
40
|
+
async def stop(self) -> None:
|
|
41
|
+
# Release all environments
|
|
42
|
+
for _, env_ids in self.session_mapping.items():
|
|
43
|
+
for env_id in env_ids:
|
|
44
|
+
self.manager_api.release(env_id)
|
|
45
|
+
|
|
46
|
+
if self.base_url is None:
|
|
47
|
+
# Embedded mode
|
|
48
|
+
self.manager_api.cleanup()
|
|
49
|
+
|
|
50
|
+
async def health(self) -> bool:
|
|
51
|
+
return True
|
|
52
|
+
|
|
53
|
+
def connect(
|
|
54
|
+
self,
|
|
55
|
+
session_id,
|
|
56
|
+
user_id,
|
|
57
|
+
env_types=None,
|
|
58
|
+
tools=None,
|
|
59
|
+
) -> List:
|
|
60
|
+
for tool in tools:
|
|
61
|
+
if not isinstance(tool, (SandboxTool, FunctionTool, MCPTool)):
|
|
62
|
+
raise ValueError(
|
|
63
|
+
"tools must be instances of SandboxTool, FunctionTool, "
|
|
64
|
+
"or MCPTool",
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
# Create a composite key
|
|
68
|
+
composite_key = self._create_composite_key(session_id, user_id)
|
|
69
|
+
|
|
70
|
+
# Check if the composite_key already has an environment
|
|
71
|
+
if composite_key in self.session_mapping:
|
|
72
|
+
# Connect to existing environment
|
|
73
|
+
return self._connect_existing_environment(composite_key)
|
|
74
|
+
else:
|
|
75
|
+
# Create a new environment
|
|
76
|
+
return self._create_new_environment(
|
|
77
|
+
composite_key,
|
|
78
|
+
env_types,
|
|
79
|
+
tools,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
def _create_new_environment(
|
|
83
|
+
self,
|
|
84
|
+
composite_key,
|
|
85
|
+
env_types=None,
|
|
86
|
+
tools=None,
|
|
87
|
+
):
|
|
88
|
+
if env_types is None:
|
|
89
|
+
assert (
|
|
90
|
+
tools is not None
|
|
91
|
+
), "tools must be specified when env_types is not set"
|
|
92
|
+
|
|
93
|
+
server_configs = None
|
|
94
|
+
if tools:
|
|
95
|
+
server_config_list = []
|
|
96
|
+
tool_env_types = set()
|
|
97
|
+
for tool in tools:
|
|
98
|
+
tool_env_types.add(tool.sandbox_type)
|
|
99
|
+
if isinstance(tool, MCPTool):
|
|
100
|
+
server_config_list.append(tool.server_configs)
|
|
101
|
+
|
|
102
|
+
if env_types is None:
|
|
103
|
+
env_types = []
|
|
104
|
+
|
|
105
|
+
if server_config_list:
|
|
106
|
+
server_configs = {"mcpServers": {}}
|
|
107
|
+
for server_config in server_config_list:
|
|
108
|
+
server_configs["mcpServers"].update(
|
|
109
|
+
server_config["mcpServers"],
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
env_types = set(env_types) | tool_env_types
|
|
113
|
+
|
|
114
|
+
wbs = []
|
|
115
|
+
for env_type in env_types:
|
|
116
|
+
if env_type is None:
|
|
117
|
+
continue
|
|
118
|
+
|
|
119
|
+
box_type = SandboxType(env_type)
|
|
120
|
+
|
|
121
|
+
box_id = self.manager_api.create_from_pool(
|
|
122
|
+
sandbox_type=box_type.value,
|
|
123
|
+
)
|
|
124
|
+
box_cls = SandboxRegistry.get_classes_by_type(box_type)
|
|
125
|
+
|
|
126
|
+
box = box_cls(
|
|
127
|
+
sandbox_id=box_id,
|
|
128
|
+
base_url=self.manager_api.base_url,
|
|
129
|
+
bearer_token=self.bearer_token,
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
# All the operation must be done after replace this action in
|
|
133
|
+
# embedded mode
|
|
134
|
+
if self.base_url is None:
|
|
135
|
+
# Embedded mode
|
|
136
|
+
box.manager_api = self.manager_api
|
|
137
|
+
|
|
138
|
+
if box_type == SandboxType.BASE:
|
|
139
|
+
if server_configs:
|
|
140
|
+
box.add_mcp_servers(server_configs, overwrite=False)
|
|
141
|
+
|
|
142
|
+
# Store mapping from composite_key to env_id
|
|
143
|
+
if composite_key not in self.session_mapping:
|
|
144
|
+
self.session_mapping[composite_key] = []
|
|
145
|
+
|
|
146
|
+
self.session_mapping[composite_key].append(box_id)
|
|
147
|
+
|
|
148
|
+
wbs.append(box)
|
|
149
|
+
return wbs
|
|
150
|
+
|
|
151
|
+
def _connect_existing_environment(self, composite_key):
|
|
152
|
+
env_ids = self.session_mapping.get(composite_key)
|
|
153
|
+
|
|
154
|
+
boxes = []
|
|
155
|
+
for env_id in env_ids:
|
|
156
|
+
info = self.manager_api.get_info(env_id)
|
|
157
|
+
version = info.get("version", "")
|
|
158
|
+
|
|
159
|
+
env_type = None
|
|
160
|
+
for x in SandboxType:
|
|
161
|
+
if x.value in version:
|
|
162
|
+
env_type = x.value
|
|
163
|
+
break
|
|
164
|
+
|
|
165
|
+
if env_type is None:
|
|
166
|
+
continue
|
|
167
|
+
|
|
168
|
+
wb_type = SandboxType(env_type)
|
|
169
|
+
|
|
170
|
+
box_cls = SandboxRegistry.get_classes_by_type(wb_type)
|
|
171
|
+
|
|
172
|
+
box = box_cls(
|
|
173
|
+
sandbox_id=env_id,
|
|
174
|
+
base_url=self.manager_api.base_url,
|
|
175
|
+
bearer_token=self.bearer_token,
|
|
176
|
+
)
|
|
177
|
+
if self.base_url is None:
|
|
178
|
+
# Embedded mode
|
|
179
|
+
box.manager_api = self.manager_api
|
|
180
|
+
|
|
181
|
+
boxes.append(box)
|
|
182
|
+
|
|
183
|
+
return boxes
|
|
184
|
+
|
|
185
|
+
def release(self, session_id, user_id):
|
|
186
|
+
composite_key = self._create_composite_key(session_id, user_id)
|
|
187
|
+
|
|
188
|
+
# Retrieve and remove env_id using composite_key
|
|
189
|
+
env_ids = self.session_mapping.pop(composite_key, [])
|
|
190
|
+
|
|
191
|
+
for env_id in env_ids:
|
|
192
|
+
self.manager_api.release(env_id)
|
|
193
|
+
|
|
194
|
+
return True
|
|
195
|
+
|
|
196
|
+
def _create_composite_key(self, session_id, user_id):
|
|
197
|
+
# Create a composite key from session_id and user_id
|
|
198
|
+
return f"{session_id}_{user_id}"
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import copy
|
|
3
|
+
import uuid
|
|
4
|
+
from abc import abstractmethod
|
|
5
|
+
from typing import List, Dict, Optional, Union, Any
|
|
6
|
+
|
|
7
|
+
from pydantic import BaseModel
|
|
8
|
+
|
|
9
|
+
from .base import ServiceWithLifecycleManager
|
|
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]]] = []
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class SessionHistoryService(ServiceWithLifecycleManager):
|
|
32
|
+
"""Abstract base class for session history management services.
|
|
33
|
+
|
|
34
|
+
This class defines the standard interface for creating, retrieving,
|
|
35
|
+
updating, and deleting conversation sessions. Concrete implementations
|
|
36
|
+
(like InMemorySessionHistoryService) will handle the actual storage logic.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
async def start(self) -> None:
|
|
40
|
+
pass
|
|
41
|
+
|
|
42
|
+
async def stop(self) -> None:
|
|
43
|
+
pass
|
|
44
|
+
|
|
45
|
+
async def health(self) -> bool:
|
|
46
|
+
return True
|
|
47
|
+
|
|
48
|
+
@abstractmethod
|
|
49
|
+
async def create_session(
|
|
50
|
+
self,
|
|
51
|
+
user_id: str,
|
|
52
|
+
session_id: Optional[str] = None,
|
|
53
|
+
) -> Session:
|
|
54
|
+
"""Creates a new session for a given user.
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
user_id: The identifier for the user.
|
|
58
|
+
session_id: Could be defined by user
|
|
59
|
+
Returns:
|
|
60
|
+
The newly created Session object.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
@abstractmethod
|
|
64
|
+
async def get_session(
|
|
65
|
+
self,
|
|
66
|
+
user_id: str,
|
|
67
|
+
session_id: str,
|
|
68
|
+
) -> (Session | None):
|
|
69
|
+
"""Retrieves a specific session.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
user_id: The identifier for the user.
|
|
73
|
+
session_id: The identifier for the session to retrieve.
|
|
74
|
+
|
|
75
|
+
Returns:
|
|
76
|
+
The Session object if found, otherwise should raise an error or
|
|
77
|
+
return None in concrete implementations.
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
@abstractmethod
|
|
81
|
+
async def delete_session(self, user_id: str, session_id: str):
|
|
82
|
+
"""Deletes a specific session.
|
|
83
|
+
|
|
84
|
+
Args:
|
|
85
|
+
user_id: The identifier for the user.
|
|
86
|
+
session_id: The identifier for the session to delete.
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
@abstractmethod
|
|
90
|
+
async def list_sessions(self, user_id: str) -> list[Session]:
|
|
91
|
+
"""Lists all sessions for a given user.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
user_id: The identifier for the user.
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
A list of Session objects.
|
|
98
|
+
"""
|
|
99
|
+
return []
|
|
100
|
+
|
|
101
|
+
async def append_message(
|
|
102
|
+
self,
|
|
103
|
+
session: Session,
|
|
104
|
+
message: Union[
|
|
105
|
+
Message,
|
|
106
|
+
List[Message],
|
|
107
|
+
Dict[str, Any],
|
|
108
|
+
List[Dict[str, Any]],
|
|
109
|
+
],
|
|
110
|
+
):
|
|
111
|
+
"""Appends a message to the history of a specific session.
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
session: The session to which the message should be appended.
|
|
115
|
+
message: The message or list of messages to append. Supports both
|
|
116
|
+
dictionary format and Message objects.
|
|
117
|
+
"""
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class InMemorySessionHistoryService(SessionHistoryService):
|
|
121
|
+
"""An in-memory implementation of the SessionHistoryService.
|
|
122
|
+
|
|
123
|
+
This service stores all session data in a dictionary, making it suitable
|
|
124
|
+
for development, testing, and scenarios where persistence is not required.
|
|
125
|
+
|
|
126
|
+
Attributes:
|
|
127
|
+
_sessions: A dictionary holding all session objects, keyed by user ID
|
|
128
|
+
and then by session ID.
|
|
129
|
+
"""
|
|
130
|
+
|
|
131
|
+
def __init__(self) -> None:
|
|
132
|
+
"""Initializes the InMemorySessionHistoryService."""
|
|
133
|
+
self._sessions: Dict[str, Dict[str, Session]] = {}
|
|
134
|
+
|
|
135
|
+
async def create_session(
|
|
136
|
+
self,
|
|
137
|
+
user_id: str,
|
|
138
|
+
session_id: Optional[str] = None,
|
|
139
|
+
) -> Session:
|
|
140
|
+
"""Creates a new session for a given user and stores it.
|
|
141
|
+
|
|
142
|
+
Args:
|
|
143
|
+
user_id: The identifier for the user creating the session.
|
|
144
|
+
session_id: The identifier for the session to delete.
|
|
145
|
+
|
|
146
|
+
Returns:
|
|
147
|
+
A deep copy of the newly created Session object.
|
|
148
|
+
"""
|
|
149
|
+
session_id = (
|
|
150
|
+
session_id.strip()
|
|
151
|
+
if session_id and session_id.strip()
|
|
152
|
+
else str(uuid.uuid4())
|
|
153
|
+
)
|
|
154
|
+
session = Session(id=session_id, user_id=user_id)
|
|
155
|
+
self._sessions.setdefault(user_id, {})[session_id] = session
|
|
156
|
+
return copy.deepcopy(session)
|
|
157
|
+
|
|
158
|
+
async def get_session(
|
|
159
|
+
self,
|
|
160
|
+
user_id: str,
|
|
161
|
+
session_id: str,
|
|
162
|
+
) -> Session | None:
|
|
163
|
+
"""Retrieves a specific session from memory.
|
|
164
|
+
|
|
165
|
+
Args:
|
|
166
|
+
user_id: The identifier for the user.
|
|
167
|
+
session_id: The identifier for the session to retrieve.
|
|
168
|
+
|
|
169
|
+
Returns:
|
|
170
|
+
A deep copy of the Session object if found, otherwise None.
|
|
171
|
+
"""
|
|
172
|
+
|
|
173
|
+
session = self._sessions.get(user_id, {}).get(session_id)
|
|
174
|
+
if not session:
|
|
175
|
+
session = Session(id=session_id, user_id=user_id)
|
|
176
|
+
self._sessions.setdefault(user_id, {})[session_id] = session
|
|
177
|
+
return copy.deepcopy(session) if session else None
|
|
178
|
+
|
|
179
|
+
async def delete_session(self, user_id: str, session_id: str) -> None:
|
|
180
|
+
"""Deletes a specific session from memory.
|
|
181
|
+
|
|
182
|
+
If the session does not exist, the method does nothing.
|
|
183
|
+
|
|
184
|
+
Args:
|
|
185
|
+
user_id: The identifier for the user.
|
|
186
|
+
session_id: The identifier for the session to delete.
|
|
187
|
+
"""
|
|
188
|
+
if user_id in self._sessions and session_id in self._sessions[user_id]:
|
|
189
|
+
del self._sessions[user_id][session_id]
|
|
190
|
+
|
|
191
|
+
async def list_sessions(self, user_id: str) -> list[Session]:
|
|
192
|
+
"""Lists all sessions for a given user.
|
|
193
|
+
|
|
194
|
+
To improve performance and reduce data transfer, the returned session
|
|
195
|
+
objects do not contain the detailed response history.
|
|
196
|
+
|
|
197
|
+
Args:
|
|
198
|
+
user_id: The identifier of the user whose sessions to list.
|
|
199
|
+
|
|
200
|
+
Returns:
|
|
201
|
+
A list of Session objects belonging to the user, without history.
|
|
202
|
+
"""
|
|
203
|
+
user_sessions = self._sessions.get(user_id, {})
|
|
204
|
+
# Return sessions without their potentially large history for
|
|
205
|
+
# efficiency.
|
|
206
|
+
sessions_without_history = []
|
|
207
|
+
for session in user_sessions.values():
|
|
208
|
+
copied_session = copy.deepcopy(session)
|
|
209
|
+
copied_session.messages = []
|
|
210
|
+
sessions_without_history.append(copied_session)
|
|
211
|
+
return sessions_without_history
|
|
212
|
+
|
|
213
|
+
async def append_message(
|
|
214
|
+
self,
|
|
215
|
+
session: Session,
|
|
216
|
+
message: Union[
|
|
217
|
+
Message,
|
|
218
|
+
List[Message],
|
|
219
|
+
Dict[str, Any],
|
|
220
|
+
List[Dict[str, Any]],
|
|
221
|
+
],
|
|
222
|
+
) -> None:
|
|
223
|
+
"""Appends message to a session's history in memory.
|
|
224
|
+
|
|
225
|
+
This method finds the authoritative session object in the in-memory
|
|
226
|
+
storage and appends the message to its history. It supports both
|
|
227
|
+
dictionary format messages and Message objects.
|
|
228
|
+
|
|
229
|
+
Args:
|
|
230
|
+
session: The session object, typically from the context. The
|
|
231
|
+
user_id and id from this object are used for lookup.
|
|
232
|
+
message: The message or list of messages to append to the
|
|
233
|
+
session's history.
|
|
234
|
+
"""
|
|
235
|
+
# Normalize to list
|
|
236
|
+
if not isinstance(message, list):
|
|
237
|
+
message = [message]
|
|
238
|
+
|
|
239
|
+
norm_message = []
|
|
240
|
+
for msg in message:
|
|
241
|
+
if not isinstance(msg, Message):
|
|
242
|
+
msg = Message.model_validate(msg)
|
|
243
|
+
norm_message.append(msg)
|
|
244
|
+
session.messages.extend(norm_message)
|
|
245
|
+
|
|
246
|
+
# update the in memory copy
|
|
247
|
+
storage_session = self._sessions.get(session.user_id, {}).get(
|
|
248
|
+
session.id,
|
|
249
|
+
)
|
|
250
|
+
if storage_session:
|
|
251
|
+
storage_session.messages.extend(message)
|
|
252
|
+
else:
|
|
253
|
+
print(
|
|
254
|
+
f"Warning: Session {session.id} not found in storage for "
|
|
255
|
+
f"append_message.",
|
|
256
|
+
)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from typing import Any, List, Union
|
|
3
|
+
|
|
4
|
+
from .base import BaseLogHandler, Tracer, TracerHandler
|
|
5
|
+
from .tracing_metric import TraceType
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def create_handler(
|
|
9
|
+
eval_mode: Union[str, List] = "default",
|
|
10
|
+
**eval_params: Any,
|
|
11
|
+
) -> List[TracerHandler]:
|
|
12
|
+
if isinstance(eval_mode, str):
|
|
13
|
+
eval_mode = [eval_mode]
|
|
14
|
+
handlers: List[TracerHandler] = []
|
|
15
|
+
if "default" in eval_mode:
|
|
16
|
+
handlers.append(BaseLogHandler())
|
|
17
|
+
elif "local_logging" in eval_mode:
|
|
18
|
+
from .local_logging_handler import (
|
|
19
|
+
LocalLogHandler,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
handlers.append(LocalLogHandler(**eval_params))
|
|
23
|
+
return handlers
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
_tracer_instances = {}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def get_tracer(
|
|
30
|
+
eval_mode: Union[str, List] = "default",
|
|
31
|
+
**eval_params: Any,
|
|
32
|
+
) -> Tracer:
|
|
33
|
+
global _tracer_instances # noqa F824
|
|
34
|
+
if isinstance(eval_mode, str):
|
|
35
|
+
eval_mode = [eval_mode]
|
|
36
|
+
eval_mode_key = tuple(eval_mode)
|
|
37
|
+
if eval_mode_key not in _tracer_instances:
|
|
38
|
+
handlers = create_handler(eval_mode, **eval_params)
|
|
39
|
+
_tracer_instances[eval_mode_key] = Tracer(handlers)
|
|
40
|
+
return _tracer_instances[eval_mode_key]
|