strands-stash-memory-manager 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.
- stash_strands/__init__.py +11 -0
- stash_strands/memory_manager.py +164 -0
- stash_strands/memory_store.py +1084 -0
- stash_strands/py.typed +0 -0
- stash_strands/types.py +37 -0
- strands_stash_memory_manager-0.1.0.dist-info/METADATA +63 -0
- strands_stash_memory_manager-0.1.0.dist-info/RECORD +11 -0
- strands_stash_memory_manager-0.1.0.dist-info/WHEEL +5 -0
- strands_stash_memory_manager-0.1.0.dist-info/licenses/LICENSE +202 -0
- strands_stash_memory_manager-0.1.0.dist-info/licenses/NOTICE +4 -0
- strands_stash_memory_manager-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"""Strands memory manager for a Stash mount that the agent reads as files."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from typing import TYPE_CHECKING, cast
|
|
7
|
+
|
|
8
|
+
from mcp import stdio_client
|
|
9
|
+
from strands.hooks import BeforeInvocationEvent
|
|
10
|
+
from strands.memory import MemoryManager
|
|
11
|
+
from strands.tools.mcp import MCPClient, MCPTransport
|
|
12
|
+
from typing_extensions import Unpack
|
|
13
|
+
|
|
14
|
+
from .memory_store import StashMemoryStore
|
|
15
|
+
from .types import StashConfig
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from strands import Agent
|
|
19
|
+
from strands.types.content import SystemContentBlock
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# `MemoryManager` takes each of these from its caller. A Stash mount has one
|
|
23
|
+
# correct value for each, and the other value breaks the agent, so this manager
|
|
24
|
+
# sets them and refuses them. The text says what this manager does instead.
|
|
25
|
+
_MANAGER_KEYS = {
|
|
26
|
+
"stores": "this manager holds the one store that it builds from config_path",
|
|
27
|
+
"injection": "the agent asks for a search, so the manager pastes none into each turn",
|
|
28
|
+
"add_tool_config": "the store writes whole turns through add_messages, and has no add",
|
|
29
|
+
"search_tool_config": "the agent searches with the `search` tool of `stash mcp`, or with "
|
|
30
|
+
"`stash search` in a shell, so the manager registers no search tool",
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
class StrandsStash(MemoryManager):
|
|
34
|
+
"""The memory of a Strands agent that reads a Stash mount.
|
|
35
|
+
|
|
36
|
+
It builds one `StashMemoryStore` from the same parameters, and takes the
|
|
37
|
+
two choices that the store needs: the mount's session context in the
|
|
38
|
+
agent's system prompt, and no search on each turn.
|
|
39
|
+
|
|
40
|
+
The agent reaches the mount in one of two ways:
|
|
41
|
+
|
|
42
|
+
- By default, with the tools of `stash mcp`: `search`, `status`, and
|
|
43
|
+
the tools of each source of the mount. When the mount has `[extraction]`,
|
|
44
|
+
`stash mcp` also gives it `save_note`. A hit names a file of the mount,
|
|
45
|
+
and the agent opens it with a file tool or a shell that the application
|
|
46
|
+
gives it. So this process must be able to read the mount point, and the
|
|
47
|
+
manager checks that when the agent starts.
|
|
48
|
+
- With `mcp=False`, with `ls`, `cat`, `grep`, and `stash search` in a
|
|
49
|
+
shell, which the application must give the agent. The manager registers
|
|
50
|
+
no tool.
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
agent = Agent(
|
|
54
|
+
model=model,
|
|
55
|
+
system_prompt=SYSTEM_PROMPT,
|
|
56
|
+
memory_manager=StrandsStash(
|
|
57
|
+
config_path="/abs/path/stash.toml",
|
|
58
|
+
session_id="session-123",
|
|
59
|
+
mount_name="agentcore-example",
|
|
60
|
+
),
|
|
61
|
+
)
|
|
62
|
+
```
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
def __init__(self, **config: Unpack[StashConfig]) -> None:
|
|
66
|
+
"""Build the store of one mount, with no search on each turn."""
|
|
67
|
+
for key, reason in _MANAGER_KEYS.items():
|
|
68
|
+
if key in config:
|
|
69
|
+
raise ValueError(
|
|
70
|
+
f"StrandsStash: {key} is not an option, because {reason}. Remove it. "
|
|
71
|
+
f"A MemoryManager applies injection to every store that it holds, "
|
|
72
|
+
f"and a Stash mount needs it off, so this manager holds one store "
|
|
73
|
+
f"and sets it itself."
|
|
74
|
+
)
|
|
75
|
+
store = StashMemoryStore(**config)
|
|
76
|
+
super().__init__(
|
|
77
|
+
stores=[store],
|
|
78
|
+
injection=False,
|
|
79
|
+
search_tool_config=False,
|
|
80
|
+
)
|
|
81
|
+
self._store = store
|
|
82
|
+
self._session_id = config["session_id"]
|
|
83
|
+
|
|
84
|
+
@property
|
|
85
|
+
def store(self) -> StashMemoryStore:
|
|
86
|
+
"""Return the store that this manager built."""
|
|
87
|
+
return self._store
|
|
88
|
+
|
|
89
|
+
async def init_agent(self, agent: Agent) -> None:
|
|
90
|
+
"""Put the session context of the mount in the agent's system prompt.
|
|
91
|
+
|
|
92
|
+
Getting that text is what proves the mount, so a mount that does not
|
|
93
|
+
serve raises and the prompt keeps the text that the application wrote.
|
|
94
|
+
Strands runs this inside `Agent(...)`, so the construction fails. The
|
|
95
|
+
alternative is worse: an agent whose system prompt sends it to files
|
|
96
|
+
that are not there.
|
|
97
|
+
"""
|
|
98
|
+
# The store has no `initialize`, so the one call below is what proves the
|
|
99
|
+
# mount. The base call registers the manager, and asks the store nothing.
|
|
100
|
+
await super().init_agent(agent)
|
|
101
|
+
context = await self._store.session_context()
|
|
102
|
+
if self._store.mcp:
|
|
103
|
+
await self._store.check_readable()
|
|
104
|
+
# Each child process that the app starts for the agent, such as its
|
|
105
|
+
# shell, gets the session of its notes, as a child of a harness does.
|
|
106
|
+
# shortcut: the process has one session, that of the newest agent; an
|
|
107
|
+
# app that serves two sessions at once in one process needs a session
|
|
108
|
+
# for each shell.
|
|
109
|
+
os.environ["STRANDS_SESSION_ID"] = self._session_id
|
|
110
|
+
agent.system_prompt = _with_session_context(agent.system_prompt_content, context)
|
|
111
|
+
if self._store.mcp:
|
|
112
|
+
server = self._store.mcp_server()
|
|
113
|
+
# As for `Agent(tools=[client])`: the agent's tool registry starts the
|
|
114
|
+
# client, and `agent.cleanup()` stops it.
|
|
115
|
+
# `MCPTransport` names the streams of `mcp` 1.x, and Strands takes the
|
|
116
|
+
# streams of 2.x too.
|
|
117
|
+
agent.tool_registry.process_tools(
|
|
118
|
+
[MCPClient(lambda: cast(MCPTransport, stdio_client(server)))]
|
|
119
|
+
)
|
|
120
|
+
agent.add_hook(self._ask_for_a_note, BeforeInvocationEvent)
|
|
121
|
+
|
|
122
|
+
def _ask_for_a_note(self, event: BeforeInvocationEvent) -> None:
|
|
123
|
+
"""Give a due note request to the agent, at the end of its new prompt.
|
|
124
|
+
|
|
125
|
+
The store gets the request with a write-back, after an earlier
|
|
126
|
+
invocation, so the agent reads it with its next prompt. An invocation
|
|
127
|
+
with no new prompt of the user keeps the request for the next one: in
|
|
128
|
+
a message of the agent, the model reads the request as its own words.
|
|
129
|
+
"""
|
|
130
|
+
if not event.messages:
|
|
131
|
+
return
|
|
132
|
+
*earlier, last = event.messages
|
|
133
|
+
content = last.get("content")
|
|
134
|
+
if last.get("role") != "user" or not isinstance(content, list):
|
|
135
|
+
return
|
|
136
|
+
request = self._store.take_note_request()
|
|
137
|
+
if request is None:
|
|
138
|
+
return
|
|
139
|
+
event.messages = [*earlier, {**last, "content": [*content, {"text": request}]}]
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _with_session_context(
|
|
143
|
+
blocks: list[SystemContentBlock] | None, context: str
|
|
144
|
+
) -> list[SystemContentBlock]:
|
|
145
|
+
"""Return the system prompt with the session context in its cached part.
|
|
146
|
+
|
|
147
|
+
It adds a block, not a string. `Agent.system_prompt` gives back only the
|
|
148
|
+
text of the prompt, so a round trip through it would drop a cache point that
|
|
149
|
+
the application set. The two leading newlines keep the context apart from
|
|
150
|
+
the prompt, because a model provider can put the blocks next to each other
|
|
151
|
+
with no separator.
|
|
152
|
+
|
|
153
|
+
A cache point ends a cached prefix, so the context goes before the last one
|
|
154
|
+
and not after it. The context is the same for the life of the agent, which
|
|
155
|
+
makes it the best part of the prompt to cache; after the last cache point,
|
|
156
|
+
the model would read it again on each turn.
|
|
157
|
+
"""
|
|
158
|
+
added: SystemContentBlock = {"text": f"\n\n{context}"}
|
|
159
|
+
prompt = list(blocks or [])
|
|
160
|
+
for index in range(len(prompt) - 1, -1, -1):
|
|
161
|
+
if "cachePoint" in prompt[index]:
|
|
162
|
+
prompt.insert(index, added)
|
|
163
|
+
return prompt
|
|
164
|
+
return [*prompt, added]
|