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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4f28f8
 
 
 
 
 
 
 
f0e94ef
 
 
 
 
 
 
 
 
 
4ef165a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0da0699
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4f28f8
 
 
 
 
 
 
 
 
 
0da0699
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
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
import logging
import uuid
from typing import Optional

from qdrant_client import QdrantClient
from qdrant_client.models import (
    Distance,
    FieldCondition,
    Filter,
    MatchAny,
    MatchValue,
    NamedSparseVector,
    NamedVector,
    PayloadSchemaType,
    PointStruct,
    SparseIndexParams,
    SparseVector,
    SparseVectorParams,
    VectorParams,
)

from app.models.pipeline import Chunk, ChunkMetadata
from app.core.exceptions import RetrievalError

logger = logging.getLogger(__name__)

# Named vector keys used in the Qdrant collection.
_DENSE_VEC = "dense"
_SPARSE_VEC = "sparse"


class VectorStore:
    def __init__(self, client: QdrantClient, collection: str):
        self.client = client
        self.collection = collection

    def ensure_collection(
        self,
        allow_recreate: bool = False,
        force_recreate: bool = False,
    ) -> None:
        """
        Creates or (re)creates the collection with named dense + sparse vectors.

        force_recreate=True  β€” always delete and recreate (github ingestion mode).
          Use this at the start of every full re-index run so the schema is clean.
        allow_recreate=True  β€” recreate only when the existing collection uses the
          old unnamed-vector format.  Safe migration path for incremental runs.
        allow_recreate=False β€” never touch an existing collection (API startup).
        """
        collections = self.client.get_collections().collections
        exists = any(c.name == self.collection for c in collections)

        if exists and force_recreate:
            logger.info("force_recreate=True β€” deleting collection %r for clean rebuild.", self.collection)
            self.client.delete_collection(self.collection)
            exists = False
        elif exists and allow_recreate:
            try:
                info = self.client.get_collection(self.collection)
                is_old_format = not isinstance(info.config.params.vectors, dict)
                has_no_sparse = not info.config.params.sparse_vectors
                if is_old_format or has_no_sparse:
                    logger.info(
                        "Collection %r uses old vector format; recreating for hybrid search.",
                        self.collection,
                    )
                    self.client.delete_collection(self.collection)
                    exists = False
            except Exception as exc:
                logger.warning("Could not inspect collection format (%s); skipping migration.", exc)

        if not exists:
            self.client.create_collection(
                collection_name=self.collection,
                vectors_config={
                    _DENSE_VEC: VectorParams(size=384, distance=Distance.COSINE),
                },
                sparse_vectors_config={
                    # on_disk=False keeps sparse index in RAM for sub-ms lookup.
                    _SPARSE_VEC: SparseVectorParams(
                        index=SparseIndexParams(on_disk=False)
                    ),
                },
            )
            logger.info("Created collection %r with dense + sparse vectors.", self.collection)

        # Payload indices β€” all idempotent; safe to run on every startup.
        for field, schema in [
            ("metadata.doc_id", PayloadSchemaType.KEYWORD),
            # chunk_type index enables fast filter-by-type in dense/sparse searches.
            ("metadata.chunk_type", PayloadSchemaType.KEYWORD),
            # keywords index enables fast MatchAny payload filter for named-entity lookup.
            ("metadata.keywords", PayloadSchemaType.KEYWORD),
        ]:
            self.client.create_payload_index(
                collection_name=self.collection,
                field_name=field,
                field_schema=schema,
            )

    def upsert_chunks(
        self,
        chunks: list[Chunk],
        dense_embeddings: list[list[float]],
        sparse_embeddings: Optional[list[tuple[list[int], list[float]]]] = None,
    ) -> list[str]:
        """
        Builds PointStruct list with named dense (and optionally sparse) vectors.
        Returns the list of Qdrant point UUIDs in the same order as `chunks`.

        Callers must capture the returned IDs when they need to reference these
        points later (e.g. raptor_summary.child_leaf_ids, question_proxy.parent_leaf_id).

        sparse_embeddings: list of (indices, values) tuples from SparseEncoder.
        If None or empty for a chunk, only the dense vector is stored.
        """
        if len(chunks) != len(dense_embeddings):
            raise ValueError("Number of chunks must match number of dense embeddings")
        if not chunks:
            return []

        # Pre-generate all UUIDs so callers can reference them before Qdrant confirms.
        point_ids = [str(uuid.uuid4()) for _ in chunks]
        points = []
        for i, (chunk, dense_vec) in enumerate(zip(chunks, dense_embeddings)):
            vector: dict = {_DENSE_VEC: dense_vec}

            if sparse_embeddings is not None:
                indices, values = sparse_embeddings[i]
                if indices:  # Skip empty sparse vectors gracefully
                    vector[_SPARSE_VEC] = SparseVector(
                        indices=indices, values=values
                    )

            points.append(
                PointStruct(
                    id=point_ids[i],
                    vector=vector,
                    payload=chunk,
                )
            )

        batch_size = 100
        for i in range(0, len(points), batch_size):
            self.client.upsert(
                collection_name=self.collection,
                points=points[i : i + batch_size],
            )
        return point_ids

    def fetch_by_point_ids(self, ids: list[str]) -> list["Chunk"]:
        """
        Fetch specific points by their Qdrant UUID β€” used by the retrieve node
        to resolve raptor_summary.child_leaf_ids and question_proxy.parent_leaf_id
        into actual Chunk objects after a dense search returns navigation nodes.

        Returns only points that actually exist; silently skips missing IDs.
        """
        if not ids:
            return []
        try:
            records = self.client.retrieve(
                collection_name=self.collection,
                ids=ids,
                with_payload=True,
                with_vectors=False,
            )
            return [Chunk(**rec.payload) for rec in records if rec.payload]
        except Exception as exc:
            logger.warning("fetch_by_point_ids failed: %s", exc)
            return []

    def keyword_filter_search(self, terms: list[str], top_k: int = 20) -> list["Chunk"]:
        """
        Payload filter search on metadata.keywords using MatchAny.
        Only returns leaf chunks β€” navigation nodes have no keywords field.

        Used by the retrieve node when expand_query produced canonical name forms
        so that BM25-invisible proper-noun variants still contribute to retrieval.
        """
        if not terms:
            return []
        try:
            records, _ = self.client.scroll(
                collection_name=self.collection,
                scroll_filter=Filter(
                    must=[
                        FieldCondition(
                            key="metadata.chunk_type",
                            match=MatchValue(value="leaf"),
                        ),
                        FieldCondition(
                            key="metadata.keywords",
                            match=MatchAny(any=[t.lower() for t in terms]),
                        ),
                    ]
                ),
                limit=top_k,
                with_payload=True,
                with_vectors=False,
            )
            return [Chunk(**rec.payload) for rec in records if rec.payload]
        except Exception as exc:
            logger.warning("keyword_filter_search failed (%s); skipping keyword results.", exc)
            return []

    def delete_by_doc_id(self, doc_id: str) -> None:
        """Filters on metadata.doc_id and deletes all matching points."""
        try:
            self.client.delete(
                collection_name=self.collection,
                points_selector=Filter(
                    must=[
                        FieldCondition(
                            key="metadata.doc_id",
                            match=MatchValue(value=doc_id),
                        )
                    ]
                ),
            )
        except Exception:
            pass  # Safe to ignore β€” collection or index may not exist yet

    def search(
        self,
        query_vector: list[float],
        top_k: int = 20,
        filters: Optional[dict] = None,
    ) -> list[Chunk]:
        """Dense vector search using the named 'dense' vector."""
        try:
            qdrant_filter = None
            if filters:
                must_conditions = [
                    FieldCondition(key=f"metadata.{k}", match=MatchValue(value=v))
                    for k, v in filters.items()
                ]
                qdrant_filter = Filter(must=must_conditions)

            results = self.client.search(
                collection_name=self.collection,
                query_vector=NamedVector(name=_DENSE_VEC, vector=query_vector),
                limit=top_k,
                query_filter=qdrant_filter,
                with_payload=True,
            )

            return [Chunk(**hit.payload) for hit in results if hit.payload]

        except Exception as exc:
            raise RetrievalError(
                f"Dense vector search failed: {exc}", context={"error": str(exc)}
            ) from exc

    def search_sparse(
        self,
        indices: list[int],
        values: list[float],
        top_k: int = 20,
    ) -> list["Chunk"]:
        """
        BM25 sparse vector search on the named 'sparse' vector.
        Filtered to chunk_type=="leaf" β€” navigation nodes (raptor_summary,
        question_proxy) have no sparse vectors anyway, but the explicit filter
        is a hard guarantee so they never surface via sparse retrieval.
        """
        if not indices:
            return []
        try:
            results = self.client.search(
                collection_name=self.collection,
                query_vector=NamedSparseVector(
                    name=_SPARSE_VEC,
                    vector=SparseVector(indices=indices, values=values),
                ),
                limit=top_k,
                query_filter=Filter(
                    must=[
                        FieldCondition(
                            key="metadata.chunk_type",
                            match=MatchValue(value="leaf"),
                        )
                    ]
                ),
                with_payload=True,
            )
            return [Chunk(**hit.payload) for hit in results if hit.payload]

        except Exception as exc:
            # Sparse index may not exist on old collections β€” log and continue.
            logger.warning("Sparse search failed (%s); skipping sparse results.", exc)
            return []

    def fetch_by_doc_id(self, doc_id: str, limit: int = 6) -> list[Chunk]:
        """
        Fetch up to `limit` chunks that share the same doc_id, ordered by their
        natural scroll order (insertion order). Used for document-graph sibling
        expansion: once a chunk from a document is retrieved by vector similarity,
        neighbouring chunks from the same document are pulled in to give the LLM
        richer context without requiring additional embedding calls.

        Uses Qdrant scroll (filter-only, no vector) so the result set is unranked β€”
        caller is responsible for reranking if order matters.
        """
        try:
            records, _ = self.client.scroll(
                collection_name=self.collection,
                scroll_filter=Filter(
                    must=[
                        FieldCondition(
                            key="metadata.doc_id",
                            match=MatchValue(value=doc_id),
                        ),
                        # Only sibling leaf chunks β€” navigation nodes from the
                        # same virtual doc_id (e.g. raptor_cluster_*) must not
                        # appear as citable siblings.
                        FieldCondition(
                            key="metadata.chunk_type",
                            match=MatchValue(value="leaf"),
                        ),
                    ]
                ),
                limit=limit,
                with_payload=True,
                with_vectors=False,
            )
            return [Chunk(**rec.payload) for rec in records if rec.payload]
        except Exception as exc:
            logger.warning("fetch_by_doc_id failed for %r: %s", doc_id, exc)
            return []

    def search_by_raptor_level(
        self,
        query_vector: list[float],
        level: int,
        top_k: int = 5,
    ) -> list[Chunk]:
        """
        Dense vector search restricted to chunks at a specific RAPTOR hierarchy level.

        level=0 β†’ leaf chunks (normal passage-level chunks).
        level=1 β†’ cluster summary nodes generated by RaptorBuilder.
        level=2 β†’ reserved for document-level summaries.

        Filter is applied via Qdrant payload filter on metadata.raptor_level.
        Old chunks that pre-date RAPTOR indexing lack the field and are excluded,
        which is the correct behaviour (they are effectively level-0 leaves already
        returned by the main dense search in retrieve.py).
        """
        try:
            results = self.client.search(
                collection_name=self.collection,
                query_vector=NamedVector(name=_DENSE_VEC, vector=query_vector),
                limit=top_k,
                query_filter=Filter(
                    must=[
                        FieldCondition(
                            key="metadata.raptor_level",
                            match=MatchValue(value=level),
                        )
                    ]
                ),
                with_payload=True,
            )
            return [Chunk(**hit.payload) for hit in results if hit.payload]
        except Exception as exc:
            logger.warning(
                "search_by_raptor_level(level=%d) failed: %s β€” skipping RAPTOR results.", level, exc
            )
            return []

    def scroll_by_source_type(
        self,
        source_types: list[str],
        limit: int = 500,
    ) -> list[Chunk]:
        """
        Retrieve all chunks matching any of the given source_types via payload
        filter β€” no vector search involved.

        Used by the enumeration_query node (Fix 1) to answer "list all projects /
        blogs / skills" queries with zero embedding or reranker calls.  The result
        is deduplicated and sorted by the caller.

        source_types: list of metadata.source_type values to include.
          e.g. ["project"]  or  ["blog"]  or  ["cv", "project", "blog"]
        limit: upper bound on total points fetched (safety cap; default 500 covers
          any realistic personal portfolio without unbounded scrolling).
        """
        if not source_types:
            return []
        try:
            # OR filter across all requested source types.
            should_conditions = [
                FieldCondition(
                    key="metadata.source_type",
                    match=MatchValue(value=st),
                )
                for st in source_types
            ]
            # Enumeration must never surface navigation nodes as list items.
            qdrant_filter = Filter(
                must=[
                    FieldCondition(
                        key="metadata.chunk_type",
                        match=MatchValue(value="leaf"),
                    )
                ],
                should=should_conditions,
            )

            records, _ = self.client.scroll(
                collection_name=self.collection,
                scroll_filter=qdrant_filter,
                limit=limit,
                with_payload=True,
                with_vectors=False,
            )
            return [Chunk(**rec.payload) for rec in records if rec.payload]
        except Exception as exc:
            logger.warning("scroll_by_source_type(%r) failed: %s", source_types, exc)
            return []