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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
"""
Document Intelligence Bridge for RAG

Bridges the document_intelligence subsystem with the RAG indexer/retriever.
Converts ParseResult to a format compatible with DocumentIndexer.
"""

from typing import List, Optional, Dict, Any
from pathlib import Path
from pydantic import BaseModel
from loguru import logger

from .store import VectorStore, get_vector_store
from .embeddings import EmbeddingAdapter, get_embedding_adapter
from .indexer import IndexingResult, IndexerConfig

# Try to import document_intelligence types
try:
    from ..document_intelligence.chunks import (
        ParseResult,
        DocumentChunk,
        BoundingBox,
        EvidenceRef,
        ChunkType,
    )
    DOCINT_AVAILABLE = True
except ImportError:
    DOCINT_AVAILABLE = False
    logger.warning("document_intelligence module not available")


class DocIntIndexer:
    """
    Indexes ParseResult from document_intelligence into the vector store.

    This bridges the new document_intelligence subsystem with the existing
    RAG infrastructure.
    """

    def __init__(
        self,
        config: Optional[IndexerConfig] = None,
        vector_store: Optional[VectorStore] = None,
        embedding_adapter: Optional[EmbeddingAdapter] = None,
    ):
        self.config = config or IndexerConfig()
        self._store = vector_store
        self._embedder = embedding_adapter

    @property
    def store(self) -> VectorStore:
        if self._store is None:
            self._store = get_vector_store()
        return self._store

    @property
    def embedder(self) -> EmbeddingAdapter:
        if self._embedder is None:
            self._embedder = get_embedding_adapter()
        return self._embedder

    def index_parse_result(
        self,
        parse_result: "ParseResult",
        source_path: Optional[str] = None,
    ) -> IndexingResult:
        """
        Index a ParseResult from document_intelligence.

        Args:
            parse_result: ParseResult from DocumentParser
            source_path: Optional override for source path

        Returns:
            IndexingResult with indexing stats
        """
        if not DOCINT_AVAILABLE:
            return IndexingResult(
                document_id="unknown",
                source_path="unknown",
                num_chunks_indexed=0,
                num_chunks_skipped=0,
                success=False,
                error="document_intelligence module not available",
            )

        document_id = parse_result.doc_id
        source = source_path or parse_result.filename

        try:
            chunks_to_index = []
            skipped = 0

            for chunk in parse_result.chunks:
                # Skip empty or short chunks
                if self.config.skip_empty_chunks:
                    if not chunk.text or len(chunk.text.strip()) < self.config.min_chunk_length:
                        skipped += 1
                        continue

                chunk_data = {
                    "chunk_id": chunk.chunk_id,
                    "document_id": document_id,
                    "source_path": source,
                    "text": chunk.text,
                    "sequence_index": chunk.sequence_index,
                    "confidence": chunk.confidence,
                }

                if self.config.include_page:
                    chunk_data["page"] = chunk.page

                if self.config.include_chunk_type:
                    chunk_data["chunk_type"] = chunk.chunk_type.value

                if self.config.include_bbox and chunk.bbox:
                    chunk_data["bbox"] = {
                        "x_min": chunk.bbox.x_min,
                        "y_min": chunk.bbox.y_min,
                        "x_max": chunk.bbox.x_max,
                        "y_max": chunk.bbox.y_max,
                    }

                chunks_to_index.append(chunk_data)

            if not chunks_to_index:
                return IndexingResult(
                    document_id=document_id,
                    source_path=source,
                    num_chunks_indexed=0,
                    num_chunks_skipped=skipped,
                    success=True,
                )

            # Generate embeddings in batches
            logger.info(f"Generating embeddings for {len(chunks_to_index)} chunks")
            texts = [c["text"] for c in chunks_to_index]

            embeddings = []
            batch_size = self.config.batch_size
            for i in range(0, len(texts), batch_size):
                batch = texts[i:i + batch_size]
                batch_embeddings = self.embedder.embed_batch(batch)
                embeddings.extend(batch_embeddings)

            # Store in vector database
            logger.info(f"Storing {len(chunks_to_index)} chunks in vector store")
            self.store.add_chunks(chunks_to_index, embeddings)

            logger.info(
                f"Indexed document {document_id}: "
                f"{len(chunks_to_index)} chunks, {skipped} skipped"
            )

            return IndexingResult(
                document_id=document_id,
                source_path=source,
                num_chunks_indexed=len(chunks_to_index),
                num_chunks_skipped=skipped,
                success=True,
            )

        except Exception as e:
            logger.error(f"Failed to index parse result: {e}")
            return IndexingResult(
                document_id=document_id,
                source_path=source,
                num_chunks_indexed=0,
                num_chunks_skipped=0,
                success=False,
                error=str(e),
            )

    def index_document(
        self,
        path: str,
        max_pages: Optional[int] = None,
    ) -> IndexingResult:
        """
        Parse and index a document in one step.

        Args:
            path: Path to document file
            max_pages: Optional limit on pages to process

        Returns:
            IndexingResult
        """
        if not DOCINT_AVAILABLE:
            return IndexingResult(
                document_id=str(path),
                source_path=str(path),
                num_chunks_indexed=0,
                num_chunks_skipped=0,
                success=False,
                error="document_intelligence module not available",
            )

        try:
            from ..document_intelligence import DocumentParser, ParserConfig

            config = ParserConfig(max_pages=max_pages)
            parser = DocumentParser(config=config)

            logger.info(f"Parsing document: {path}")
            parse_result = parser.parse(path)

            return self.index_parse_result(parse_result, source_path=str(path))

        except Exception as e:
            logger.error(f"Failed to parse and index document: {e}")
            return IndexingResult(
                document_id=str(path),
                source_path=str(path),
                num_chunks_indexed=0,
                num_chunks_skipped=0,
                success=False,
                error=str(e),
            )

    def delete_document(self, document_id: str) -> int:
        """Remove a document from the index."""
        return self.store.delete_document(document_id)

    def get_stats(self) -> Dict[str, Any]:
        """Get indexing statistics."""
        total_chunks = self.store.count()

        return {
            "total_chunks": total_chunks,
            "embedding_model": self.embedder.model_name,
            "embedding_dimension": self.embedder.embedding_dimension,
        }


