simplify memory_url

This commit is contained in:
phernandez
2025-01-15 17:28:22 -06:00
parent 344fc70f3a
commit b529e18584
2 changed files with 14 additions and 43 deletions
+11 -21
View File
@@ -3,32 +3,26 @@
The memory:// URL scheme provides a unified way to address knowledge across projects:
memory://project-name/path/to/content
The host portion (project-name) identifies the knowledge base context, while
the path represents a relative permalink within that project.
Examples:
memory://basic-memory/specs/search/* # Pattern matching
memory://basic-memory/topic/search~ranking # Fuzzy search
memory://basic-memory/specs/link-resolution # Exact permalink
memory://basic-memory/related/sync # Related content
memory://basic-memory/context/current # Context building
memory://basic-memory/specs/search/* # Pattern matching
memory://basic-memory/specs/xyz # Exact permalink
memory://basic-memory/related/sync # Related content
"""
from typing import Optional, Dict, Any
from pydantic import BaseModel, Field, field_validator, ValidationError
from pydantic import BaseModel, Field, field_validator
class MemoryUrl(BaseModel):
"""memory:// URL scheme for knowledge addressing."""
scheme: str = Field(default="memory", frozen=True)
host: str
path: str
host: str # Project identifier
path: str # Full path
# Special fields for pattern matching and context
pattern: Optional[str] = None
fuzzy: Optional[str] = None
params: Dict[str, Any] = Field(default_factory=dict)
# Query params
pattern: Optional[str] = None # Path pattern if * present
params: Dict[str, Any] = Field(default_factory=dict) # For special modes like 'related'
@field_validator("scheme")
@classmethod
@@ -61,7 +55,7 @@ class MemoryUrl(BaseModel):
raise ValueError("URL must include both host and path")
host, path = parts
path = "/" + path # Add leading slash for consistency
path = "/" + path # Add leading slash
# Create base URL
url = cls(scheme=scheme, host=host, path=path)
@@ -69,14 +63,10 @@ class MemoryUrl(BaseModel):
# Parse special patterns
path_no_slash = path[1:] if path.startswith('/') else path
# Handle glob patterns - keep * for FTS5
# Handle glob patterns - preserve * for FTS
if '*' in path_no_slash:
url.pattern = path_no_slash
# Handle fuzzy search
if '~' in path_no_slash:
url.fuzzy = path_no_slash.replace('~', ' ')
# Extract special prefixes
segments = path_no_slash.split('/')
if segments and segments[0] in {'related', 'context'}:
+3 -22
View File
@@ -12,26 +12,15 @@ def test_basic_permalink():
assert url.host == "basic-memory"
assert url.path == "/specs/search"
assert url.pattern is None
assert url.fuzzy is None
assert url.params == {}
def test_glob_pattern():
"""Test glob pattern conversion."""
"""Test pattern matching."""
url = MemoryUrl.parse("memory://basic-memory/specs/search/*")
assert url.host == "basic-memory"
assert url.path == "/specs/search/*"
assert url.pattern == "specs/search/%"
assert url.fuzzy is None
def test_fuzzy_search():
"""Test fuzzy search term parsing."""
url = MemoryUrl.parse("memory://basic-memory/topic/search~ranking")
assert url.host == "basic-memory"
assert url.path == "/topic/search~ranking"
assert url.pattern is None
assert url.fuzzy == "topic/search ranking"
assert url.pattern == "specs/search/*"
def test_related_prefix():
@@ -73,15 +62,7 @@ def test_complex_pattern():
url = MemoryUrl.parse("memory://basic-memory/specs/*/search/*")
assert url.host == "basic-memory"
assert url.path == "/specs/*/search/*"
assert url.pattern == "specs/%/search/%"
def test_complex_fuzzy():
"""Test multiple fuzzy terms."""
url = MemoryUrl.parse("memory://basic-memory/specs/search~ranking~performance")
assert url.host == "basic-memory"
assert url.path == "/specs/search~ranking~performance"
assert url.fuzzy == "specs/search ranking performance"
assert url.pattern == "specs/*/search/*"
def test_url_reconstruction():