You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.6 KiB
50 lines
1.6 KiB
"""
|
|
routers/index.py — /index and /reindex endpoints.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from fastapi import APIRouter, BackgroundTasks, Depends
|
|
|
|
from core.database import get_pool
|
|
from core.settings import Settings
|
|
from models.requests import IndexRequest, ReindexRequest
|
|
from models.responses import JobResponse
|
|
|
|
router = APIRouter(prefix='/index', tags=['indexing'])
|
|
|
|
|
|
def _get_settings() -> Settings:
|
|
from main import app_settings
|
|
return app_settings
|
|
|
|
|
|
async def _enqueue_job(agent_type: str, payload: dict, pool) -> str:
|
|
job_id = str(uuid.uuid4())
|
|
async with pool.acquire() as conn:
|
|
await conn.execute(
|
|
"""
|
|
INSERT INTO agent_jobs (id, agent_type, payload)
|
|
VALUES ($1::uuid, $2, $3::jsonb)
|
|
""",
|
|
job_id,
|
|
agent_type,
|
|
__import__('json').dumps(payload),
|
|
)
|
|
return job_id
|
|
|
|
|
|
@router.post('', response_model=JobResponse)
|
|
async def index_file(req: IndexRequest, settings: Settings = Depends(_get_settings)):
|
|
pool = await get_pool()
|
|
job_id = await _enqueue_job('ingestion', {'path': req.path, 'force': True}, pool)
|
|
return JobResponse(job_id=job_id, status='pending', message=f'Indexing {req.path}')
|
|
|
|
|
|
@router.post('/reindex', response_model=JobResponse)
|
|
async def reindex_vault(req: ReindexRequest, settings: Settings = Depends(_get_settings)):
|
|
pool = await get_pool()
|
|
job_id = await _enqueue_job('ingestion', {'reindex_all': True, 'force': req.force}, pool)
|
|
return JobResponse(job_id=job_id, status='pending', message='Full vault reindex queued')
|