nodestep.middleware.memory ¶
Long-term memory for the model, stored as JSON files by FilesystemMemory.
See Memory.
| Name | What it is |
|---|---|
| Middleware | |
BaseMemory | Base class for middleware that gives the model long-term memory. |
FilesystemMemory | Memory tools that store entries as UTF-8 JSON files under root_dir. |
| Entries and tool results | |
Memory | A stored memory entry. |
MemorySearchResult | Result of the search_memory tool. |
MemoryListResult | Memories of one scope, the result of the list_memories tool. |
MemoryDeleteResult | Result of the delete_memory tool. |
| Storage and search | |
save_memory_to_disk | Write a memory as JSON, keeping its original creation time. |
load_memories_from_disk | Load all memories of a scope. |
delete_memory_from_disk | Delete one memory. |
bm25_search | Rank memories against a query with BM25 keyword scoring. |
| Errors | |
MemoryConfigError | Raised when FilesystemMemory is configured with an unusable root or scope. |
Middleware¶
BaseMemory¶
Base class for middleware that gives the model long-term memory.
Subclass of Middleware.
FilesystemMemory¶
FilesystemMemory(root_dir, *, scope)
Memory tools that store entries as UTF-8 JSON files under root_dir.
Subclass of BaseMemory.
Entries of scope "user:42" live in root_dir/memories/user/42/.
root_dir | str | Path |
Storage directory; created on the first write. |
scope | str | Callable[[ToolContext], str] |
The scope, or a function of the call's |
MemoryConfigError |
If |
TypeError |
If |
Methods
scope_for(ctx)¶
Return the scope of a tool call.
scope_for(ctx)¶scope_for(ctx) -> str
ctx | ToolContext | |
TypeError |
If the scope callable returns something other than a |
Entries and tool results¶
Memory¶
Memory(*, key, title, content, memory_type='fact', scope='global', created_at=None, updated_at=None)
A stored memory entry.
key | str |
Name of the entry within its scope; saving the same key replaces it. |
title | str | |
content | str | |
memory_type | str |
Free label chosen by the model, such as |
scope | str |
Scope such as |
created_at | datetime | None |
Set to the current time when not given; a save keeps the stored one. |
updated_at | datetime | None |
Set to the current time when not given and on every save. |
Methods
searchable_text()¶
Text used for keyword search.
searchable_text()¶searchable_text() -> str
MemorySearchResult¶
MemorySearchResult(*, query, results, skipped=[])
Result of the search_memory tool.
query | str | |
results | list[Memory] |
Matching memories, best first. |
skipped | list[str] |
Memory files that could not be read. |
MemoryListResult¶
MemoryListResult(*, scope, entries, skipped=[])
Memories of one scope, the result of the list_memories tool.
scope | str | |
entries | list[Memory] | |
skipped | list[str] |
Memory files that could not be read. |
MemoryDeleteResult¶
MemoryDeleteResult(*, key, scope, deleted)
Result of the delete_memory tool.
key | str | |
scope | str | |
deleted | bool |
Whether an entry with the key existed and was deleted. |
Storage and search¶
save_memory_to_disk¶
load_memories_from_disk¶
load_memories_from_disk(root_dir, scope) -> MemoryListResult
Load all memories of a scope.
root_dir | Path |
Folder that holds the |
scope | str |
Scope such as |
MemoryListResult |
The memories, and in |
delete_memory_from_disk¶
delete_memory_from_disk(root_dir, scope, key) -> bool
Delete one memory.
root_dir | Path |
Folder that holds the |
scope | str | |
key | str | |
bool |
Whether a memory was deleted. |
bm25_search¶
bm25_search(memories, query, top_k=10, k1=1.2, b=0.75) -> list[Memory]
Rank memories against a query with BM25 keyword scoring.
memories | list[Memory] | |
query | str |
Words to look for in the titles and contents. |
top_k | int |
Maximum number of results. |
k1 | float |
BM25 term saturation: how much repeated words count. |
b | float |
BM25 length normalization, from 0 (none) to 1 (full). |
list[Memory] |
Matching memories, best first. |
ValueError |
If |
Errors¶
MemoryConfigError¶
Raised when FilesystemMemory is configured with an unusable root or scope.
Subclass of NodestepError.