class DocIntRetriever:
    """
    Retriever with document_intelligence EvidenceRef support.

    Wraps DocumentRetriever with conversions to document_intelligence types.
    """

    def __init__(
        self,
        vector_store: Optional[VectorStore] = None,
        embedding_adapter: Optional[EmbeddingAdapter] = None,
        similarity_threshold: float = 0.5,
    ):
        self._store = vector_store
        self._embedder = embedding_adapter
        self.similarity_threshold = similarity_threshold

    @property
    def store(self) -> VectorStore:
        if self._store is None:
            self._store = get_vector_store()
        return self._store

    @property
    def embedder(self) -> EmbeddingAdapter:
        if self._embedder is None:
            self._embedder = get_embedding_adapter()
        return self._embedder

    def retrieve(
        self,
        query: str,
        top_k: int = 5,
        document_id: Optional[str] = None,
        chunk_types: Optional[List[str]] = None,
        page_range: Optional[tuple] = None,
    ) -> List[Dict[str, Any]]:
        """
        Retrieve relevant chunks.

        Args:
            query: Search query
            top_k: Number of results
            document_id: Filter by document
            chunk_types: Filter by chunk type(s)
            page_range: Filter by page range (start, end)

        Returns:
            List of chunk dicts with metadata
        """
        # Build filters
        filters = {}

        if document_id:
            filters["document_id"] = document_id

        if chunk_types:
            filters["chunk_type"] = chunk_types

        if page_range:
            filters["page"] = {"min": page_range[0], "max": page_range[1]}

        # Embed query
        query_embedding = self.embedder.embed_text(query)

        # Search
        results = self.store.search(
            query_embedding=query_embedding,
            top_k=top_k,
            filters=filters if filters else None,
        )

        # Convert to dicts
        chunks = []
        for result in results:
            if result.similarity < self.similarity_threshold:
                continue

            chunk = {
                "chunk_id": result.chunk_id,
                "document_id": result.document_id,
                "text": result.text,
                "similarity": result.similarity,
                "page": result.page,
                "chunk_type": result.chunk_type,
                "bbox": result.bbox,
                "source_path": result.metadata.get("source_path"),
                "confidence": result.metadata.get("confidence"),
            }
            chunks.append(chunk)

        return chunks

    def retrieve_with_evidence(
        self,
        query: str,
        top_k: int = 5,
        document_id: Optional[str] = None,
        chunk_types: Optional[List[str]] = None,
        page_range: Optional[tuple] = None,
    ) -> tuple:
        """
        Retrieve chunks with EvidenceRef objects.

        Returns:
            Tuple of (chunks, evidence_refs)
        """
        chunks = self.retrieve(
            query, top_k, document_id, chunk_types, page_range
        )

        evidence_refs = []

        if DOCINT_AVAILABLE:
            for chunk in chunks:
                bbox = None
                if chunk.get("bbox"):
                    bbox_data = chunk["bbox"]
                    bbox = BoundingBox(
                        x_min=bbox_data.get("x_min", 0),
                        y_min=bbox_data.get("y_min", 0),
                        x_max=bbox_data.get("x_max", 1),
                        y_max=bbox_data.get("y_max", 1),
                        normalized=True,
                    )
                else:
                    bbox = BoundingBox(x_min=0, y_min=0, x_max=1, y_max=1)

                evidence = EvidenceRef(
                    chunk_id=chunk["chunk_id"],
                    doc_id=chunk["document_id"],
                    page=chunk.get("page", 1),
                    bbox=bbox,
                    source_type=chunk.get("chunk_type", "text"),
                    snippet=chunk["text"][:200],
                    confidence=chunk.get("confidence", chunk["similarity"]),
                )
                evidence_refs.append(evidence)

        return chunks, evidence_refs

    def build_context(
        self,
        chunks: List[Dict[str, Any]],
        max_length: int = 8000,
    ) -> str:
        """Build context string from retrieved chunks."""
        if not chunks:
            return ""

        parts = []
        for i, chunk in enumerate(chunks, 1):
            header = f"[{i}]"
            if chunk.get("page"):
                header += f" Page {chunk['page']}"
            if chunk.get("chunk_type"):
                header += f" ({chunk['chunk_type']})"
            header += f" [sim={chunk['similarity']:.2f}]"

            parts.append(header)
            parts.append(chunk["text"])
            parts.append("")

        context = "\n".join(parts)

        if len(context) > max_length:
            context = context[:max_length] + "\n...[truncated]"

        return context


