gnosisllm-knowledge 0.3.0__py3-none-any.whl → 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. gnosisllm_knowledge/api/knowledge.py +225 -35
  2. gnosisllm_knowledge/backends/memory/indexer.py +27 -2
  3. gnosisllm_knowledge/backends/memory/searcher.py +111 -10
  4. gnosisllm_knowledge/backends/opensearch/agentic.py +14 -9
  5. gnosisllm_knowledge/backends/opensearch/indexer.py +48 -3
  6. gnosisllm_knowledge/backends/opensearch/mappings.py +12 -4
  7. gnosisllm_knowledge/backends/opensearch/queries.py +33 -33
  8. gnosisllm_knowledge/backends/opensearch/searcher.py +9 -6
  9. gnosisllm_knowledge/cli/app.py +58 -19
  10. gnosisllm_knowledge/cli/commands/agentic.py +15 -9
  11. gnosisllm_knowledge/cli/commands/load.py +169 -19
  12. gnosisllm_knowledge/cli/commands/memory.py +10 -0
  13. gnosisllm_knowledge/cli/commands/search.py +9 -10
  14. gnosisllm_knowledge/cli/commands/setup.py +25 -1
  15. gnosisllm_knowledge/cli/utils/config.py +4 -4
  16. gnosisllm_knowledge/core/domain/__init__.py +13 -0
  17. gnosisllm_knowledge/core/domain/discovery.py +166 -0
  18. gnosisllm_knowledge/core/domain/document.py +14 -19
  19. gnosisllm_knowledge/core/domain/search.py +10 -25
  20. gnosisllm_knowledge/core/domain/source.py +11 -12
  21. gnosisllm_knowledge/core/events/__init__.py +8 -0
  22. gnosisllm_knowledge/core/events/types.py +122 -5
  23. gnosisllm_knowledge/core/exceptions.py +93 -0
  24. gnosisllm_knowledge/core/interfaces/agentic.py +11 -3
  25. gnosisllm_knowledge/core/interfaces/indexer.py +10 -1
  26. gnosisllm_knowledge/core/interfaces/searcher.py +10 -1
  27. gnosisllm_knowledge/core/interfaces/streaming.py +10 -4
  28. gnosisllm_knowledge/fetchers/__init__.py +8 -0
  29. gnosisllm_knowledge/fetchers/config.py +27 -0
  30. gnosisllm_knowledge/fetchers/neoreader.py +31 -3
  31. gnosisllm_knowledge/fetchers/neoreader_discovery.py +505 -0
  32. gnosisllm_knowledge/loaders/__init__.py +5 -1
  33. gnosisllm_knowledge/loaders/discovery.py +338 -0
  34. gnosisllm_knowledge/loaders/discovery_streaming.py +343 -0
  35. gnosisllm_knowledge/loaders/factory.py +46 -0
  36. gnosisllm_knowledge/services/indexing.py +35 -20
  37. gnosisllm_knowledge/services/search.py +37 -20
  38. gnosisllm_knowledge/services/streaming_pipeline.py +39 -7
  39. {gnosisllm_knowledge-0.3.0.dist-info → gnosisllm_knowledge-0.4.0.dist-info}/METADATA +30 -10
  40. gnosisllm_knowledge-0.4.0.dist-info/RECORD +81 -0
  41. gnosisllm_knowledge-0.3.0.dist-info/RECORD +0 -77
  42. {gnosisllm_knowledge-0.3.0.dist-info → gnosisllm_knowledge-0.4.0.dist-info}/WHEEL +0 -0
  43. {gnosisllm_knowledge-0.3.0.dist-info → gnosisllm_knowledge-0.4.0.dist-info}/entry_points.txt +0 -0
