-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathmemory.py
More file actions
278 lines (211 loc) · 8.69 KB
/
memory.py
File metadata and controls
278 lines (211 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""Schemas for memory context."""
from datetime import datetime
from typing import List, Optional, Annotated, Sequence, Literal, Union, Dict
from annotated_types import MinLen, MaxLen
from pydantic import BaseModel, Field, BeforeValidator, TypeAdapter, field_serializer
from basic_memory.schemas.search import SearchItemType
def validate_memory_url_path(path: str) -> bool:
"""Validate that a memory URL path is well-formed.
Args:
path: The path part of a memory URL (without memory:// prefix)
Returns:
True if the path is valid, False otherwise
Examples:
>>> validate_memory_url_path("specs/search")
True
>>> validate_memory_url_path("memory//test") # Double slash
False
>>> validate_memory_url_path("invalid://test") # Contains protocol
False
"""
# Empty paths are not valid
if not path or not path.strip():
return False
# Check for invalid protocol schemes within the path first (more specific)
if "://" in path:
return False
# Check for double slashes (except at the beginning for absolute paths)
if "//" in path:
return False
# Check for invalid characters (excluding * which is used for pattern matching)
invalid_chars = {"<", ">", '"', "|", "?"}
if any(char in path for char in invalid_chars):
return False
return True
def normalize_memory_url(url: str | None) -> str:
"""Normalize a MemoryUrl string with validation.
Args:
url: A path like "specs/search" or "memory://specs/search"
Returns:
Normalized URL starting with memory://
Raises:
ValueError: If the URL path is malformed
Examples:
>>> normalize_memory_url("specs/search")
'memory://specs/search'
>>> normalize_memory_url("memory://specs/search")
'memory://specs/search'
>>> normalize_memory_url("memory//test")
Traceback (most recent call last):
...
ValueError: Invalid memory URL path: 'memory//test' contains double slashes
"""
if not url:
raise ValueError("Memory URL cannot be empty")
# Strip whitespace for consistency
url = url.strip()
if not url:
raise ValueError("Memory URL cannot be empty or whitespace")
clean_path = url.removeprefix("memory://")
# Validate the extracted path
if not validate_memory_url_path(clean_path):
# Provide specific error messages for common issues
if "://" in clean_path:
raise ValueError(f"Invalid memory URL path: '{clean_path}' contains protocol scheme")
elif "//" in clean_path:
raise ValueError(f"Invalid memory URL path: '{clean_path}' contains double slashes")
else:
raise ValueError(f"Invalid memory URL path: '{clean_path}' contains invalid characters")
return f"memory://{clean_path}"
MemoryUrl = Annotated[
str,
BeforeValidator(str.strip), # Clean whitespace
BeforeValidator(normalize_memory_url), # Validate and normalize the URL
MinLen(1),
MaxLen(2028),
]
memory_url = TypeAdapter(MemoryUrl)
def memory_url_path(url: memory_url) -> str: # pyright: ignore
"""
Returns the uri for a url value by removing the prefix "memory://" from a given MemoryUrl.
This function processes a given MemoryUrl by removing the "memory://"
prefix and returns the resulting string. If the provided url does not
begin with "memory://", the function will simply return the input url
unchanged.
:param url: A MemoryUrl object representing the URL with a "memory://" prefix.
:type url: MemoryUrl
:return: A string representing the URL with the "memory://" prefix removed.
:rtype: str
"""
return url.removeprefix("memory://")
class EntitySummary(BaseModel):
"""Simplified entity representation."""
type: Literal["entity"] = "entity"
permalink: Optional[str]
title: str
content: Optional[str] = None
file_path: str
created_at: Annotated[
datetime, Field(json_schema_extra={"type": "string", "format": "date-time"})
]
@field_serializer("created_at")
def serialize_created_at(self, dt: datetime) -> str:
return dt.isoformat()
class RelationSummary(BaseModel):
"""Simplified relation representation."""
type: Literal["relation"] = "relation"
title: str
file_path: str
permalink: str
relation_type: str
from_entity: Optional[str] = None
to_entity: Optional[str] = None
created_at: Annotated[
datetime, Field(json_schema_extra={"type": "string", "format": "date-time"})
]
@field_serializer("created_at")
def serialize_created_at(self, dt: datetime) -> str:
return dt.isoformat()
class ObservationSummary(BaseModel):
"""Simplified observation representation."""
type: Literal["observation"] = "observation"
title: str
file_path: str
permalink: str
category: str
content: str
created_at: Annotated[
datetime, Field(json_schema_extra={"type": "string", "format": "date-time"})
]
@field_serializer("created_at")
def serialize_created_at(self, dt: datetime) -> str:
return dt.isoformat()
class MemoryMetadata(BaseModel):
"""Simplified response metadata."""
uri: Optional[str] = None
types: Optional[List[SearchItemType]] = None
depth: int
timeframe: Optional[str] = None
generated_at: Annotated[
datetime, Field(json_schema_extra={"type": "string", "format": "date-time"})
]
primary_count: Optional[int] = None # Changed field name
related_count: Optional[int] = None # Changed field name
total_results: Optional[int] = None # For backward compatibility
total_relations: Optional[int] = None
total_observations: Optional[int] = None
@field_serializer("generated_at")
def serialize_generated_at(self, dt: datetime) -> str:
return dt.isoformat()
class ContextResult(BaseModel):
"""Context result containing a primary item with its observations and related items."""
primary_result: Annotated[
Union[EntitySummary, RelationSummary, ObservationSummary],
Field(discriminator="type", description="Primary item"),
]
observations: Sequence[ObservationSummary] = Field(
description="Observations belonging to this entity", default_factory=list
)
related_results: Sequence[
Annotated[
Union[EntitySummary, RelationSummary, ObservationSummary], Field(discriminator="type")
]
] = Field(description="Related items", default_factory=list)
class GraphContext(BaseModel):
"""Complete context response."""
# hierarchical results
results: Sequence[ContextResult] = Field(
description="Hierarchical results with related items nested", default_factory=list
)
# Context metadata
metadata: MemoryMetadata
page: Optional[int] = None
page_size: Optional[int] = None
class ActivityStats(BaseModel):
"""Statistics about activity across all projects."""
total_projects: int
active_projects: int = Field(description="Projects with activity in timeframe")
most_active_project: Optional[str] = None
total_items: int = Field(description="Total items across all projects")
total_entities: int = 0
total_relations: int = 0
total_observations: int = 0
class ProjectActivity(BaseModel):
"""Activity summary for a single project."""
project_name: str
project_path: str
activity: GraphContext = Field(description="The actual activity data for this project")
item_count: int = Field(description="Total items in this project's activity")
last_activity: Optional[
Annotated[datetime, Field(json_schema_extra={"type": "string", "format": "date-time"})]
] = Field(default=None, description="Most recent activity timestamp")
active_folders: List[str] = Field(default_factory=list, description="Most active folders")
@field_serializer("last_activity")
def serialize_last_activity(self, dt: Optional[datetime]) -> Optional[str]:
return dt.isoformat() if dt else None
class ProjectActivitySummary(BaseModel):
"""Summary of activity across all projects."""
projects: Dict[str, ProjectActivity] = Field(
description="Activity per project, keyed by project name"
)
summary: ActivityStats
timeframe: str = Field(description="The timeframe used for the query")
generated_at: Annotated[
datetime, Field(json_schema_extra={"type": "string", "format": "date-time"})
]
guidance: Optional[str] = Field(
default=None, description="Assistant guidance for project selection and session management"
)
@field_serializer("generated_at")
def serialize_generated_at(self, dt: datetime) -> str:
return dt.isoformat()