Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

4.9 KB
Raw Blame
from datetime import datetime, UTC

from loguru import logger

from ...database.models import ResearchResource
from ...database.session_context import get_user_db_session


def get_resources_for_research(research_id, username):
    """
    Retrieve resources associated with a specific research project.

    Args:
        research_id (str): The UUID of the research.
        username (str): The authenticated caller — required so the lookup
            opens that user's encrypted database explicitly. Without it,
            ``get_user_db_session()`` falls back to a contextvar read
            (or a cross-session password) which can silently return data
            from the wrong user's DB if context isn't set.

    Returns:
        list: List of resource objects for the research.
    """
    try:
        with get_user_db_session(username) as db_session:
            # Query to get resources for the research
            resources_list = (
                db_session.query(ResearchResource)
                .filter_by(research_id=research_id)
                .order_by(ResearchResource.id.asc())
                .all()
            )

            resources = []
            for resource in resources_list:
                resources.append(
                    {
                        "id": resource.id,
                        "research_id": resource.research_id,
                        "title": resource.title,
                        "url": resource.url,
                        "content_preview": resource.content_preview,
                        "source_type": resource.source_type,
                        "metadata": resource.resource_metadata or {},
                    }
                )

            return resources

    except Exception:
        logger.exception("Error retrieving resources for research")
        raise


def add_resource(
    research_id,
    title,
    url,
    content_preview=None,
    source_type="web",
    metadata=None,
    username=None,
):
    """
    Add a new resource to the research_resources table

    Args:
        research_id (str): The UUID of the research
        title (str): The title of the resource
        url (str): The URL of the resource
        content_preview (str, optional): A preview of the content
        source_type (str, optional): The type of source
        metadata (dict, optional): Additional metadata
        username (str): The authenticated caller — REQUIRED. Under FastAPI
            get_user_db_session needs it explicitly; without it the open
            raises DatabaseSessionError and this always 500s (the sibling
            get_resources_for_research was fixed the same way).

    Returns:
        ResearchResource: The created resource object
    """
    try:
        with get_user_db_session(username) as db_session:
            # NOTE: ResearchResource has no `accessed_at` column, and
            # `created_at` (String, nullable=False) has no server default —
            # so passing accessed_at raised TypeError and omitting created_at
            # would violate NOT NULL. Match the working insert in
            # research_sources_service.ResearchResource(...): set created_at
            # to an ISO string, drop accessed_at. (This function 500'd on
            # every call on both this branch and main until now.)
            resource = ResearchResource(
                research_id=research_id,
                title=title,
                url=url,
                content_preview=content_preview,
                source_type=source_type,
                resource_metadata=metadata,
                created_at=datetime.now(UTC).isoformat(),
            )

            db_session.add(resource)
            db_session.commit()

            return resource

    except Exception:
        logger.exception("Error adding resource")
        raise


def delete_resource(resource_id, username=None):
    """
    Delete a resource from the database

    Args:
        resource_id (int): The ID of the resource to delete
        username (str): The authenticated caller — REQUIRED (see add_resource;
            without it get_user_db_session raises and this always 500s).

    Returns:
        bool: True if deletion was successful

    Raises:
        ValueError: If resource not found
    """
    try:
        with get_user_db_session(username) as db_session:
            # Find the resource
            resource = (
                db_session.query(ResearchResource)
                .filter_by(id=resource_id)
                .first()
            )

            if not resource:
                raise ValueError(f"Resource with ID {resource_id} not found")  # noqa: TRY301 — re-raised by except ValueError

            db_session.delete(resource)
            db_session.commit()

            logger.info(f"Deleted resource {resource_id}")
            return True

    except ValueError:
        raise
    except Exception:
        logger.exception("Error deleting resource")
        raise