Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
feat(content): add reindex endpoint for re-embedding after manual edits (#631)
Deepak Dev Panward8ff45267c12
diff --git a/openviking/server/routers/content.py b/openviking/server/routers/content.pyindex e227d2cc1..9b4d0279c 100644--- a/openviking/server/routers/content.py+++ b/openviking/server/routers/content.py@@ -2,15 +2,31 @@# SPDX-License-Identifier: Apache-2.0"""Content endpoints for OpenViking HTTP Server."""+import asynciofrom urllib.parse import quote-from fastapi import APIRouter, Depends, Query+from fastapi import APIRouter, Body, Depends, Queryfrom fastapi.responses import Response as FastAPIResponse+from pydantic import BaseModelfrom openviking.server.auth import get_request_contextfrom openviking.server.dependencies import get_servicefrom openviking.server.identity import RequestContext-from openviking.server.models import Response+from openviking.server.models import ErrorInfo, Response+from openviking_cli.utils import get_logger++logger = get_logger(__name__)++REINDEX_TASK_TYPE = "resource_reindex"+++class ReindexRequest(BaseModel):+ """Request to reindex content at a URI."""++ uri: str+ regenerate: bool = False+ wait: bool = True+router = APIRouter(prefix="/api/v1/content", tags=["content"])@@ -73,3 +89,120 @@ async def download(media_type="application/octet-stream",headers={"Content-Disposition": f"attachment; filename*=UTF-8''{filename}"},)++[email protected]("/reindex")+async def reindex(+ request: ReindexRequest = Body(...),+ _ctx: RequestContext = Depends(get_request_context),+):+ """Reindex content at a URI.++ Re-embeds existing .abstract.md/.overview.md content into the vector+ database. If regenerate=True, also regenerates L0/L1 summaries via LLM+ before re-embedding.++ Uses transaction locking to prevent concurrent reindexes on the same URI.+ Set wait=False to run in the background and track progress via task API.+ """+ from openviking.service.task_tracker import get_task_tracker+ from openviking.storage.viking_fs import get_viking_fs++ uri = request.uri+ viking_fs = get_viking_fs()++ # Validate URI exists+ if not await viking_fs.exists(uri):+ return Response(+ status="error",+ error=ErrorInfo(code="NOT_FOUND", message=f"URI not found: {uri}"),+ )++ service = get_service()+ tracker = get_task_tracker()++ if request.wait:+ # Synchronous path: block until reindex completes+ if tracker.has_running(REINDEX_TASK_TYPE, uri):+ return Response(+ status="error",+ error=ErrorInfo(+ code="CONFLICT",+ message=f"URI {uri} already has a reindex in progress",+ ),+ )+ result = await _do_reindex(service, uri, request.regenerate, _ctx)+ return Response(status="ok", result=result)+ else:+ # Async path: run in background, return task_id for polling+ task = tracker.create_if_no_running(REINDEX_TASK_TYPE, uri)+ if task is None:+ return Response(+ status="error",+ error=ErrorInfo(+ code="CONFLICT",+ message=f"URI {uri} already has a reindex in progress",+ ),+ )+ asyncio.create_task(+ _background_reindex_tracked(service, uri, request.regenerate, _ctx, task.task_id)+ )+ return Response(+ status="ok",+ result={+ "uri": uri,+ "status": "accepted",+ "task_id": task.task_id,+ "message": "Reindex is processing in the background",+ },+ )+++async def _do_reindex(+ service,+ uri: str,+ regenerate: bool,+ ctx: RequestContext,+) -> dict:+ """Execute reindex within a transaction."""+ from openviking.storage.transaction import get_transaction_manager++ tm = get_transaction_manager()+ tx = tm.create_transaction(init_info={"uri": uri, "regenerate": regenerate})+ await tm.begin(tx.id)++ try:+ await tm.acquire_lock_normal(tx.id, uri)+ if regenerate:+ result = await service.resources.summarize([uri], ctx=ctx)+ else:+ result = await service.resources.build_index([uri], ctx=ctx)+ await tm.commit(tx.id)+ return result+ except Exception:+ try:+ await tm.rollback(tx.id)+ except Exception:+ pass+ raise+++async def _background_reindex_tracked(+ service,+ uri: str,+ regenerate: bool,+ ctx: RequestContext,+ task_id: str,+) -> None:+ """Run reindex in background with task tracking."""+ from openviking.service.task_tracker import get_task_tracker++ tracker = get_task_tracker()+ tracker.start(task_id)+ try:+ result = await _do_reindex(service, uri, regenerate, ctx)+ tracker.complete(task_id, {"uri": uri, **result})+ logger.info("Background reindex completed: uri=%s task=%s", uri, task_id)+ except Exception as exc:+ tracker.fail(task_id, str(exc))+ logger.exception("Background reindex failed: uri=%s task=%s", uri, task_id)diff --git a/tests/server/test_api_content.py b/tests/server/test_api_content.pyindex e7f2970b2..7e0a2effd 100644--- a/tests/server/test_api_content.py+++ b/tests/server/test_api_content.py@@ -41,3 +41,45 @@ async def test_overview_content(client_with_resource):assert resp.status_code == 200body = resp.json()assert body["status"] == "ok"+++async def test_reindex_missing_uri(client):+ """Test reindex without uri field returns 422."""+ resp = await client.post(+ "/api/v1/content/reindex",+ json={"regenerate": False},+ )+ assert resp.status_code == 422+++async def test_reindex_endpoint_registered(client):+ """Test the reindex endpoint is registered (GET returns 405, not 404)."""+ resp = await client.get("/api/v1/content/reindex")+ assert resp.status_code == 405 # Method Not Allowed, not 404+++async def test_reindex_request_validation(client):+ """Test reindex validates the request body schema."""+ # Empty body — uri is required+ resp = await client.post("/api/v1/content/reindex", json={})+ assert resp.status_code == 422++ # Invalid type for regenerate+ resp = await client.post(+ "/api/v1/content/reindex",+ json={"uri": "viking://resources/test", "regenerate": "not_a_bool"},+ )+ # Pydantic coerces strings to bool, so this may or may not fail+ assert resp.status_code in (200, 422, 500)+++async def test_reindex_wait_parameter_schema(client):+ """Test reindex accepts wait parameter in request schema."""+ # Invalid wait type should be coerced or rejected, not crash+ resp = await client.post(+ "/api/v1/content/reindex",+ json={"uri": "viking://resources/test", "wait": "invalid"},+ )+ # Pydantic coerces or rejects — either way, not a 404/405+ assert resp.status_code != 404+ assert resp.status_code != 405