@@ -1,4 +1,39 @@
1
- """High-level Knowledge API facade."""
1
+ """High-level Knowledge API facade.
2
+
3
+ This module provides the main entry point for the gnosisllm-knowledge library.
4
+ The Knowledge class is a high-level facade that abstracts the complexity of
5
+ loading, indexing, and searching knowledge documents.
6
+
7
+ Note:
8
+ This library is tenant-agnostic. Multi-tenancy should be handled at the
9
+ API layer by using separate indices per account (e.g.,
10
+ `knowledge-{account_id}`) rather than filtering by account_id.
11
+
12
+ Example:
13
+ ```python
14
+ # Create Knowledge instance for a specific tenant
15
+ knowledge = Knowledge.from_opensearch(
16
+ host="localhost",
17
+ port=9200,
18
+ )
19
+
20
+ # Use a tenant-specific index
21
+ tenant_index = f"knowledge-{account_id}"
22
+
23
+ # Load content
24
+ await knowledge.load(
25
+ "https://docs.example.com/sitemap.xml",
26
+ index_name=tenant_index,
27
+ collection_id="docs",
28
+ )
29
+
30
+ # Search (tenant isolation via index name)
31
+ results = await knowledge.search(
32
+ "how to configure",
33
+ index_name=tenant_index,
34
+ )
35
+ ```
36
+ """
2
37
 
3
38
  from __future__ import annotations
4
39
 
@@ -130,6 +165,10 @@ class Knowledge:
130
165
  ) -> Knowledge:
131
166
  """Create Knowledge instance with OpenSearch backend.
132
167
 
168
+ This factory creates a Knowledge instance configured for OpenSearch.
169
+ The returned instance is tenant-agnostic - multi-tenancy should be
170
+ handled by using separate indices per account.
171
+
133
172
  Args:
134
173
  host: OpenSearch host.
135
174
  port: OpenSearch port.
@@ -147,6 +186,19 @@ class Knowledge:
147
186
  Note:
148
187
  Embeddings are generated automatically by OpenSearch ingest pipeline.
149
188
  Run 'gnosisllm-knowledge setup' to configure the ML model.
189
+
190
+ Example:
191
+ ```python
192
+ # Create a Knowledge instance
193
+ knowledge = Knowledge.from_opensearch(
194
+ host="localhost",
195
+ port=9200,
196
+ )
197
+
198
+ # Use tenant-specific index for isolation
199
+ tenant_index = f"gnosisllm-{account_id}-knowledge"
200
+ await knowledge.load(source, index_name=tenant_index)
201
+ ```
150
202
  """
151
203
  # Import OpenSearch client
152
204
  try:
@@ -216,15 +268,29 @@ class Knowledge:
216
268
  def from_env(cls) -> Knowledge:
217
269
  """Create Knowledge instance from environment variables.
218
270
 
271
+ This factory creates a Knowledge instance using configuration from
272
+ environment variables. The returned instance is tenant-agnostic -
273
+ multi-tenancy should be handled by using separate indices per account.
274
+
219
275
  Returns:
220
276
  Configured Knowledge instance.
277
+
278
+ Example:
279
+ ```python
280
+ # Create from environment
281
+ knowledge = Knowledge.from_env()
282
+
283
+ # Use tenant-specific index for isolation
284
+ tenant_index = f"gnosisllm-{account_id}-knowledge"
285
+ await knowledge.search("query", index_name=tenant_index)
286
+ ```
221
287
  """
222
288
  config = OpenSearchConfig.from_env()
223
289
  neoreader_config = NeoreaderConfig.from_env()
224
290
 
225
291
  return cls.from_opensearch(
226
292
  config=config,
227
- neoreader_url=neoreader_config.base_url if neoreader_config.base_url else None,
293
+ neoreader_url=neoreader_config.host if neoreader_config.host else None,
228
294
  )
229
295
 
230
296
  @property
@@ -318,7 +384,6 @@ class Knowledge:
318
384
  source: str,
319
385
  *,
320
386
  index_name: str | None = None,
321
- account_id: str | None = None,
322
387
  collection_id: str | None = None,
323
388
  source_id: str | None = None,
324
389
  source_type: str | None = None,
@@ -329,10 +394,13 @@ class Knowledge:
329
394
 
330
395
  Automatically detects source type (sitemap, website, etc.).
331
396
 
397
+ Note:
398
+ This method is tenant-agnostic. Multi-tenancy should be handled
399
+ by using separate indices per account.
400
+
332
401
  Args:
333
402
  source: Source URL or path.
334
- index_name: Target index (uses default if not provided).
335
- account_id: Account ID for multi-tenancy.
403
+ index_name: Target index (use tenant-specific name for isolation).
336
404
  collection_id: Collection ID.
337
405
  source_id: Source ID (auto-generated if not provided).
