agentscope-runtime 0.2.0b2__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/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} +12 -18
- 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 -2
- agentscope_runtime/sandbox/client/http_client.py +1 -0
- agentscope_runtime/sandbox/enums.py +2 -0
- 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.0b2.dist-info → agentscope_runtime-1.0.0.dist-info}/METADATA +240 -168
- agentscope_runtime-1.0.0.dist-info/RECORD +240 -0
- {agentscope_runtime-0.2.0b2.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 -220
- 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.0b2.dist-info/RECORD +0 -183
- {agentscope_runtime-0.2.0b2.dist-info → agentscope_runtime-1.0.0.dist-info}/WHEEL +0 -0
- {agentscope_runtime-0.2.0b2.dist-info → agentscope_runtime-1.0.0.dist-info}/licenses/LICENSE +0 -0
- {agentscope_runtime-0.2.0b2.dist-info → agentscope_runtime-1.0.0.dist-info}/top_level.txt +0 -0
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# pylint:disable=protected-access,unused-argument
|
|
3
|
+
"""AgentScope Long Memory implementation based on MemoryService."""
|
|
4
|
+
import asyncio
|
|
5
|
+
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from agentscope.memory import LongTermMemoryBase
|
|
9
|
+
from agentscope.message import Msg, TextBlock, ThinkingBlock
|
|
10
|
+
from agentscope.tool import ToolResponse
|
|
11
|
+
|
|
12
|
+
from ..message import agentscope_msg_to_message
|
|
13
|
+
from ....engine.services.memory import MemoryService
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class AgentScopeLongTermMemory(LongTermMemoryBase):
|
|
17
|
+
"""
|
|
18
|
+
AgentScope Long Memory subclass based on LongTermMemoryBase.
|
|
19
|
+
|
|
20
|
+
This class stores messages in an underlying MemoryService instance.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
service (MemoryService): The backend memory service.
|
|
24
|
+
user_id (str): The user ID linked to this memory.
|
|
25
|
+
session_id (str): The session ID linked to this memory.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(
|
|
29
|
+
self,
|
|
30
|
+
service: MemoryService,
|
|
31
|
+
user_id: str,
|
|
32
|
+
session_id: str,
|
|
33
|
+
):
|
|
34
|
+
super().__init__()
|
|
35
|
+
self._service = service
|
|
36
|
+
self.user_id = user_id
|
|
37
|
+
self.session_id = session_id
|
|
38
|
+
|
|
39
|
+
async def record(
|
|
40
|
+
self,
|
|
41
|
+
msgs: list[Msg | None],
|
|
42
|
+
**kwargs: Any,
|
|
43
|
+
) -> None:
|
|
44
|
+
"""
|
|
45
|
+
Record a list of messages into the memory service.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
msgs (list[Msg | None]):
|
|
49
|
+
A list of AgentScope `Msg` objects to store. `None` entries
|
|
50
|
+
in the list will be ignored. If the list is empty, nothing
|
|
51
|
+
will be recorded.
|
|
52
|
+
**kwargs (Any):
|
|
53
|
+
Additional keyword arguments, currently unused but kept for
|
|
54
|
+
compatibility/future extensions.
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
None
|
|
58
|
+
"""
|
|
59
|
+
if not msgs:
|
|
60
|
+
return
|
|
61
|
+
|
|
62
|
+
messages = agentscope_msg_to_message(msgs)
|
|
63
|
+
await self._service.add_memory(
|
|
64
|
+
user_id=self.user_id,
|
|
65
|
+
session_id=self.session_id,
|
|
66
|
+
messages=messages,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
async def retrieve(
|
|
70
|
+
self,
|
|
71
|
+
msg: Msg | list[Msg] | None,
|
|
72
|
+
limit: int = 5,
|
|
73
|
+
**kwargs: Any,
|
|
74
|
+
) -> str:
|
|
75
|
+
"""Retrieve the content from the long-term memory.
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
msg (`Msg | list[Msg] | None`):
|
|
79
|
+
The message to search for in the memory, which should be
|
|
80
|
+
specific and concise, e.g. the person's name, the date, the
|
|
81
|
+
location, etc.
|
|
82
|
+
limit (`int`, optional):
|
|
83
|
+
The maximum number of memories to retrieve per search, i.e.,
|
|
84
|
+
the number of memories to retrieve for the message. if the
|
|
85
|
+
message is a list of messages, the limit will be applied to
|
|
86
|
+
each message. If the message is a single message, then the
|
|
87
|
+
limit is the total number of memories to retrieve for the
|
|
88
|
+
message.
|
|
89
|
+
**kwargs (`Any`):
|
|
90
|
+
Additional keyword arguments.
|
|
91
|
+
|
|
92
|
+
Returns:
|
|
93
|
+
`str`:
|
|
94
|
+
The retrieved memory
|
|
95
|
+
"""
|
|
96
|
+
|
|
97
|
+
if not msg:
|
|
98
|
+
# Build a none message
|
|
99
|
+
msg = [
|
|
100
|
+
Msg(
|
|
101
|
+
name="assistant",
|
|
102
|
+
content=[
|
|
103
|
+
TextBlock(
|
|
104
|
+
type="text",
|
|
105
|
+
text="",
|
|
106
|
+
),
|
|
107
|
+
],
|
|
108
|
+
role="assistant",
|
|
109
|
+
),
|
|
110
|
+
]
|
|
111
|
+
|
|
112
|
+
if isinstance(msg, Msg):
|
|
113
|
+
msg = [msg]
|
|
114
|
+
|
|
115
|
+
tasks = [
|
|
116
|
+
self._service.search_memory(
|
|
117
|
+
user_id=self.user_id,
|
|
118
|
+
messages=agentscope_msg_to_message(m),
|
|
119
|
+
filters={"top_k": limit},
|
|
120
|
+
)
|
|
121
|
+
for m in msg
|
|
122
|
+
]
|
|
123
|
+
results_lists = await asyncio.gather(*tasks, return_exceptions=True)
|
|
124
|
+
|
|
125
|
+
processed_results = []
|
|
126
|
+
for result in results_lists:
|
|
127
|
+
if isinstance(result, Exception):
|
|
128
|
+
processed_results.append(f"Error: {result}")
|
|
129
|
+
else:
|
|
130
|
+
processed_results.append("\n".join(str(r) for r in result))
|
|
131
|
+
|
|
132
|
+
# Convert results to string
|
|
133
|
+
return "\n".join(processed_results)
|
|
134
|
+
|
|
135
|
+
async def record_to_memory(
|
|
136
|
+
self,
|
|
137
|
+
thinking: str,
|
|
138
|
+
content: list[str],
|
|
139
|
+
**kwargs: Any,
|
|
140
|
+
) -> ToolResponse:
|
|
141
|
+
"""Use this function to record important information that you may
|
|
142
|
+
need later. The target content should be specific and concise, e.g.
|
|
143
|
+
who, when, where, do what, why, how, etc.
|
|
144
|
+
|
|
145
|
+
Args:
|
|
146
|
+
thinking (`str`):
|
|
147
|
+
Your thinking and reasoning about what to record
|
|
148
|
+
content (`list[str]`):
|
|
149
|
+
The content to remember, which is a list of strings.
|
|
150
|
+
"""
|
|
151
|
+
# Building agentscope msgs
|
|
152
|
+
try:
|
|
153
|
+
thinking_blocks = [
|
|
154
|
+
ThinkingBlock(
|
|
155
|
+
type="thinking",
|
|
156
|
+
thinking=thinking,
|
|
157
|
+
),
|
|
158
|
+
]
|
|
159
|
+
|
|
160
|
+
text_blocks = [
|
|
161
|
+
TextBlock(
|
|
162
|
+
type="text",
|
|
163
|
+
text=cnt,
|
|
164
|
+
)
|
|
165
|
+
for cnt in content
|
|
166
|
+
]
|
|
167
|
+
|
|
168
|
+
msgs = [
|
|
169
|
+
Msg(
|
|
170
|
+
name="assistant",
|
|
171
|
+
content=thinking_blocks + text_blocks,
|
|
172
|
+
role="assistant",
|
|
173
|
+
),
|
|
174
|
+
]
|
|
175
|
+
|
|
176
|
+
await self.record(msgs)
|
|
177
|
+
|
|
178
|
+
return ToolResponse(
|
|
179
|
+
content=[
|
|
180
|
+
TextBlock(
|
|
181
|
+
type="text",
|
|
182
|
+
text="Successfully recorded content to memory",
|
|
183
|
+
),
|
|
184
|
+
],
|
|
185
|
+
)
|
|
186
|
+
except Exception as e:
|
|
187
|
+
return ToolResponse(
|
|
188
|
+
content=[
|
|
189
|
+
TextBlock(
|
|
190
|
+
type="text",
|
|
191
|
+
text=f"Error recording content to memory: {str(e)}",
|
|
192
|
+
),
|
|
193
|
+
],
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
async def retrieve_from_memory(
|
|
197
|
+
self,
|
|
198
|
+
keywords: list[str],
|
|
199
|
+
limit: int = 5,
|
|
200
|
+
**kwargs: Any,
|
|
201
|
+
) -> ToolResponse:
|
|
202
|
+
"""Retrieve the memory based on the given keywords.
|
|
203
|
+
|
|
204
|
+
Args:
|
|
205
|
+
keywords (`list[str]`):
|
|
206
|
+
Concise search cues—such as a person's name, a specific date,
|
|
207
|
+
a location, or a short description of the memory you want to
|
|
208
|
+
retrieve. Each keyword is executed as an independent query
|
|
209
|
+
against the memory store.
|
|
210
|
+
limit (`int`, optional):
|
|
211
|
+
The maximum number of memories to retrieve per search, i.e.,
|
|
212
|
+
the number of memories to retrieve for each keyword.
|
|
213
|
+
Returns:
|
|
214
|
+
`ToolResponse`:
|
|
215
|
+
A ToolResponse containing the retrieved memories as JSON text.
|
|
216
|
+
"""
|
|
217
|
+
|
|
218
|
+
try:
|
|
219
|
+
results = []
|
|
220
|
+
msgs = []
|
|
221
|
+
|
|
222
|
+
for keyword in keywords:
|
|
223
|
+
msgs.append(
|
|
224
|
+
Msg(
|
|
225
|
+
name="assistant",
|
|
226
|
+
content=[
|
|
227
|
+
TextBlock(
|
|
228
|
+
type="text",
|
|
229
|
+
text=keyword,
|
|
230
|
+
),
|
|
231
|
+
],
|
|
232
|
+
role="assistant",
|
|
233
|
+
),
|
|
234
|
+
)
|
|
235
|
+
results = await self.retrieve(
|
|
236
|
+
msgs,
|
|
237
|
+
limit=limit,
|
|
238
|
+
**kwargs,
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
return ToolResponse(
|
|
242
|
+
content=[
|
|
243
|
+
TextBlock(
|
|
244
|
+
type="text",
|
|
245
|
+
text=results,
|
|
246
|
+
),
|
|
247
|
+
],
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
except Exception as e:
|
|
251
|
+
return ToolResponse(
|
|
252
|
+
content=[
|
|
253
|
+
TextBlock(
|
|
254
|
+
type="text",
|
|
255
|
+
text=f"Error retrieving memory: {str(e)}",
|
|
256
|
+
),
|
|
257
|
+
],
|
|
258
|
+
)
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# pylint:disable=protected-access
|
|
3
|
+
"""AgentScope Memory implementation based on SessionHistoryService."""
|
|
4
|
+
import functools
|
|
5
|
+
|
|
6
|
+
from typing import Union
|
|
7
|
+
|
|
8
|
+
from agentscope.memory import MemoryBase
|
|
9
|
+
from agentscope.message import Msg
|
|
10
|
+
|
|
11
|
+
from ..message import agentscope_msg_to_message, message_to_agentscope_msg
|
|
12
|
+
from ....engine.services.session_history import SessionHistoryService
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def ensure_session(func):
|
|
16
|
+
"""
|
|
17
|
+
Async decorator that ensures the AgentScope session exists before
|
|
18
|
+
method execution.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
@functools.wraps(func)
|
|
22
|
+
async def wrapper(self, *args, **kwargs):
|
|
23
|
+
await self._check_session()
|
|
24
|
+
return await func(self, *args, **kwargs)
|
|
25
|
+
|
|
26
|
+
return wrapper
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class AgentScopeSessionHistoryMemory(MemoryBase):
|
|
30
|
+
"""
|
|
31
|
+
AgentScope Memory subclass based on MemoryBase.
|
|
32
|
+
|
|
33
|
+
This class stores messages in an underlying SessionHistoryService instance.
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
service (SessionHistoryService): The backend session history service.
|
|
37
|
+
user_id (str): The user ID linked to this memory.
|
|
38
|
+
session_id (str): The session ID linked to this memory.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
def __init__(
|
|
42
|
+
self,
|
|
43
|
+
service: SessionHistoryService,
|
|
44
|
+
user_id: str,
|
|
45
|
+
session_id: str,
|
|
46
|
+
):
|
|
47
|
+
super().__init__()
|
|
48
|
+
self._service = service
|
|
49
|
+
self.user_id = user_id
|
|
50
|
+
self.session_id = session_id
|
|
51
|
+
self._session = None
|
|
52
|
+
|
|
53
|
+
async def _check_session(self) -> None:
|
|
54
|
+
"""
|
|
55
|
+
Check if the session exists in the backend.
|
|
56
|
+
"""
|
|
57
|
+
# Always check for session to stay in sync with backend
|
|
58
|
+
self._session = await self._service.get_session(
|
|
59
|
+
self.user_id,
|
|
60
|
+
self.session_id,
|
|
61
|
+
)
|
|
62
|
+
if self._session is None:
|
|
63
|
+
self._session = await self._service.create_session(
|
|
64
|
+
self.user_id,
|
|
65
|
+
self.session_id,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
def state_dict(self):
|
|
69
|
+
"""
|
|
70
|
+
Get current memory state as a dictionary.
|
|
71
|
+
Always fetch from backend to ensure consistency.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
def load_state_dict(
|
|
75
|
+
self,
|
|
76
|
+
state_dict: dict,
|
|
77
|
+
strict: bool = True,
|
|
78
|
+
) -> None:
|
|
79
|
+
"""
|
|
80
|
+
Load memory state from a dictionary into backend storage.
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
@ensure_session
|
|
84
|
+
async def size(self) -> int:
|
|
85
|
+
"""The size of the memory."""
|
|
86
|
+
current_message = self._session.messages
|
|
87
|
+
agentscope_msg = message_to_agentscope_msg(current_message)
|
|
88
|
+
return len(agentscope_msg)
|
|
89
|
+
|
|
90
|
+
@ensure_session
|
|
91
|
+
async def add(
|
|
92
|
+
self,
|
|
93
|
+
memories: Union[list[Msg], Msg, None],
|
|
94
|
+
allow_duplicates: bool = False, # pylint: disable=unused-argument
|
|
95
|
+
) -> None:
|
|
96
|
+
"""Add messages to backend service."""
|
|
97
|
+
if memories is None:
|
|
98
|
+
return
|
|
99
|
+
if isinstance(memories, Msg):
|
|
100
|
+
memories = [memories]
|
|
101
|
+
if not isinstance(memories, list):
|
|
102
|
+
raise TypeError(f"Expected Msg or list[Msg], got {type(memories)}")
|
|
103
|
+
|
|
104
|
+
# Convert Msg -> backend Message
|
|
105
|
+
backend_messages = agentscope_msg_to_message(memories)
|
|
106
|
+
|
|
107
|
+
if self._session:
|
|
108
|
+
await self._service.append_message(self._session, backend_messages)
|
|
109
|
+
|
|
110
|
+
@ensure_session
|
|
111
|
+
async def delete(self, index: Union[list[int], int]) -> None:
|
|
112
|
+
"""
|
|
113
|
+
Delete messages by index
|
|
114
|
+
"""
|
|
115
|
+
# TODO: add method to delete messages instead of replacement
|
|
116
|
+
if isinstance(index, int):
|
|
117
|
+
index = [index]
|
|
118
|
+
|
|
119
|
+
current_message = self._session.messages
|
|
120
|
+
agentscope_msg = message_to_agentscope_msg(current_message)
|
|
121
|
+
|
|
122
|
+
invalid_index = [_ for _ in index if _ < 0 or _ >= len(agentscope_msg)]
|
|
123
|
+
|
|
124
|
+
if invalid_index:
|
|
125
|
+
raise IndexError(
|
|
126
|
+
f"The index {invalid_index} does not exist.",
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
agentscope_msg = [
|
|
130
|
+
_ for idx, _ in enumerate(agentscope_msg) if idx not in index
|
|
131
|
+
]
|
|
132
|
+
|
|
133
|
+
await self.clear()
|
|
134
|
+
await self.add(agentscope_msg)
|
|
135
|
+
|
|
136
|
+
@ensure_session
|
|
137
|
+
async def clear(self) -> None:
|
|
138
|
+
"""Clear backend session memory."""
|
|
139
|
+
await self._service.delete_session(
|
|
140
|
+
user_id=self.user_id,
|
|
141
|
+
session_id=self.session_id,
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
@ensure_session
|
|
145
|
+
async def get_memory(self) -> list[Msg]:
|
|
146
|
+
"""
|
|
147
|
+
Retrieve memory content.
|
|
148
|
+
For sync purposes, we reload from backend before returning.
|
|
149
|
+
"""
|
|
150
|
+
current_message = self._session.messages
|
|
151
|
+
agentscope_msg = message_to_agentscope_msg(current_message)
|
|
152
|
+
return agentscope_msg
|