File size: 1,272 Bytes
1fee1c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# COST: ZERO OpenAI tokens.
# Reads ChromaDB metadata only β€” no embedding or similarity search performed.

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))

from dotenv import load_dotenv

load_dotenv()

from chroma import get_collection  # noqa: E402


def list_indexed_documents() -> list[str]:
    """Return the names of all documents that have been indexed in the vector store.

    Queries the ChromaDB collection metadata to collect every unique
    'source_file' value without loading any embeddings or performing any
    similarity search.  No LLM call is made.

    This is useful for letting the agent know which documents are available
    before deciding whether to retrieve chunks or ask the user to upload more
    files.

    Returns:
        A sorted list of unique source file names (e.g. ['manual.pdf', 'notes.docx']).
        Returns an empty list if no documents have been indexed yet.
    """
    collection = get_collection()
    if collection.count() == 0:
        return []

    result = collection.get(include=["metadatas"])
    seen: set[str] = set()
    for meta in result["metadatas"]:
        source = meta.get("source_file")
        if source:
            seen.add(source)

    return sorted(seen)