# Singleton instances
_docint_indexer: Optional[DocIntIndexer] = None
_docint_retriever: Optional[DocIntRetriever] = None


def get_docint_indexer(
    config: Optional[IndexerConfig] = None,
    vector_store: Optional[VectorStore] = None,
    embedding_adapter: Optional[EmbeddingAdapter] = None,
) -> DocIntIndexer:
    """Get or create singleton DocIntIndexer."""
    global _docint_indexer

    if _docint_indexer is None:
        _docint_indexer = DocIntIndexer(
            config=config,
            vector_store=vector_store,
            embedding_adapter=embedding_adapter,
        )

    return _docint_indexer


def get_docint_retriever(
    vector_store: Optional[VectorStore] = None,
    embedding_adapter: Optional[EmbeddingAdapter] = None,
    similarity_threshold: float = 0.5,
) -> DocIntRetriever:
    """Get or create singleton DocIntRetriever."""
    global _docint_retriever

    if _docint_retriever is None:
        _docint_retriever = DocIntRetriever(
            vector_store=vector_store,
            embedding_adapter=embedding_adapter,
            similarity_threshold=similarity_threshold,
        )

    return _docint_retriever


def reset_docint_components():
    """Reset singleton instances."""
    global _docint_indexer, _docint_retriever
    _docint_indexer = None
    _docint_retriever = None