feat: add watch to mcp process (#83)

This commit is contained in:
Paul Hernandez
2025-04-06 18:33:10 -05:00
committed by GitHub
parent 617e60bda4
commit 00c8633cfc
6 changed files with 92 additions and 24 deletions
+42 -3
View File
@@ -1,5 +1,6 @@
"""FastAPI application for basic-memory knowledge graph API."""
import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
@@ -7,16 +8,54 @@ from fastapi.exception_handlers import http_exception_handler
from loguru import logger
from basic_memory import db
from basic_memory.config import config as app_config
from basic_memory.api.routers import knowledge, search, memory, resource, project_info
from basic_memory.api.routers import knowledge, memory, project_info, resource, search
from basic_memory.config import config as project_config
from basic_memory.config import config_manager
from basic_memory.sync import SyncService, WatchService
async def run_background_sync(sync_service: SyncService, watch_service: WatchService): # pragma: no cover
logger.info(f"Starting watch service to sync file changes in dir: {project_config.home}")
# full sync
await sync_service.sync(project_config.home, show_progress=False)
# watch changes
await watch_service.run()
@asynccontextmanager
async def lifespan(app: FastAPI): # pragma: no cover
"""Lifecycle manager for the FastAPI app."""
await db.run_migrations(app_config)
await db.run_migrations(project_config)
# app config
basic_memory_config = config_manager.load_config()
logger.info(f"Sync changes enabled: {basic_memory_config.sync_changes}")
logger.info(f"Update permalinks on move enabled: {basic_memory_config.update_permalinks_on_move}")
watch_task = None
if basic_memory_config.sync_changes:
# import after migrations have run
from basic_memory.cli.commands.sync import get_sync_service
sync_service = await get_sync_service()
watch_service = WatchService(
sync_service=sync_service,
file_service=sync_service.entity_service.file_service,
config=project_config,
)
watch_task = asyncio.create_task(run_background_sync(sync_service, watch_service))
else:
logger.info("Sync changes disabled. Skipping watch service.")
# proceed with startup
yield
logger.info("Shutting down Basic Memory API")
if watch_task:
watch_task.cancel()
await db.shutdown_db()