Refactor: single collection with shared/private visibility

Breaking change: per-agent collections replaced by a single 'memories'
collection with metadata-based access control.

- All memories live in one collection
- 'agent' metadata tracks the creator
- 'private' (bool, default False) controls visibility
- Query returns: shared (private=False) + own private (agent=asker)
- Save endpoint accepts optional 'private' field
- Version bumped to 0.2.0
This commit is contained in:
2026-06-25 15:04:13 +02:00
parent 82328b0a45
commit 4a1ab6ae50
2 changed files with 93 additions and 27 deletions

28
main.py
View File

@@ -2,8 +2,10 @@
Endpoints
---------
- ``POST /api/{agent_name}/save`` — Save a memory
- ``POST /api/{agent_name}/query`` — Query memories by semantic similarity
- ``POST /api/{agent_name}/save`` — Save a memory (shared by default,
or private if ``private: true``)
- ``POST /api/{agent_name}/query`` — Query memories visible to the agent
(all shared + its own private ones)
- ``GET /health`` — Health check
"""
@@ -25,8 +27,9 @@ from store import MemoryStore
app = FastAPI(
title="Vector Memory Server",
description="Semantic memory retrieval via REST. "
"Per-agent collections, powered by ChromaDB + all-MiniLM-L6-v2.",
version="0.1.0",
"Shared-by-default, private-opt-in. "
"ChromaDB + all-MiniLM-L6-v2.",
version="0.2.0",
)
store = MemoryStore(Config.DATA_DIR)
@@ -38,6 +41,7 @@ store = MemoryStore(Config.DATA_DIR)
class SaveRequest(BaseModel):
text: str
private: bool = False
type: Optional[str] = "fact"
tags: Optional[list[str]] = None
date: Optional[str] = None
@@ -63,9 +67,15 @@ class QueryResponse(BaseModel):
@app.post("/api/{agent_name}/save", response_model=SaveResponse)
async def save_memory(agent_name: str, request: SaveRequest) -> SaveResponse:
"""Embed and store a memory for the given agent."""
"""Embed and store a memory for *agent_name*.
By default the memory is **shared** (visible to all agents).
Set ``private: true`` to restrict visibility to *agent_name* only.
"""
try:
metadata: dict = {}
metadata: dict = {
"private": request.private,
}
if request.type:
metadata["type"] = request.type
if request.tags:
@@ -83,7 +93,11 @@ async def save_memory(agent_name: str, request: SaveRequest) -> SaveResponse:
async def query_memory(
agent_name: str, request: QueryRequest
) -> QueryResponse:
"""Retrieve semantically similar memories for the given agent."""
"""Retrieve memories visible to *agent_name*.
Returns all **shared** memories from any agent, plus **private**
memories owned by *agent_name*.
"""
try:
results = store.query(agent_name, request.query, request.top_n)
return QueryResponse(results=results)