mirror of
https://github.com/basicmachines-co/basic-memory
synced 2026-06-21 13:47:35 +00:00
remove activity router, replace with memory/recent
This commit is contained in:
@@ -6,7 +6,7 @@ from fastapi import FastAPI
|
||||
from loguru import logger
|
||||
|
||||
from basic_memory import db
|
||||
from .routers import knowledge, discovery, search, activity, memory, resource
|
||||
from .routers import knowledge, discovery, search, memory, resource
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -29,7 +29,6 @@ app = FastAPI(
|
||||
# Include routers
|
||||
app.include_router(knowledge.router)
|
||||
app.include_router(discovery.router)
|
||||
app.include_router(activity.router)
|
||||
app.include_router(search.router)
|
||||
app.include_router(memory.router)
|
||||
app.include_router(resource.router)
|
||||
@@ -4,7 +4,6 @@ from . import knowledge_router as knowledge
|
||||
from . import discovery_router as discovery
|
||||
from . import memory_router as memory
|
||||
from . import resource_router as resource
|
||||
from . import activity_router as activity
|
||||
from . import search_router as search
|
||||
|
||||
__all__ = ["knowledge", "discovery", "memory", "resource", "activity", "search"]
|
||||
__all__ = ["knowledge", "discovery", "memory", "resource", "search"]
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
"""Activity router for tracking recent changes."""
|
||||
|
||||
from typing import List, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from loguru import logger
|
||||
|
||||
from basic_memory.deps import get_activity_service
|
||||
from basic_memory.services.activity_service import ActivityService
|
||||
from basic_memory.schemas.activity import RecentActivity, ActivityType
|
||||
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/activity",
|
||||
tags=["activity"]
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/recent",
|
||||
response_model=RecentActivity,
|
||||
summary="Get recent activity"
|
||||
)
|
||||
async def get_recent_activity(
|
||||
activity_service: ActivityService = Depends(get_activity_service),
|
||||
timeframe: str = "1d",
|
||||
activity_types: Optional[List[ActivityType]] = Query(None),
|
||||
) -> RecentActivity:
|
||||
"""
|
||||
Get recent activity across the knowledge base.
|
||||
|
||||
Args:
|
||||
timeframe: Time window to look back (1h, 1d, 1w, 1m)
|
||||
activity_types: Optional list of ActivityType values to include
|
||||
|
||||
Returns:
|
||||
RecentActivity with changes and summary
|
||||
"""
|
||||
logger.debug(
|
||||
f"Getting recent activity (timeframe={timeframe}, "
|
||||
f"types={activity_types})"
|
||||
)
|
||||
|
||||
return await activity_service.get_recent_activity(
|
||||
timeframe=timeframe,
|
||||
activity_types=[t.value for t in activity_types] if activity_types else None,
|
||||
)
|
||||
@@ -1,14 +1,17 @@
|
||||
"""Routes for memory:// URI operations."""
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
from typing import Optional, List, Annotated
|
||||
|
||||
from fastapi import APIRouter
|
||||
from dateparser import parse
|
||||
from fastapi import APIRouter, Query
|
||||
from loguru import logger
|
||||
|
||||
from basic_memory.config import config
|
||||
from basic_memory.deps import ContextServiceDep, EntityRepositoryDep
|
||||
from basic_memory.repository import EntityRepository
|
||||
from basic_memory.repository.search_repository import SearchIndexRow
|
||||
from basic_memory.schemas.base import TimeFrame
|
||||
from basic_memory.schemas.memory import (
|
||||
MemoryUrl,
|
||||
GraphContext,
|
||||
@@ -23,49 +26,8 @@ from basic_memory.services.context_service import ContextResultRow
|
||||
router = APIRouter(prefix="/memory", tags=["memory"])
|
||||
|
||||
|
||||
def parse_timeframe(timeframe: str) -> Optional[datetime]:
|
||||
"""Convert timeframe string to datetime.
|
||||
|
||||
Formats:
|
||||
- 7d: 7 days ago
|
||||
- 30d: 30 days ago
|
||||
- None: no time limit
|
||||
"""
|
||||
if not timeframe:
|
||||
return None
|
||||
|
||||
if not timeframe.endswith("d"):
|
||||
raise ValueError("Timeframe must be in days (e.g., '7d')")
|
||||
|
||||
days = int(timeframe[:-1])
|
||||
return datetime.utcnow() - timedelta(days=days)
|
||||
|
||||
|
||||
@router.get("/{uri:path}", response_model=GraphContext)
|
||||
async def get_memory_context(
|
||||
context_service: ContextServiceDep,
|
||||
entity_repository: EntityRepositoryDep,
|
||||
uri: str,
|
||||
depth: int = 1,
|
||||
timeframe: str = "7d",
|
||||
max_results: int = 10,
|
||||
) -> GraphContext:
|
||||
"""Get rich context from memory:// URI."""
|
||||
# add the project name from the config to the url as the "host
|
||||
# Parse URI
|
||||
logger.debug(
|
||||
f"Getting context for URI: `{uri}` depth: `{depth}` timeframe: `{timeframe}` max_results: `{max_results}`"
|
||||
)
|
||||
memory_url = MemoryUrl(f"memory://{config.project}/{uri}")
|
||||
|
||||
# Parse timeframe
|
||||
since = parse_timeframe(timeframe)
|
||||
|
||||
# Build context
|
||||
context = await context_service.build_context(
|
||||
memory_url, depth=depth, since=since, max_results=max_results
|
||||
)
|
||||
|
||||
async def to_graph_context(context, entity_repository: EntityRepository):
|
||||
# return results
|
||||
async def to_summary(item: SearchIndexRow | ContextResultRow):
|
||||
match item.type:
|
||||
@@ -81,10 +43,9 @@ async def get_memory_context(
|
||||
category=item.category, content=item.content, permalink=item.permalink
|
||||
)
|
||||
case SearchItemType.RELATION:
|
||||
|
||||
from_entity = await entity_repository.find_by_id(item.from_id)
|
||||
to_entity = await entity_repository.find_by_id(item.to_id)
|
||||
|
||||
|
||||
return RelationSummary(
|
||||
permalink=item.permalink,
|
||||
type=item.type,
|
||||
@@ -96,9 +57,69 @@ async def get_memory_context(
|
||||
primary_results = [await to_summary(r) for r in context["primary_results"]]
|
||||
related_results = [await to_summary(r) for r in context["related_results"]]
|
||||
metadata = MemoryMetadata.model_validate(context["metadata"])
|
||||
|
||||
# Transform to GraphContext
|
||||
return GraphContext(
|
||||
primary_results=primary_results, related_results=related_results, metadata=metadata
|
||||
)
|
||||
|
||||
|
||||
|
||||
@router.get("/recent", response_model=GraphContext)
|
||||
async def recent(
|
||||
context_service: ContextServiceDep,
|
||||
entity_repository: EntityRepositoryDep,
|
||||
types: Annotated[list[SearchItemType] | None, Query()] = None,
|
||||
depth: int = 1,
|
||||
timeframe: TimeFrame = "7d",
|
||||
max_results: int = 10,
|
||||
) -> GraphContext:
|
||||
# return all types by default
|
||||
types = (
|
||||
[SearchItemType.ENTITY, SearchItemType.RELATION, SearchItemType.OBSERVATION]
|
||||
if not types
|
||||
else types
|
||||
)
|
||||
|
||||
logger.debug(
|
||||
f"Getting recent context: `{types}` depth: `{depth}` timeframe: `{timeframe}` max_results: `{max_results}`"
|
||||
)
|
||||
# Parse timeframe
|
||||
since = parse(timeframe)
|
||||
|
||||
# Build context
|
||||
context = await context_service.build_context(
|
||||
types=types, depth=depth, since=since, max_results=max_results
|
||||
)
|
||||
return await to_graph_context(context, entity_repository=entity_repository)
|
||||
|
||||
|
||||
# get_memory_context needs to be declared last so other paths can match
|
||||
|
||||
@router.get("/{uri:path}", response_model=GraphContext)
|
||||
async def get_memory_context(
|
||||
context_service: ContextServiceDep,
|
||||
entity_repository: EntityRepositoryDep,
|
||||
uri: str,
|
||||
depth: int = 1,
|
||||
timeframe: TimeFrame = "7d",
|
||||
max_results: int = 10,
|
||||
) -> GraphContext:
|
||||
"""Get rich context from memory:// URI."""
|
||||
# add the project name from the config to the url as the "host
|
||||
# Parse URI
|
||||
logger.debug(
|
||||
f"Getting context for URI: `{uri}` depth: `{depth}` timeframe: `{timeframe}` max_results: `{max_results}`"
|
||||
)
|
||||
memory_url = MemoryUrl(f"memory://{config.project}/{uri}")
|
||||
|
||||
# Parse timeframe
|
||||
since = parse(timeframe)
|
||||
|
||||
# Build context
|
||||
context = await context_service.build_context(
|
||||
memory_url, depth=depth, since=since, max_results=max_results
|
||||
)
|
||||
return await to_graph_context(context, entity_repository=entity_repository)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user