338
406
  source_type: Explicit source type (auto-detected if not provided).
@@ -366,7 +434,6 @@ class Knowledge:
366
434
  return await service.load_and_index(
367
435
  source=source,
368
436
  index_name=index,
369
- account_id=account_id,
370
437
  collection_id=collection_id,
371
438
  source_id=source_id,
372
439
  **options,
@@ -377,7 +444,6 @@ class Knowledge:
377
444
  source: str,
378
445
  *,
379
446
  index_name: str | None = None,
380
- account_id: str | None = None,
381
447
  collection_id: str | None = None,
382
448
  collection_name: str | None = None,
383
449
  source_id: str | None = None,
@@ -398,10 +464,13 @@ class Knowledge:
398
464
  - Document storage: O(index_batch_size)
399
465
  - In-flight fetches: O(fetch_concurrency * avg_page_size)
400
466
 
467
+ Note:
468
+ This method is tenant-agnostic. Multi-tenancy should be handled
469
+ by using separate indices per account.
470
+
401
471
  Args:
402
472
  source: Sitemap URL.
403
- index_name: Target index (uses default if not provided).
404
- account_id: Account ID for multi-tenancy.
473
+ index_name: Target index (use tenant-specific name for isolation).
405
474
  collection_id: Collection ID.
406
475
  collection_name: Collection name for display.
407
476
  source_id: Source ID (auto-generated if not provided).
@@ -419,6 +488,7 @@ class Knowledge:
419
488
  # Efficiently load 100k+ URL sitemap
420
489
  result = await knowledge.load_streaming(
421
490
  "https://large-site.com/sitemap.xml",
491
+ index_name="knowledge-account123", # Tenant-specific
422
492
  url_batch_size=100,
423
493
  fetch_concurrency=20,
424
494
  max_urls=50000,
@@ -454,7 +524,6 @@ class Knowledge:
454
524
  return await pipeline.execute(
455
525
  source=source,
456
526
  index_name=index,
457
- account_id=account_id,
458
527
  collection_id=collection_id,
459
528
  collection_name=collection_name,
460
529
  source_id=source_id,
@@ -471,7 +540,6 @@ class Knowledge:
471
540
  mode: SearchMode = SearchMode.HYBRID,
472
541
  limit: int = 10,
473
542
  offset: int = 0,
474
- account_id: str | None = None,
475
543
  collection_ids: list[str] | None = None,
476
544
  source_ids: list[str] | None = None,
477
545
  min_score: float | None = None,
@@ -479,13 +547,16 @@ class Knowledge:
479
547
  ) -> SearchResult:
480
548
  """Search for knowledge documents.
481
549
 
550
+ Note:
551
+ This method is tenant-agnostic. Multi-tenancy should be handled
552
+ by using separate indices per account.
553
+
482
554
  Args:
483
555
  query: Search query text.
484
- index_name: Index to search (uses default if not provided).
556
+ index_name: Index to search (use tenant-specific name for isolation).
485
557
  mode: Search mode (semantic, keyword, hybrid).
486
558
  limit: Maximum results.
487
559
  offset: Result offset for pagination.
488
- account_id: Account ID for multi-tenancy.
489
560
  collection_ids: Filter by collection IDs.
490
561
  source_ids: Filter by source IDs.
491
562
  min_score: Minimum score threshold.
@@ -500,7 +571,6 @@ class Knowledge:
500
571
  mode=mode,
501
572
  limit=limit,
502
573
  offset=offset,
503
- account_id=account_id,
504
574
  collection_ids=collection_ids,
505
575
  source_ids=source_ids,
506
576
  min_score=min_score,
@@ -578,19 +648,73 @@ class Knowledge:
578
648
 
579
649
  # === Management Methods ===
580
650
 
651
+ async def get_document(
652
+ self,
653
+ document_id: str,
654
+ *,
655
+ index_name: str | None = None,
656
+ ) -> dict[str, Any] | None:
657
+ """Get a single document by ID.
658
+
659
+ Note:
660
+ This method is tenant-agnostic. Multi-tenancy should be handled
661
+ by using separate indices per account.
662
+
663
+ Args:
664
+ document_id: Document ID to retrieve.
665
+ index_name: Index name (use tenant-specific name for isolation).
666
+ Uses default index if not provided.
667
+
668
+ Returns:
669
+ Document dict with all fields (excluding embeddings) or None if not found.
670
+ """
671
+ index = index_name or self._default_index
672
+ if not index:
673
+ raise ValueError("No index specified and no default index configured")
674
+
675
+ return await self._indexer.get(document_id, index)
676
+
677
+ async def delete_document(
678
+ self,
679
+ document_id: str,
680
+ *,
681
+ index_name: str | None = None,
682
+ ) -> bool:
683
+ """Delete a single document by ID.
684
+
685
+ Note:
686
+ This method is tenant-agnostic. Multi-tenancy should be handled
687
+ by using separate indices per account.
688
+
689
+ Args:
690
+ document_id: Document ID to delete.
691
+ index_name: Index name (use tenant-specific name for isolation).
692
+ Uses default index if not provided.
693
+
694
+ Returns:
695
+ True if deleted, False if not found.
696
+ """
697
+ index = index_name or self._default_index
698
+ if not index:
699
+ raise ValueError("No index specified and no default index configured")
700
+
701
+ return await self._indexer.delete(document_id, index)
702
+
581
703
  async def delete_source(
582
704
  self,
583
705
  source_id: str,
584
706
  *,
585
707
  index_name: str | None = None,
586
- account_id: str | None = None,
587
708
  ) -> int:
588
709
  """Delete all documents from a source.
589
710
 
711
+ Note:
712
+ This method is tenant-agnostic. Multi-tenancy should be handled
713
+ by using separate indices per account.
714
+
590
715
  Args:
591
716
  source_id: Source ID to delete.
592
- index_name: Index name.
593
- account_id: Account ID for multi-tenancy.
717
+ index_name: Index name (use tenant-specific name for isolation).
594
718
 
595
719
  Returns:
596
720
  Count of deleted documents.
@@ -599,21 +723,23 @@ class Knowledge:
599
723
  if not index:
600
724
  raise ValueError("No index specified")
601
725
 
602
- return await self.indexing.delete_source(source_id, index, account_id)
726
+ return await self.indexing.delete_source(source_id, index)
603
727
 
604
728
  async def delete_collection(
605
729
  self,
606
730
  collection_id: str,
607
731
  *,
608
732
  index_name: str | None = None,
609
- account_id: str | None = None,
610
733
  ) -> int:
611
734
  """Delete all documents from a collection.
612
735
 
736
+ Note:
737
+ This method is tenant-agnostic. Multi-tenancy should be handled
738
+ by using separate indices per account.
739
+
613
740
  Args:
614
741
  collection_id: Collection ID to delete.
615
- index_name: Index name.
616
- account_id: Account ID for multi-tenancy.
742
+ index_name: Index name (use tenant-specific name for isolation).
617
743
 
618
744
  Returns:
619
745
  Count of deleted documents.
@@ -622,54 +748,85 @@ class Knowledge:
622
748
  if not index:
623
749
  raise ValueError("No index specified")
624
750
 
625
- return await self.indexing.delete_collection(collection_id, index, account_id)
751
+ return await self.indexing.delete_collection(collection_id, index)
626
752
 
627
753
  async def count(
628
754
  self,
629
755
  *,
630
756
  index_name: str | None = None,
631
- account_id: str | None = None,
632
757
  collection_id: str | None = None,
758
+ source_id: str | None = None,
633
759
  ) -> int:
634
760
  """Count documents.
635
761
 
762
+ Note:
763
+ This method is tenant-agnostic. Multi-tenancy should be handled
764
+ by using separate indices per account.
765
+
636
766
  Args:
637
- index_name: Index to count.
638
- account_id: Filter by account.
767
+ index_name: Index to count (use tenant-specific name for isolation).
639
768
  collection_id: Filter by collection.
769
+ source_id: Filter by source (for source deletion confirmation).
640
770
 
641
771
  Returns:
642
772
  Document count.
643
773
  """
644
774
  return await self.search_service.count(
645
775
  index_name=index_name,
646
- account_id=account_id,
647
776
  collection_id=collection_id,
777
+ source_id=source_id,
648
778
  )
649
779
 
650
780
  # === Collection and Stats Methods ===
651
781
 
652
- async def get_collections(self) -> list[dict[str, Any]]:
782
+ async def get_collections(
783
+ self,
784
+ *,
785
+ index_name: str | None = None,
786
+ ) -> list[dict[str, Any]]:
653
787
  """Get all collections with document counts.
654
788
 
655
789
  Aggregates unique collection_ids from indexed documents.
656
790
 
791
+ Note:
792
+ This method is tenant-agnostic. Multi-tenancy should be handled
793
+ by using separate indices per account.
794
+
795
+ Args:
796
+ index_name: Index to query (use tenant-specific name for isolation).
797
+ Uses default index if not provided.
798
+
657
799
  Returns:
658
800
  List of collection dictionaries with id, name, and document_count.
659
801
  """
660
- return await self.search_service.get_collections()
802
+ index = index_name or self._default_index
803
+ return await self.search_service.get_collections(index_name=index)
661
804
 
662
- async def get_stats(self) -> dict[str, Any]:
805
+ async def get_stats(
806
+ self,
807
+ *,
808
+ index_name: str | None = None,
809
+ ) -> dict[str, Any]:
663
810
  """Get index statistics.
664
811
 
812
+ Note:
813
+ This method is tenant-agnostic. Multi-tenancy should be handled
814
+ by using separate indices per account.
815
+
816
+ Args:
817
+ index_name: Index to query (use tenant-specific name for isolation).
818
+ Uses default index if not provided.
819
+
665
820
  Returns:
666
821
  Dictionary with document_count, index_name, and other stats.
667
822
  """
668
- return await self.search_service.get_stats()
823
+ index = index_name or self._default_index
824
+ return await self.search_service.get_stats(index_name=index)
669
825
 
670
826
  async def list_documents(
671
827
  self,
672
828
  *,
829
+ index_name: str | None = None,
673
830
  source_id: str | None = None,
674
831
  collection_id: str | None = None,
675
832
  limit: int = 50,
@@ -677,7 +834,13 @@ class Knowledge:
677
834
  ) -> dict[str, Any]:
678
835
  """List documents with optional filters.
679
836
 
837
+ Note:
838
+ This method is tenant-agnostic. Multi-tenancy should be handled
839
+ by using separate indices per account.
840
+
680
841
  Args:
842
+ index_name: Index to query (use tenant-specific name for isolation).
843
+ Uses default index if not provided.
681
844
  source_id: Optional source ID filter.
682
845
  collection_id: Optional collection ID filter.
683
846
  limit: Maximum documents to return (max 100).
@@ -686,9 +849,9 @@ class Knowledge:
686
849
  Returns:
687
850
  Dictionary with documents, total, limit, offset.
688
851
  """
689
- index = self._default_index
852
+ index = index_name or self._default_index
690
853
  if not index:
691
- raise ValueError("No default index configured")
854
+ raise ValueError("No index specified and no default index configured")
692
855
 
693
856
  # Clamp limit to reasonable bounds
694
857
  limit = min(max(1, limit), 100)
@@ -823,6 +986,33 @@ class Knowledge:
823
986
  return await agentic_searcher.agentic_search(agentic_query, index, **options)
824
987
 
825
988
  async def close(self) -> None:
826
- """Close connections and clean up resources."""
827
- # Subclasses or future implementations can override this
828
- pass
989
+ """Close connections and clean up resources.
990
+
991
+ Closes the underlying AsyncOpenSearch client to prevent
992
+ unclosed aiohttp session warnings. Properly handles
993
+ CancelledError during event loop shutdown.
994
+ """
995
+ import asyncio
996
+
997
+ # Close the OpenSearch client via the searcher
998
+ # Note: indexer, searcher, and setup share the same client instance,
999
+ # so closing via searcher is sufficient
1000
+ if hasattr(self._searcher, '_client') and self._searcher._client is not None:
1001
+ client = self._searcher._client
1002
+ try:
1003
+ await client.close()
1004
+ logger.debug("Closed OpenSearch client connection")
1005
+ except asyncio.CancelledError:
1006
+ # Event loop is shutting down - this is expected during cleanup
1007
+ logger.debug("OpenSearch client close cancelled (event loop shutting down)")
1008
+ except Exception as e:
1009
+ logger.warning(f"Error closing OpenSearch client: {e}")
1010
+ finally:
1011
+ # Clear client reference on all components that share it
1012
+ # This prevents any accidental reuse after close
1013
+ if hasattr(self._searcher, '_client'):
1014
+ self._searcher._client = None
1015
+ if hasattr(self._indexer, '_client'):
1016
+ self._indexer._client = None
1017
+ if self._setup and hasattr(self._setup, '_client'):
1018
+ self._setup._client = None
@@ -1,4 +1,10 @@
1
- """In-memory document indexer for testing."""
1
+ """In-memory document indexer for testing.
2
+
3
+ Note:
4
+ This library is tenant-agnostic. Multi-tenancy is achieved through index
5
+ isolation (e.g., `knowledge-{account_id}`). The memory indexer does not
6
+ include tenant filtering logic - use separate index names per tenant.
7
+ """
2
8
 
3
9
  from __future__ import annotations
4
10
 
@@ -14,6 +20,9 @@ from gnosisllm_knowledge.core.domain.result import BatchResult, IndexResult
14
20
  class MemoryIndexer:
15
21
  """In-memory document indexer for testing.
16
22
 
23
+ This indexer is tenant-agnostic. Multi-tenancy is achieved through index
24
+ isolation by using tenant-specific index names.
25
+
17
26
  Stores documents in a dictionary for fast testing without
18
27
  requiring an external OpenSearch instance.
19
28
 
@@ -185,6 +194,22 @@ class MemoryIndexer:
185
194
  """
186
195
  return await self.index(document, index_name)
187
196
 
197
+ async def get(
198
+ self,
199
+ doc_id: str,
200
+ index_name: str,
201
+ ) -> dict[str, Any] | None:
202
+ """Get a document by ID (async interface).
203
+
204
+ Args:
205
+ doc_id: Document ID.
206
+ index_name: Index name.
207
+
208
+ Returns:
209
+ Document dictionary or None if not found.
210
+ """
211
+ return self._indices.get(index_name, {}).get(doc_id)
212
+
188
213
  async def delete(
189
214
  self,
190
215
  doc_id: str,
@@ -365,8 +390,8 @@ class MemoryIndexer:
365
390
  "url": document.url,
366
391
  "title": document.title,
367
392
  "source": document.source,
368
- "account_id": document.account_id,
369
393
  "collection_id": document.collection_id,
394
+ "collection_name": document.collection_name,
370
395
  "source_id": document.source_id,
371
396
  "chunk_index": document.chunk_index,
372
397
  "total_chunks": document.total_chunks,
@@ -1,10 +1,16 @@
1
- """In-memory document searcher for testing."""
1
+ """In-memory document searcher for testing.
2
+
3
+ Note: This module is tenant-agnostic. Multi-tenancy should be handled
4
+ at the API layer by using separate indices per account (e.g.,
5
+ gnosisllm-{account_id}-knowledge) rather than filtering by account_id.
6
+ """
2
7
 
3
8
  from __future__ import annotations
4
9
 
5
10
  import math
6
11
  import re
7
12
  import time
13
+ import warnings
8
14
  from typing import Any, Callable
9
15
 
10
16
  from gnosisllm_knowledge.backends.memory.indexer import MemoryIndexer
@@ -147,7 +153,7 @@ class MemorySearcher:
147
153
 
148
154
  for doc in filtered_docs:
149
155
  content = doc.get("content", "").lower()
150
- title = doc.get("title", "").lower()
156
+ title = (doc.get("title") or "").lower()
151
157
 
152
158
  # Simple TF scoring
153
159
  content_score = sum(
@@ -209,7 +215,7 @@ class MemorySearcher:
209
215
  for doc in filtered_docs:
210
216
  # Keyword score
211
217
  content = doc.get("content", "").lower()
212
- title = doc.get("title", "").lower()
218
+ title = (doc.get("title") or "").lower()
213
219
  keyword_score = sum(content.count(term) for term in query_terms)
214
220
  keyword_score += sum(title.count(term) for term in query_terms) * 2
215
221
 
@@ -348,6 +354,101 @@ class MemorySearcher:
348
354
  results.append(result)
349
355
  return results
350
356
 
357
+ async def list_documents(
358
+ self,
359
+ index_name: str,
360
+ *,
361
+ source_id: str | None = None,
362
+ collection_id: str | None = None,
363
+ limit: int = 50,
364
+ offset: int = 0,
365
+ ) -> dict[str, Any]:
366
+ """List documents with optional filters.
367
+
368
+ Args:
369
+ index_name: Index to query.
370
+ source_id: Optional source ID filter.
371
+ collection_id: Optional collection ID filter.
372
+ limit: Maximum documents to return.
373
+ offset: Number of documents to skip.
374
+
375
+ Returns:
376
+ Dictionary with documents, total, limit, offset.
377
+ """
378
+ documents = self._indexer.get_all(index_name)
379
+
380
+ # Apply filters
381
+ if source_id:
382
+ documents = [d for d in documents if d.get("source_id") == source_id]
383
+ if collection_id:
384
+ documents = [d for d in documents if d.get("collection_id") == collection_id]
385
+
386
+ total = len(documents)
387
+
388
+ # Apply pagination
389
+ paginated = documents[offset : offset + limit]
390
+
391
+ return {
392
+ "documents": paginated,
393
+ "total": total,
394
+ "limit": limit,
395
+ "offset": offset,
396
+ }
397
+
398
+ def count(self, index_name: str) -> int:
399
+ """Count documents in index.
400
+
401
+ Args:
402
+ index_name: Index to count.
403
+
404
+ Returns:
405
+ Document count.
406
+ """
407
+ return self._indexer.count(index_name)
408
+
409
+ async def get_collections(self, index_name: str) -> list[dict[str, Any]]:
410
+ """Get unique collections with document counts.
411
+
412
+ Args:
413
+ index_name: Index to query.
414
+
415
+ Returns:
416
+ List of collections with id, name, and document_count.
417
+ """
418
+ documents = self._indexer.get_all(index_name)
419
+ collections: dict[str, dict[str, Any]] = {}
420
+
421
+ for doc in documents:
422
+ col_id = doc.get("collection_id")
423
+ if not col_id:
424
+ continue
425
+
426
+ if col_id not in collections:
427
+ collections[col_id] = {
428
+ "id": col_id,
429
+ "name": doc.get("collection_name") or col_id,
430
+ "document_count": 0,
431
+ }
432
+ collections[col_id]["document_count"] += 1
433
+
434
+ return list(collections.values())
435
+
436
+ async def get_stats(self, index_name: str) -> dict[str, Any]:
437
+ """Get index statistics.
438
+
439
+ Args:
440
+ index_name: Index to query.
441
+
442
+ Returns:
443
+ Dictionary with document_count and index info.
444
+ """
445
+ count = self._indexer.count(index_name)
446
+ return {
447
+ "document_count": count,
448
+ "index_name": index_name,
449
+ "exists": count > 0 or index_name in self._indexer._indices,
450
+ }
451
+
351
452
  def _apply_filters(
352
453
  self,
353
454
  documents: list[dict[str, Any]],
@@ -355,6 +456,10 @@ class MemorySearcher:
355
456
  ) -> list[dict[str, Any]]:
356
457
  """Apply query filters to documents.
357
458
 
459
+ Note:
460
+ This method is tenant-agnostic. Multi-tenancy should be handled
461
+ at the API layer by using separate indices per account.
462
+
358
463
  Args:
359
464
  documents: Documents to filter.
360
465
  query: Query with filter parameters.
@@ -364,10 +469,6 @@ class MemorySearcher:
364
469
  """
365
470
  filtered = documents
366
471
 
367
- # Account filter
368
- if query.account_id:
369
- filtered = [d for d in filtered if d.get("account_id") == query.account_id]
370
-
371
472
  # Collection filter
372
473
  if query.collection_ids:
373
474
  filtered = [
@@ -378,9 +479,9 @@ class MemorySearcher:
378
479
  if query.source_ids:
379
480
  filtered = [d for d in filtered if d.get("source_id") in query.source_ids]
380
481
 
381
- # Custom filters
382
- if query.filters:
383
- for field, value in query.filters.items():
482
+ # Custom metadata filters
483
+ if query.metadata_filters:
484
+ for field, value in query.metadata_filters.items():
384
485
  if isinstance(value, list):
385
486
  filtered = [d for d in filtered if d.get(field) in value]
386
487
  else: