feat(memory): wire database into reader, writer, and bootstrap
- reader: list_memories() queries memory_meta; lookup_memories() uses FTS5 with fallback to JSON index substring search - writer: write_memory() and append_memory() upsert to DB after every file write - dirs: bootstrap() calls init_db() + migrate_from_files() on startup Existing .md files remain the canonical store; SQLite is the search index. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from pyra.memory import _MEMORY_ROOT
|
||||
@@ -20,6 +21,25 @@ def _resolve_and_validate(name: str) -> Path:
|
||||
return path
|
||||
|
||||
|
||||
def _upsert_to_db(path: Path, content: str, summary: str = "", keywords: list[str] | None = None) -> None:
|
||||
from pyra.memory import database
|
||||
if not database._DB_PATH.exists():
|
||||
return
|
||||
rel = path.relative_to(_MEMORY_ROOT).as_posix()
|
||||
category = rel.split("/")[0] if "/" in rel else "root"
|
||||
stat = path.stat()
|
||||
mtime = datetime.datetime.fromtimestamp(stat.st_mtime).isoformat(timespec="seconds")
|
||||
database.upsert(
|
||||
rel,
|
||||
content=content,
|
||||
category=category,
|
||||
size_bytes=stat.st_size,
|
||||
modified=mtime,
|
||||
summary=summary,
|
||||
keywords=keywords,
|
||||
)
|
||||
|
||||
|
||||
def write_memory(
|
||||
name: str,
|
||||
content: str,
|
||||
@@ -34,6 +54,7 @@ def write_memory(
|
||||
if summary or keywords:
|
||||
rel_key = path.relative_to(_MEMORY_ROOT).as_posix()
|
||||
update_json_entry(rel_key, summary, keywords or [])
|
||||
_upsert_to_db(path, content, summary, keywords)
|
||||
return path
|
||||
|
||||
|
||||
@@ -42,9 +63,12 @@ def append_memory(name: str, content: str) -> Path:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
if path.exists():
|
||||
existing = path.read_text()
|
||||
path.write_text(existing.rstrip() + "\n\n" + content)
|
||||
new_content = existing.rstrip() + "\n\n" + content
|
||||
path.write_text(new_content)
|
||||
else:
|
||||
path.write_text(content)
|
||||
new_content = content
|
||||
path.write_text(new_content)
|
||||
safe_chmod(path, 0o600)
|
||||
update_index()
|
||||
_upsert_to_db(path, new_content)
|
||||
return path
|
||||
|
||||
Reference in New Issue
Block a user