agentscope-runtime 1.0.5.post1__py3-none-any.whl → 1.1.0b2__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 +3 -0
- agentscope_runtime/adapters/agentscope/message.py +36 -295
- agentscope_runtime/adapters/agentscope/stream.py +89 -2
- agentscope_runtime/adapters/agno/message.py +11 -2
- agentscope_runtime/adapters/agno/stream.py +1 -0
- agentscope_runtime/adapters/langgraph/__init__.py +1 -3
- agentscope_runtime/adapters/langgraph/message.py +11 -106
- agentscope_runtime/adapters/langgraph/stream.py +1 -0
- agentscope_runtime/adapters/ms_agent_framework/message.py +11 -1
- agentscope_runtime/adapters/ms_agent_framework/stream.py +1 -0
- agentscope_runtime/adapters/text/stream.py +1 -0
- agentscope_runtime/common/container_clients/agentrun_client.py +0 -3
- agentscope_runtime/common/container_clients/boxlite_client.py +26 -15
- agentscope_runtime/common/container_clients/fc_client.py +0 -11
- agentscope_runtime/common/utils/deprecation.py +14 -17
- agentscope_runtime/common/utils/logging.py +44 -0
- agentscope_runtime/engine/app/agent_app.py +5 -5
- agentscope_runtime/engine/app/celery_mixin.py +43 -4
- agentscope_runtime/engine/deployers/adapter/agui/__init__.py +8 -1
- agentscope_runtime/engine/deployers/adapter/agui/agui_adapter_utils.py +6 -1
- agentscope_runtime/engine/deployers/adapter/agui/agui_protocol_adapter.py +2 -2
- agentscope_runtime/engine/deployers/utils/service_utils/fastapi_factory.py +13 -0
- agentscope_runtime/engine/runner.py +31 -6
- agentscope_runtime/engine/schemas/agent_schemas.py +28 -0
- agentscope_runtime/engine/services/sandbox/sandbox_service.py +41 -9
- agentscope_runtime/sandbox/box/base/base_sandbox.py +4 -0
- agentscope_runtime/sandbox/box/browser/browser_sandbox.py +4 -0
- agentscope_runtime/sandbox/box/dummy/dummy_sandbox.py +9 -2
- agentscope_runtime/sandbox/box/filesystem/filesystem_sandbox.py +4 -0
- agentscope_runtime/sandbox/box/gui/gui_sandbox.py +5 -1
- agentscope_runtime/sandbox/box/mobile/mobile_sandbox.py +4 -0
- agentscope_runtime/sandbox/box/sandbox.py +122 -13
- agentscope_runtime/sandbox/client/async_http_client.py +1 -0
- agentscope_runtime/sandbox/client/base.py +0 -1
- agentscope_runtime/sandbox/client/http_client.py +0 -2
- agentscope_runtime/sandbox/manager/heartbeat_mixin.py +486 -0
- agentscope_runtime/sandbox/manager/sandbox_manager.py +740 -153
- agentscope_runtime/sandbox/manager/server/app.py +18 -11
- agentscope_runtime/sandbox/manager/server/config.py +10 -2
- agentscope_runtime/sandbox/mcp_server.py +0 -1
- agentscope_runtime/sandbox/model/__init__.py +2 -1
- agentscope_runtime/sandbox/model/container.py +90 -3
- agentscope_runtime/sandbox/model/manager_config.py +45 -1
- agentscope_runtime/version.py +1 -1
- {agentscope_runtime-1.0.5.post1.dist-info → agentscope_runtime-1.1.0b2.dist-info}/METADATA +36 -54
- {agentscope_runtime-1.0.5.post1.dist-info → agentscope_runtime-1.1.0b2.dist-info}/RECORD +50 -69
- agentscope_runtime/adapters/agentscope/long_term_memory/__init__.py +0 -6
- agentscope_runtime/adapters/agentscope/long_term_memory/_long_term_memory_adapter.py +0 -258
- agentscope_runtime/adapters/agentscope/memory/__init__.py +0 -6
- agentscope_runtime/adapters/agentscope/memory/_memory_adapter.py +0 -152
- agentscope_runtime/engine/services/agent_state/__init__.py +0 -25
- agentscope_runtime/engine/services/agent_state/redis_state_service.py +0 -166
- agentscope_runtime/engine/services/agent_state/state_service.py +0 -179
- agentscope_runtime/engine/services/agent_state/state_service_factory.py +0 -52
- agentscope_runtime/engine/services/memory/__init__.py +0 -33
- agentscope_runtime/engine/services/memory/mem0_memory_service.py +0 -128
- agentscope_runtime/engine/services/memory/memory_service.py +0 -292
- agentscope_runtime/engine/services/memory/memory_service_factory.py +0 -126
- agentscope_runtime/engine/services/memory/redis_memory_service.py +0 -290
- agentscope_runtime/engine/services/memory/reme_personal_memory_service.py +0 -109
- agentscope_runtime/engine/services/memory/reme_task_memory_service.py +0 -11
- agentscope_runtime/engine/services/memory/tablestore_memory_service.py +0 -301
- agentscope_runtime/engine/services/session_history/__init__.py +0 -32
- agentscope_runtime/engine/services/session_history/redis_session_history_service.py +0 -283
- agentscope_runtime/engine/services/session_history/session_history_service.py +0 -267
- agentscope_runtime/engine/services/session_history/session_history_service_factory.py +0 -73
- agentscope_runtime/engine/services/session_history/tablestore_session_history_service.py +0 -288
- {agentscope_runtime-1.0.5.post1.dist-info → agentscope_runtime-1.1.0b2.dist-info}/WHEEL +0 -0
- {agentscope_runtime-1.0.5.post1.dist-info → agentscope_runtime-1.1.0b2.dist-info}/entry_points.txt +0 -0
- {agentscope_runtime-1.0.5.post1.dist-info → agentscope_runtime-1.1.0b2.dist-info}/licenses/LICENSE +0 -0
- {agentscope_runtime-1.0.5.post1.dist-info → agentscope_runtime-1.1.0b2.dist-info}/top_level.txt +0 -0
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
import os
|
|
3
|
-
from typing import Optional, Dict, Any, List
|
|
4
|
-
|
|
5
|
-
from mem0 import AsyncMemoryClient
|
|
6
|
-
|
|
7
|
-
from .memory_service import MemoryService
|
|
8
|
-
from ...schemas.agent_schemas import Message, MessageType, ContentType
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
class Mem0MemoryService(MemoryService):
|
|
12
|
-
"""
|
|
13
|
-
Memory service that uses mem0 to store and retrieve memories.
|
|
14
|
-
To get the api key, please refer to the following link:
|
|
15
|
-
https://docs.mem0.ai/platform/quickstart
|
|
16
|
-
"""
|
|
17
|
-
|
|
18
|
-
def __init__(self, **kwargs):
|
|
19
|
-
super().__init__(**kwargs)
|
|
20
|
-
self.service = None
|
|
21
|
-
self._health = False
|
|
22
|
-
|
|
23
|
-
@staticmethod
|
|
24
|
-
async def get_query_text(message: Message) -> str:
|
|
25
|
-
"""
|
|
26
|
-
Get the query text from the message.
|
|
27
|
-
"""
|
|
28
|
-
if message:
|
|
29
|
-
if message.type == MessageType.MESSAGE:
|
|
30
|
-
for content in message.content:
|
|
31
|
-
if content.type == ContentType.TEXT:
|
|
32
|
-
return content.text
|
|
33
|
-
return ""
|
|
34
|
-
|
|
35
|
-
@staticmethod
|
|
36
|
-
def transform_message(message: Message) -> dict:
|
|
37
|
-
content_text = None
|
|
38
|
-
|
|
39
|
-
try:
|
|
40
|
-
if hasattr(message, "content") and isinstance(
|
|
41
|
-
message.content,
|
|
42
|
-
list,
|
|
43
|
-
):
|
|
44
|
-
if len(message.content) > 0 and hasattr(
|
|
45
|
-
message.content[0],
|
|
46
|
-
"text",
|
|
47
|
-
):
|
|
48
|
-
content_text = message.content[0].text
|
|
49
|
-
except (AttributeError, IndexError):
|
|
50
|
-
# Log error or handle appropriately
|
|
51
|
-
pass
|
|
52
|
-
|
|
53
|
-
return {
|
|
54
|
-
"role": getattr(message, "role", None),
|
|
55
|
-
"content": content_text,
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
async def transform_messages(self, messages: List[Message]) -> List[dict]:
|
|
59
|
-
return [self.transform_message(message) for message in messages]
|
|
60
|
-
|
|
61
|
-
async def start(self):
|
|
62
|
-
mem0_api_key = os.getenv("MEM0_API_KEY")
|
|
63
|
-
if mem0_api_key is None:
|
|
64
|
-
raise ValueError("MEM0_API_KEY is not set")
|
|
65
|
-
mem0_api_key = os.getenv("MEM0_API_KEY")
|
|
66
|
-
|
|
67
|
-
# get the mem0 client instance
|
|
68
|
-
self.service = AsyncMemoryClient(api_key=mem0_api_key)
|
|
69
|
-
self._health = True
|
|
70
|
-
|
|
71
|
-
async def stop(self):
|
|
72
|
-
self.service = None
|
|
73
|
-
self._health = False
|
|
74
|
-
|
|
75
|
-
async def health(self):
|
|
76
|
-
return self._health
|
|
77
|
-
|
|
78
|
-
async def add_memory(
|
|
79
|
-
self,
|
|
80
|
-
user_id: str,
|
|
81
|
-
messages: list,
|
|
82
|
-
session_id: Optional[str] = None,
|
|
83
|
-
):
|
|
84
|
-
messages = await self.transform_messages(messages)
|
|
85
|
-
return await self.service.add(
|
|
86
|
-
messages=messages,
|
|
87
|
-
user_id=user_id,
|
|
88
|
-
run_id=session_id,
|
|
89
|
-
# async_mode=True,
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
async def search_memory(
|
|
93
|
-
self,
|
|
94
|
-
user_id: str,
|
|
95
|
-
messages: list,
|
|
96
|
-
filters: Optional[Dict[str, Any]] = None,
|
|
97
|
-
) -> list:
|
|
98
|
-
query = await self.get_query_text(messages[-1])
|
|
99
|
-
kwargs = {
|
|
100
|
-
"query": query,
|
|
101
|
-
"user_id": user_id,
|
|
102
|
-
}
|
|
103
|
-
if filters:
|
|
104
|
-
kwargs["filters"] = filters
|
|
105
|
-
return await self.service.search(**kwargs)
|
|
106
|
-
|
|
107
|
-
async def list_memory(
|
|
108
|
-
self,
|
|
109
|
-
user_id: str,
|
|
110
|
-
filters: Optional[Dict[str, Any]] = None,
|
|
111
|
-
) -> list:
|
|
112
|
-
kwargs = {"user_id": user_id}
|
|
113
|
-
if filters:
|
|
114
|
-
kwargs["filters"] = filters
|
|
115
|
-
return await self.service.get_all(**kwargs)
|
|
116
|
-
|
|
117
|
-
async def delete_memory(
|
|
118
|
-
self,
|
|
119
|
-
user_id: str,
|
|
120
|
-
session_id: Optional[str] = None,
|
|
121
|
-
) -> None:
|
|
122
|
-
if session_id:
|
|
123
|
-
return await self.service.delete_all(
|
|
124
|
-
user_id=user_id,
|
|
125
|
-
run_id=session_id,
|
|
126
|
-
)
|
|
127
|
-
else:
|
|
128
|
-
return await self.service.delete_all(user_id=user_id)
|
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
from abc import abstractmethod
|
|
3
|
-
from typing import Optional, Dict, Any
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
from pydantic import Field
|
|
7
|
-
|
|
8
|
-
from ..base import ServiceWithLifecycleManager
|
|
9
|
-
from ...schemas.agent_schemas import MessageType, Message
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class MemoryService(ServiceWithLifecycleManager):
|
|
13
|
-
"""
|
|
14
|
-
Used to store and retrieve long memory from the database or in-memory.
|
|
15
|
-
The memory is organized by the user id at top level, under which there are
|
|
16
|
-
two different memory manage strategies,
|
|
17
|
-
- one is the message grouped by the session id, the session id is under
|
|
18
|
-
the user id,
|
|
19
|
-
- the other is the message grouped by the user id only
|
|
20
|
-
"""
|
|
21
|
-
|
|
22
|
-
@abstractmethod
|
|
23
|
-
async def add_memory(
|
|
24
|
-
self,
|
|
25
|
-
user_id: str,
|
|
26
|
-
messages: list,
|
|
27
|
-
session_id: Optional[str] = None,
|
|
28
|
-
) -> None:
|
|
29
|
-
"""
|
|
30
|
-
Adds messages to the memory service.
|
|
31
|
-
|
|
32
|
-
Args:
|
|
33
|
-
user_id: The user id.
|
|
34
|
-
messages: The messages to add.
|
|
35
|
-
session_id: The session id, which is optional.
|
|
36
|
-
"""
|
|
37
|
-
|
|
38
|
-
async def stop(self):
|
|
39
|
-
raise NotImplementedError
|
|
40
|
-
|
|
41
|
-
async def start(self):
|
|
42
|
-
raise NotImplementedError
|
|
43
|
-
|
|
44
|
-
@abstractmethod
|
|
45
|
-
async def search_memory(
|
|
46
|
-
self,
|
|
47
|
-
user_id: str,
|
|
48
|
-
messages: list,
|
|
49
|
-
filters: Optional[Dict[str, Any]] = Field(
|
|
50
|
-
description="Associated filters for the messages, "
|
|
51
|
-
"such as top_k, score etc.",
|
|
52
|
-
default=None,
|
|
53
|
-
),
|
|
54
|
-
) -> list:
|
|
55
|
-
"""
|
|
56
|
-
Searches messages from the memory service.
|
|
57
|
-
|
|
58
|
-
Args:
|
|
59
|
-
user_id: The user id.
|
|
60
|
-
messages: The user query or the query with history messages,
|
|
61
|
-
both in the format of list of messages. If messages is a list,
|
|
62
|
-
the search will be based on the content of the last message.
|
|
63
|
-
filters: The filters used to search memory
|
|
64
|
-
"""
|
|
65
|
-
|
|
66
|
-
@abstractmethod
|
|
67
|
-
async def list_memory(
|
|
68
|
-
self,
|
|
69
|
-
user_id: str,
|
|
70
|
-
filters: Optional[Dict[str, Any]] = Field(
|
|
71
|
-
description="Associated filters for the messages, "
|
|
72
|
-
"such as top_k, score etc.",
|
|
73
|
-
default=None,
|
|
74
|
-
),
|
|
75
|
-
) -> list:
|
|
76
|
-
"""
|
|
77
|
-
Lists the memory items for a given user with filters, such as
|
|
78
|
-
page_num, page_size, etc.
|
|
79
|
-
|
|
80
|
-
Args:
|
|
81
|
-
user_id: The user id.
|
|
82
|
-
filters: The filters for the memory items.
|
|
83
|
-
"""
|
|
84
|
-
|
|
85
|
-
@abstractmethod
|
|
86
|
-
async def delete_memory(
|
|
87
|
-
self,
|
|
88
|
-
user_id: str,
|
|
89
|
-
session_id: Optional[str] = None,
|
|
90
|
-
) -> None:
|
|
91
|
-
"""
|
|
92
|
-
Deletes the memory items for a given user with certain session id,
|
|
93
|
-
or all the memory items for a given user.
|
|
94
|
-
"""
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
class InMemoryMemoryService(MemoryService):
|
|
98
|
-
"""
|
|
99
|
-
An in-memory implementation of the memory service.
|
|
100
|
-
"""
|
|
101
|
-
|
|
102
|
-
_DEFAULT_SESSION_ID = "default"
|
|
103
|
-
|
|
104
|
-
def __init__(self) -> None:
|
|
105
|
-
"""Initializes the InMemorySessionHistoryService."""
|
|
106
|
-
self._store: Optional[Dict[str, Dict[str, list]]] = None
|
|
107
|
-
self._health = False
|
|
108
|
-
|
|
109
|
-
async def start(self) -> None:
|
|
110
|
-
"""Initialize the in-memory store."""
|
|
111
|
-
if self._store is None:
|
|
112
|
-
self._store = {}
|
|
113
|
-
self._health = True
|
|
114
|
-
|
|
115
|
-
async def stop(self) -> None:
|
|
116
|
-
"""Stops the service."""
|
|
117
|
-
if self._store is not None:
|
|
118
|
-
self._store.clear()
|
|
119
|
-
self._store = None
|
|
120
|
-
self._health = False
|
|
121
|
-
|
|
122
|
-
async def health(self) -> bool:
|
|
123
|
-
"""Checks the health of the service."""
|
|
124
|
-
return self._health
|
|
125
|
-
|
|
126
|
-
async def add_memory(
|
|
127
|
-
self,
|
|
128
|
-
user_id: str,
|
|
129
|
-
messages: list,
|
|
130
|
-
session_id: Optional[str] = None,
|
|
131
|
-
) -> None:
|
|
132
|
-
"""
|
|
133
|
-
Adds messages to the in-memory store.
|
|
134
|
-
|
|
135
|
-
Args:
|
|
136
|
-
user_id: The user's unique identifier.
|
|
137
|
-
messages: A list of messages to be added.
|
|
138
|
-
session_id: An optional session identifier. If not provided,
|
|
139
|
-
a default session is used.
|
|
140
|
-
"""
|
|
141
|
-
if self._store is None:
|
|
142
|
-
raise RuntimeError("Service not started")
|
|
143
|
-
|
|
144
|
-
if user_id not in self._store:
|
|
145
|
-
self._store[user_id] = {}
|
|
146
|
-
|
|
147
|
-
storage_key = session_id if session_id else self._DEFAULT_SESSION_ID
|
|
148
|
-
|
|
149
|
-
if storage_key not in self._store[user_id]:
|
|
150
|
-
self._store[user_id][storage_key] = []
|
|
151
|
-
|
|
152
|
-
if messages:
|
|
153
|
-
self._store[user_id][storage_key].extend(messages)
|
|
154
|
-
|
|
155
|
-
async def search_memory(
|
|
156
|
-
self,
|
|
157
|
-
user_id: str,
|
|
158
|
-
messages: list,
|
|
159
|
-
filters: Optional[Dict[str, Any]] = None,
|
|
160
|
-
) -> list:
|
|
161
|
-
"""
|
|
162
|
-
Searches messages from the in-memory store for a specific user
|
|
163
|
-
based on keywords.
|
|
164
|
-
|
|
165
|
-
Args:
|
|
166
|
-
user_id: The user's unique identifier.
|
|
167
|
-
messages: A list of messages, where the last message's content
|
|
168
|
-
is used as the search query.
|
|
169
|
-
filters: Optional filters to apply, such as 'top_k' to limit the
|
|
170
|
-
number of returned messages.
|
|
171
|
-
|
|
172
|
-
Returns:
|
|
173
|
-
A list of matching messages from the store.
|
|
174
|
-
"""
|
|
175
|
-
if self._store is None:
|
|
176
|
-
raise RuntimeError("Service not started")
|
|
177
|
-
|
|
178
|
-
if user_id not in self._store:
|
|
179
|
-
return []
|
|
180
|
-
|
|
181
|
-
if (
|
|
182
|
-
not messages
|
|
183
|
-
or not isinstance(messages, list)
|
|
184
|
-
or len(messages) == 0
|
|
185
|
-
):
|
|
186
|
-
return []
|
|
187
|
-
|
|
188
|
-
message = messages[-1]
|
|
189
|
-
query = await self.get_query_text(message)
|
|
190
|
-
if not query:
|
|
191
|
-
return []
|
|
192
|
-
|
|
193
|
-
keywords = set(query.lower().split())
|
|
194
|
-
|
|
195
|
-
all_messages = []
|
|
196
|
-
for session_messages in self._store[user_id].values():
|
|
197
|
-
all_messages.extend(session_messages)
|
|
198
|
-
|
|
199
|
-
matched_messages = []
|
|
200
|
-
for msg in all_messages:
|
|
201
|
-
candidate_content = await self.get_query_text(msg)
|
|
202
|
-
if candidate_content:
|
|
203
|
-
msg_content_lower = candidate_content.lower()
|
|
204
|
-
if any(keyword in msg_content_lower for keyword in keywords):
|
|
205
|
-
matched_messages.append(msg)
|
|
206
|
-
|
|
207
|
-
if (
|
|
208
|
-
filters
|
|
209
|
-
and "top_k" in filters
|
|
210
|
-
and isinstance(filters["top_k"], int)
|
|
211
|
-
):
|
|
212
|
-
return matched_messages[-filters["top_k"] :]
|
|
213
|
-
|
|
214
|
-
return matched_messages
|
|
215
|
-
|
|
216
|
-
async def get_query_text(self, message: Message) -> str:
|
|
217
|
-
"""
|
|
218
|
-
Gets the query text from the messages.
|
|
219
|
-
|
|
220
|
-
Args:
|
|
221
|
-
message: A list of messages.
|
|
222
|
-
|
|
223
|
-
Returns:
|
|
224
|
-
The query text.
|
|
225
|
-
"""
|
|
226
|
-
if message:
|
|
227
|
-
if message.type == MessageType.MESSAGE:
|
|
228
|
-
for content in message.content:
|
|
229
|
-
if content.type == "text":
|
|
230
|
-
return content.text
|
|
231
|
-
return ""
|
|
232
|
-
|
|
233
|
-
async def list_memory(
|
|
234
|
-
self,
|
|
235
|
-
user_id: str,
|
|
236
|
-
filters: Optional[Dict[str, Any]] = None,
|
|
237
|
-
) -> list:
|
|
238
|
-
"""
|
|
239
|
-
Lists messages from the in-memory store with pagination support.
|
|
240
|
-
|
|
241
|
-
Args:
|
|
242
|
-
user_id: The user's unique identifier.
|
|
243
|
-
filters: Optional filters for pagination, including 'page_num'
|
|
244
|
-
and 'page_size'.
|
|
245
|
-
|
|
246
|
-
Returns:
|
|
247
|
-
A paginated list of messages.
|
|
248
|
-
"""
|
|
249
|
-
if self._store is None:
|
|
250
|
-
raise RuntimeError("Service not started")
|
|
251
|
-
|
|
252
|
-
if user_id not in self._store:
|
|
253
|
-
return []
|
|
254
|
-
|
|
255
|
-
all_messages = []
|
|
256
|
-
# Sort by session id to have a consistent order for pagination
|
|
257
|
-
for session_id in sorted(self._store[user_id].keys()):
|
|
258
|
-
all_messages.extend(self._store[user_id][session_id])
|
|
259
|
-
|
|
260
|
-
page_num = filters.get("page_num", 1) if filters else 1
|
|
261
|
-
page_size = filters.get("page_size", 10) if filters else 10
|
|
262
|
-
|
|
263
|
-
start_index = (page_num - 1) * page_size
|
|
264
|
-
end_index = start_index + page_size
|
|
265
|
-
|
|
266
|
-
return all_messages[start_index:end_index]
|
|
267
|
-
|
|
268
|
-
async def delete_memory(
|
|
269
|
-
self,
|
|
270
|
-
user_id: str,
|
|
271
|
-
session_id: Optional[str] = None,
|
|
272
|
-
) -> None:
|
|
273
|
-
"""
|
|
274
|
-
Deletes messages from the in-memory store.
|
|
275
|
-
|
|
276
|
-
Args:
|
|
277
|
-
user_id: The user's unique identifier.
|
|
278
|
-
session_id: If provided, only deletes the messages for that
|
|
279
|
-
session. Otherwise, deletes all messages for the user.
|
|
280
|
-
"""
|
|
281
|
-
if self._store is None:
|
|
282
|
-
raise RuntimeError("Service not started")
|
|
283
|
-
|
|
284
|
-
if user_id not in self._store:
|
|
285
|
-
return
|
|
286
|
-
|
|
287
|
-
if session_id:
|
|
288
|
-
if session_id in self._store[user_id]:
|
|
289
|
-
del self._store[user_id][session_id]
|
|
290
|
-
else:
|
|
291
|
-
if user_id in self._store:
|
|
292
|
-
del self._store[user_id]
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
|
|
3
|
-
from typing import Callable, Dict
|
|
4
|
-
|
|
5
|
-
from ..service_factory import ServiceFactory
|
|
6
|
-
from .memory_service import MemoryService, InMemoryMemoryService
|
|
7
|
-
from .redis_memory_service import RedisMemoryService
|
|
8
|
-
from .mem0_memory_service import Mem0MemoryService
|
|
9
|
-
from .reme_personal_memory_service import ReMePersonalMemoryService
|
|
10
|
-
from .reme_task_memory_service import ReMeTaskMemoryService
|
|
11
|
-
|
|
12
|
-
try:
|
|
13
|
-
from .tablestore_memory_service import (
|
|
14
|
-
TablestoreMemoryService,
|
|
15
|
-
SearchStrategy,
|
|
16
|
-
)
|
|
17
|
-
|
|
18
|
-
TABLESTORE_AVAILABLE = True
|
|
19
|
-
except ImportError:
|
|
20
|
-
TABLESTORE_AVAILABLE = False
|
|
21
|
-
SearchStrategy = None # type: ignore
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
class MemoryServiceFactory(ServiceFactory[MemoryService]):
|
|
25
|
-
"""
|
|
26
|
-
Factory for MemoryService, supporting both environment variables and
|
|
27
|
-
kwargs.
|
|
28
|
-
|
|
29
|
-
Usage examples:
|
|
30
|
-
1. Startup using only environment variables:
|
|
31
|
-
export MEMORY_BACKEND=redis
|
|
32
|
-
export MEMORY_REDIS_REDIS_URL="redis://localhost:6379/5"
|
|
33
|
-
service = await MemoryServiceFactory.create()
|
|
34
|
-
|
|
35
|
-
2. Override environment variables using arguments:
|
|
36
|
-
export MEMORY_BACKEND=redis
|
|
37
|
-
export MEMORY_REDIS_REDIS_URL="redis://localhost:6379/5"
|
|
38
|
-
service = await MemoryServiceFactory.create(
|
|
39
|
-
redis_url="redis://otherhost:6379/1"
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
3. Register a custom backend:
|
|
43
|
-
from my_backend import PostgresMemoryService
|
|
44
|
-
MemoryServiceFactory.register_backend(
|
|
45
|
-
"postgres",
|
|
46
|
-
PostgresMemoryService,
|
|
47
|
-
)
|
|
48
|
-
export MEMORY_BACKEND=postgres
|
|
49
|
-
export MEMORY_POSTGRES_DSN="postgresql://user:pass@localhost/db"
|
|
50
|
-
service = await MemoryServiceFactory.create()
|
|
51
|
-
"""
|
|
52
|
-
|
|
53
|
-
_registry: Dict[str, Callable[..., MemoryService]] = {}
|
|
54
|
-
_env_prefix = "MEMORY_"
|
|
55
|
-
_default_backend = "in_memory"
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
# === Default built-in backend registration ===
|
|
59
|
-
|
|
60
|
-
MemoryServiceFactory.register_backend(
|
|
61
|
-
"in_memory",
|
|
62
|
-
lambda **kwargs: InMemoryMemoryService(),
|
|
63
|
-
)
|
|
64
|
-
|
|
65
|
-
MemoryServiceFactory.register_backend(
|
|
66
|
-
"redis",
|
|
67
|
-
RedisMemoryService,
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
MemoryServiceFactory.register_backend(
|
|
71
|
-
"mem0",
|
|
72
|
-
Mem0MemoryService,
|
|
73
|
-
)
|
|
74
|
-
|
|
75
|
-
MemoryServiceFactory.register_backend(
|
|
76
|
-
"reme_personal",
|
|
77
|
-
ReMePersonalMemoryService,
|
|
78
|
-
)
|
|
79
|
-
|
|
80
|
-
MemoryServiceFactory.register_backend(
|
|
81
|
-
"reme_task",
|
|
82
|
-
ReMeTaskMemoryService,
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
if TABLESTORE_AVAILABLE:
|
|
86
|
-
|
|
87
|
-
def _create_tablestore_memory_service(**kwargs):
|
|
88
|
-
"""Helper function to create a TablestoreMemoryService instance."""
|
|
89
|
-
search_strategy = kwargs.get("search_strategy")
|
|
90
|
-
if isinstance(search_strategy, str):
|
|
91
|
-
search_strategy = SearchStrategy(search_strategy.lower())
|
|
92
|
-
elif search_strategy is None:
|
|
93
|
-
search_strategy = SearchStrategy.FULL_TEXT
|
|
94
|
-
|
|
95
|
-
return TablestoreMemoryService(
|
|
96
|
-
tablestore_client=kwargs["tablestore_client"],
|
|
97
|
-
search_strategy=search_strategy,
|
|
98
|
-
embedding_model=kwargs.get("embedding_model"),
|
|
99
|
-
vector_dimension=int(kwargs.get("vector_dimension", 1536)),
|
|
100
|
-
table_name=kwargs.get("table_name", "agentscope_runtime_memory"),
|
|
101
|
-
search_index_schema=kwargs.get("search_index_schema"),
|
|
102
|
-
text_field=kwargs.get("text_field", "text"),
|
|
103
|
-
embedding_field=kwargs.get("embedding_field", "embedding"),
|
|
104
|
-
vector_metric_type=kwargs.get("vector_metric_type"),
|
|
105
|
-
**{
|
|
106
|
-
k: v
|
|
107
|
-
for k, v in kwargs.items()
|
|
108
|
-
if k
|
|
109
|
-
not in [
|
|
110
|
-
"tablestore_client",
|
|
111
|
-
"search_strategy",
|
|
112
|
-
"embedding_model",
|
|
113
|
-
"vector_dimension",
|
|
114
|
-
"table_name",
|
|
115
|
-
"search_index_schema",
|
|
116
|
-
"text_field",
|
|
117
|
-
"embedding_field",
|
|
118
|
-
"vector_metric_type",
|
|
119
|
-
]
|
|
120
|
-
},
|
|
121
|
-
)
|
|
122
|
-
|
|
123
|
-
MemoryServiceFactory.register_backend(
|
|
124
|
-
"tablestore",
|
|
125
|
-
_create_tablestore_memory_service,
|
|
126
|
-
)
|