library_syncer
zocrates.rag.library_syncer ¶
Background ingestion worker.
Coordinates the document processing pipeline: ZoteroDB → DocumentProcessor → Chunker → VectorStore
CHUNK_OVERLAP_CHARS
module-attribute
¶
DocumentProcessor ¶
DocumentProcessor(cache_dir, converters=None, document_converter_id=None, datalab_non_pdf_fallback='markitdown')
Converts attachments (PDF, HTML, etc.) to Markdown with local caching and image extraction. Manages cache persistence and delegates conversion to registered BaseDocumentConverters.
Initialize the DocumentProcessor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path
|
Directory used to store converted Markdown files and the
|
required |
converters
|
list[BaseDocumentConverter] | None
|
Optional list of document converters. If None, defaults to [DirectReadConverter(), MarkItDownConverter(), DoclingLocalConverter(), DatalabConverter()]. |
None
|
document_converter_id
|
str | None
|
Global active converter ID from config.
When set, |
None
|
datalab_non_pdf_fallback
|
str
|
Fallback converter ID when active converter is 'datalab' and content_type is not PDF ('markitdown' or 'docling_local'). |
'markitdown'
|
Source code in zocrates/rag/document_processor.py
datalab_non_pdf_fallback
property
writable
¶
Return the fallback converter ID for non-PDF items when Datalab is active.
clear_failure ¶
Clear failure record(s) for an item and optionally a specific converter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
converter_id
|
str | None
|
Specific converter ID to clear, or None to clear all. |
None
|
Source code in zocrates/rag/document_processor.py
convert_with ¶
convert_with(item_key, converter_id, source_path, content_type, parent_item_type=None, title=None, collections=None, parent_item_key=None, authors=None, year=None, publication=None, doi=None, url=None, abstract=None, abstract_source=None, update_root_index=True)
Convert a document with a specific converter engine.
This is the explicit user-action path. No fallback -- if the specified converter doesn't support the content type, an error is raised. Any existing conversion for this item is destroyed and replaced.
The caller is responsible for re-indexing the vector store after this method returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
converter_id
|
str
|
ID of the converter to use. |
required |
source_path
|
Path
|
Path to the source file on disk. |
required |
content_type
|
str
|
MIME type of the source file. |
required |
parent_item_type
|
str | None
|
Zotero parent item type. |
None
|
title
|
str | None
|
Parent item title. |
None
|
collections
|
list[Any] | None
|
Parent item collections. |
None
|
parent_item_key
|
str | None
|
Parent Zotero item key string. |
None
|
authors
|
list[str] | None
|
Optional author names list. |
None
|
year
|
str | int | None
|
Optional publication year. |
None
|
publication
|
str | None
|
Optional publication venue string. |
None
|
doi
|
str | None
|
Optional DOI string. |
None
|
url
|
str | None
|
Optional source URL string. |
None
|
abstract
|
str | None
|
Optional item abstract. |
None
|
abstract_source
|
str | None
|
Optional source of item abstract. |
None
|
update_root_index
|
bool
|
If True (default), updates the root index.md. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
The converted Markdown text (with YAML frontmatter). |
Raises:
| Type | Description |
|---|---|
UnsupportedConversionError
|
If the converter doesn't support the content type. |
KeyError
|
If no converter with |
Source code in zocrates/rag/document_processor.py
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 | |
delete_cached ¶
Delete an item bundle from disk and conversion_meta DB.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key to remove. |
required |
update_root_index
|
bool
|
If True (default), updates the root index.md catalog. |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if an item bundle or DB row was deleted, False if item was not cached. |
Source code in zocrates/rag/document_processor.py
ensure_parent_item_key ¶
Ensure an item in conversion_meta has parent_item_key populated.
Source code in zocrates/rag/document_processor.py
get_all_meta_keys ¶
Return the set of all unique item keys tracked in conversion_meta.
Source code in zocrates/rag/document_processor.py
get_all_metadata ¶
Return metadata for all cached documents in a single fast query.
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Any]]
|
Dictionary mapping |
Source code in zocrates/rag/document_processor.py
get_failures ¶
Retrieve recorded conversion failures for an item key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Any]]
|
Dict mapping converter_id -> failure details dict. |
Source code in zocrates/rag/document_processor.py
get_metadata ¶
Fetch metadata dictionary for an item key from conversion_meta.db.
Source code in zocrates/rag/document_processor.py
get_or_convert ¶
get_or_convert(item_key, source_path, content_type, parent_item_type=None, title=None, collections=None, parent_item_key=None, authors=None, year=None, publication=None, doi=None, url=None, abstract=None, abstract_source=None, update_root_index=True)
Return the Markdown for an attachment, converting if not already cached or if the source file has changed since the last conversion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
source_path
|
Path
|
Path to the source file on disk. |
required |
content_type
|
str
|
MIME type of the source file. |
required |
parent_item_type
|
str | None
|
Zotero parent item type (e.g. 'journalArticle'). |
None
|
title
|
str | None
|
Parent item title. |
None
|
collections
|
list[Any] | None
|
Parent item collections list. |
None
|
parent_item_key
|
str | None
|
Parent Zotero item key string. |
None
|
authors
|
list[str] | None
|
Optional author names list. |
None
|
year
|
str | int | None
|
Optional publication year. |
None
|
publication
|
str | None
|
Optional publication venue string. |
None
|
doi
|
str | None
|
Optional DOI string. |
None
|
url
|
str | None
|
Optional source URL string. |
None
|
abstract
|
str | None
|
Optional abstract or summary text. |
None
|
abstract_source
|
str | None
|
Optional source of abstract ('zotero', 'generated', 'extractive'). |
None
|
update_root_index
|
bool
|
If True (default), updates the root index.md catalog. |
True
|
Returns:
| Type | Description |
|---|---|
str | None
|
The converted Markdown text with YAML frontmatter, or None on error. |
Source code in zocrates/rag/document_processor.py
is_cached ¶
Return True if a converted Markdown file exists for item_key.
Does not validate freshness against the source file hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in zocrates/rag/document_processor.py
list_cached_keys ¶
Return the item keys of all documents currently in the Markdown cache.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of item key strings (subdirectories containing |
Source code in zocrates/rag/document_processor.py
read_cached ¶
Read cached Markdown without triggering conversion.
Source code in zocrates/rag/document_processor.py
record_failure ¶
Record or update a failed conversion attempt for an item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
converter_id
|
str
|
Converter engine ID that failed. |
required |
error_message
|
str
|
Error description or exception string. |
required |
source_hash
|
str
|
SHA-256 hex digest of the source file at failure time. |
required |
Source code in zocrates/rag/document_processor.py
regenerate_root_index ¶
register_converter ¶
Register a new document converter strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
converter
|
BaseDocumentConverter
|
Instance of BaseDocumentConverter to register. |
required |
prepend
|
bool
|
If True (default), inserts the converter at the highest priority. |
True
|
Source code in zocrates/rag/document_processor.py
update_abstract ¶
Update abstract and abstract_source in conversion_meta for an item.
Source code in zocrates/rag/document_processor.py
update_indexed_at ¶
Update the indexed_at timestamp for item_key in conversion_meta DB.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
timestamp
|
str | None
|
Optional ISO-8601 timestamp string. Defaults to current UTC time. |
None
|
Source code in zocrates/rag/document_processor.py
LibrarySyncer ¶
Coordinates RAG indexing pipeline.
Initialize the LibrarySyncer with all RAG pipeline components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zotero_data_dir
|
Path
|
Root Zotero data directory containing
|
required |
sqlite_path
|
Path
|
Absolute path to |
required |
Source code in zocrates/rag/library_syncer.py
close ¶
Tear down resources held by this syncer instance.
Source code in zocrates/rag/library_syncer.py
full_sync
async
¶
Scan every item, note, and standalone attachment in the library and ingest those not yet indexed.
Additive Phase 7 onboarding parameters (backward compatible):
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
progress_callback
|
Callable[[dict], None] | None
|
Optional synchronous callback invoked at each
item boundary with a progress dict ( |
None
|
cancel_check
|
Callable[[], bool] | None
|
Optional cooperative-cancel predicate polled at the
existing per-item yield points. When it returns True the sync
finishes the current item and stops, skipping the tail
graph-relations/orphan-purge phases. Resume semantics are
untouched ( |
None
|
Source code in zocrates/rag/library_syncer.py
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 | |
ingest_items
async
¶
Force-ingest a list of item, note, or attachment IDs (e.g. newly added by Zotero).
Source code in zocrates/rag/library_syncer.py
purge_item ¶
Atomically purge an item across all four datastores: disk bundle, metadata DB, vector/FTS store, and root index.
Source code in zocrates/rag/library_syncer.py
sync_item
async
¶
Sync a single item, note, or attachment by ID or key.
Source code in zocrates/rag/library_syncer.py
sync_metadata_staleness ¶
Scan cached bundles and synchronize frontmatter and FTS5 for any modified in Zotero.
Executes a <5ms batched staleness check comparing conversion_meta.db against zotero.sqlite. If items are stale, updates their YAML frontmatter, toc.json, chunks_fts Chunk 0, and Chroma metadata without re-running document converters or OCR engines.
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Summary dict containing counts of total, updated, skipped, and errored items. |
Source code in zocrates/rag/library_syncer.py
VectorStore ¶
Hybrid vector + keyword search index for Zotero document chunks. Uses versioned Chroma collections per embedding model/dimension.
Initialize the VectorStore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chroma_dir
|
Path
|
Directory for ChromaDB persistent storage. |
required |
fts_db_path
|
Path
|
Filesystem path for the SQLite FTS5 database. |
required |
embedder
|
EmbedFn
|
Callable that maps a list of strings to a list of
embedding vectors and exposes |
required |
Source code in zocrates/rag/vector_store.py
acquire_write_lock ¶
Return the store's RLock for external write operations (e.g., model swap).
Returns:
| Type | Description |
|---|---|
Any
|
The |
close ¶
Close the Chroma client and release underlying storage handles.
Source code in zocrates/rag/vector_store.py
delete_document ¶
Remove all chunks for the given item_key from both Chroma and FTS5.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key identifying the document to delete. |
required |
get_all_chunk_ids_for_item ¶
Return all chunk IDs for an item key in chunk_index order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of chunk IDs. |
Source code in zocrates/rag/vector_store.py
get_all_ingested_item_keys ¶
Return the set of all unique item_key strings currently ingested in the active collection.
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of unique item_key strings. |
Source code in zocrates/rag/vector_store.py
get_all_item_keys ¶
Return the set of all unique item keys indexed in the chunk_meta SQLite table.
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of unique item_key strings. |
Source code in zocrates/rag/vector_store.py
get_intersecting_chunk_ids ¶
Return chunk IDs for an item key that intersect the given line range [start_line, end_line].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
start_line
|
int
|
1-indexed start line of the range. |
required |
end_line
|
int
|
1-indexed end line of the range. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of chunk IDs in chunk_index order. |
Source code in zocrates/rag/vector_store.py
ingest_document ¶
Index all chunks from a single document into Chroma and FTS5.
Deletes any existing chunks for item_key before inserting, making
the operation idempotent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Unique Zotero item key used as the primary document ID. |
required |
item_id
|
int
|
Numeric Zotero item ID stored as chunk metadata. |
required |
chunks
|
list[ChunkResult]
|
Ordered list of |
required |
extra_metadata
|
dict[str, Any] | None
|
Additional key/value pairs merged into every chunk's Chroma metadata dict. |
None
|
Source code in zocrates/rag/vector_store.py
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 | |
is_ingested ¶
Check whether any chunks exist for item_key in the active collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key to look up. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in zocrates/rag/vector_store.py
list_collections ¶
List all stored versioned Chroma collections with their metadata and chunk counts.
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of dicts, each with keys |
list[dict[str, Any]]
|
|
Source code in zocrates/rag/vector_store.py
reset_collection ¶
Reset/clear vector store collection(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
str
|
Which collection(s) to reset:
- |
'active'
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict with keys |
dict[str, Any]
|
|
Source code in zocrates/rag/vector_store.py
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 | |
search ¶
search(query, top_k=RETRIEVAL_TOP_K, final_k=RETRIEVAL_FINAL_K, item_key_filter=None, chunk_type_filter=None, exclude_chunk_ids=None, reranker=None)
Hybrid (semantic + BM25) search with RRF fusion and raw score metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Natural language query string. |
required |
top_k
|
int
|
Number of candidates retrieved from each index before fusion. |
RETRIEVAL_TOP_K
|
final_k
|
int
|
Maximum number of results to return after fusion. |
RETRIEVAL_FINAL_K
|
item_key_filter
|
list[str] | None
|
If provided, restrict search to these Zotero item keys. |
None
|
chunk_type_filter
|
str | list[str] | None
|
Optional chunk type or list of chunk types to filter by. |
None
|
exclude_chunk_ids
|
set[str] | list[str] | None
|
Optional set or list of chunk IDs to exclude from results. |
None
|
reranker
|
Callable[[str, list[str]], list[float]] | None
|
Optional callable |
None
|
Returns:
| Type | Description |
|---|---|
list[SearchResult]
|
List of |
list[SearchResult]
|
capped at |
Source code in zocrates/rag/vector_store.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 | |
update_chunk_text_preserve_vector ¶
Update stored document text in Chroma while explicitly preserving existing vector.
Passing existing embeddings explicitly bypasses Chroma's internal embedding function, guaranteeing zero ML overhead, zero network roundtrips, and strict dimension alignment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_id
|
str
|
Canonical chunk identifier (e.g. |
required |
new_text
|
str
|
New document text for the chunk. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if chunk was found, had embeddings, and was updated; False otherwise. |
Source code in zocrates/rag/vector_store.py
update_metadata ¶
Update metadata for all chunks belonging to item_key without re-embedding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
metadata
|
dict[str, Any]
|
Key/value pairs to merge into existing chunk metadatas. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if chunks were found and updated, False if item was not found. |
Source code in zocrates/rag/vector_store.py
ZoteroAttachment
dataclass
¶
An attachment (PDF, HTML, etc.) linked to a Zotero item.
ZoteroDB ¶
Read-only façade over zotero.sqlite.
Initialize ZoteroDB.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sqlite_path
|
Path
|
Absolute path to the |
required |
storage_dir
|
Path
|
Absolute path to the Zotero |
required |
Source code in zocrates/rag/zotero_db.py
get_active_item_keys ¶
Return a set of item keys that exist in the library and are not deleted or parent-deleted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_keys
|
list[str]
|
List of Zotero item keys (can include regular items, attachments, notes). |
required |
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of active item keys. |
Source code in zocrates/rag/zotero_db.py
get_all_arxiv_ids ¶
Return a set of all normalized arXiv IDs across active non-deleted items in the library.
Source code in zocrates/rag/zotero_db.py
get_all_attachments ¶
Return all file attachments with a parent item across the library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_types
|
tuple[str, ...]
|
Tuple of MIME types to include. |
('application/pdf', 'text/html', 'text/markdown', 'text/plain')
|
Returns:
| Type | Description |
|---|---|
list[ZoteroAttachment]
|
List of |
Source code in zocrates/rag/zotero_db.py
get_all_convertible_attachments ¶
Return all valid document attachments in the library in a single fast query.
Source code in zocrates/rag/zotero_db.py
get_all_dedup_records ¶
Return lightweight records (title, year, creators, doi, arxiv_id) for active items for fuzzy dedup.
Source code in zocrates/rag/zotero_db.py
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 | |
get_all_dois ¶
Return a set of all normalized DOIs across active non-deleted items in the library.
Source code in zocrates/rag/zotero_db.py
get_all_item_relations ¶
Return all explicit item relations as (source_key, target_key) pairs.
Source code in zocrates/rag/zotero_db.py
get_all_items ¶
Return all non-attachment, non-deleted items in the library.
Returns:
| Type | Description |
|---|---|
list[ZoteroItem]
|
List of fully populated |
Source code in zocrates/rag/zotero_db.py
get_all_notes ¶
Return all notes (standalone and child notes) in the library.
Returns:
| Type | Description |
|---|---|
list[ZoteroNote]
|
List of |
Source code in zocrates/rag/zotero_db.py
get_all_titles ¶
Return a list of all titles across active non-deleted items in the library.
Source code in zocrates/rag/zotero_db.py
get_attachment_by_id ¶
Fetch a single file attachment by its item ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_id
|
int
|
Numeric Zotero item ID of the attachment. |
required |
Returns:
| Type | Description |
|---|---|
ZoteroAttachment | None
|
|
ZoteroAttachment | None
|
or deleted. |
Source code in zocrates/rag/zotero_db.py
get_attachment_by_key ¶
Fetch a single file attachment by its item key or parent item key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key string (attachment key or parent item key). |
required |
Returns:
| Type | Description |
|---|---|
ZoteroAttachment | None
|
|
Source code in zocrates/rag/zotero_db.py
get_attachments ¶
get_attachments(parent_item_id, content_types=('application/pdf', 'text/html', 'text/markdown', 'text/plain'))
Return file attachments for a given parent item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent_item_id
|
int
|
Numeric ID of the parent Zotero item. |
required |
content_types
|
tuple[str, ...]
|
Tuple of MIME types to include; defaults to PDF, HTML, Markdown, and plain text. |
('application/pdf', 'text/html', 'text/markdown', 'text/plain')
|
Returns:
| Type | Description |
|---|---|
list[ZoteroAttachment]
|
List of |
list[ZoteroAttachment]
|
paths ( |
list[ZoteroAttachment]
|
The |
list[ZoteroAttachment]
|
available, otherwise the attachment filename. |
Source code in zocrates/rag/zotero_db.py
get_item ¶
Fetch a single item with full metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_id
|
int
|
Numeric Zotero item ID. |
required |
Returns:
| Type | Description |
|---|---|
ZoteroItem | None
|
Fully populated |
ZoteroItem | None
|
and relations), or |
Source code in zocrates/rag/zotero_db.py
get_item_by_key ¶
Fetch a single item by its string key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key string (e.g. '89X2ELQ6'). |
required |
Returns:
| Type | Description |
|---|---|
ZoteroItem | None
|
Fully populated |
Source code in zocrates/rag/zotero_db.py
get_item_date_modified ¶
Fetch the last modified timestamp for an item as timezone-aware UTC datetime.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
8-character Zotero alphanumeric key. |
required |
Returns:
| Type | Description |
|---|---|
datetime | None
|
Timezone-aware UTC datetime, or None if item not found. |
Source code in zocrates/rag/zotero_db.py
get_item_keys_in_collection ¶
Return the unique item keys for all items belonging to a collection name or key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
collection_name
|
str
|
Name or key of the collection. |
required |
recursive
|
bool
|
If True and parentCollectionID exists, includes items in all subcollections. |
True
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of unique item keys. |
Source code in zocrates/rag/zotero_db.py
get_item_relations ¶
Return target item keys related to the specified item.
Source code in zocrates/rag/zotero_db.py
get_item_tags ¶
Return tag names for an item, optionally filtered by tag type (0=user, 1=automatic).
Source code in zocrates/rag/zotero_db.py
get_items_by_ids ¶
Fetch multiple items by their numeric IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_ids
|
list[int]
|
List of numeric Zotero item IDs. |
required |
Returns:
| Type | Description |
|---|---|
list[ZoteroItem]
|
List of fully populated |
list[ZoteroItem]
|
tags, and collections). Items not found or deleted are silently |
list[ZoteroItem]
|
omitted. |
Source code in zocrates/rag/zotero_db.py
get_new_item_ids_since ¶
Return item IDs created after last_seen_item_id for incremental sync.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
last_seen_item_id
|
int
|
The highest item ID already processed. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
Sorted list of numeric item IDs greater than |
list[int]
|
that are not attachments, notes, or deleted items. |
Source code in zocrates/rag/zotero_db.py
get_note_by_id ¶
Fetch a single note by note item ID.
Source code in zocrates/rag/zotero_db.py
get_note_by_key ¶
Fetch a single note by its Zotero item key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
note_key
|
str
|
The alphanumeric Zotero item key of the note. |
required |
Returns:
| Type | Description |
|---|---|
ZoteroNote | None
|
A |
ZoteroNote | None
|
or the item has been deleted. |
Source code in zocrates/rag/zotero_db.py
get_notes_for_item ¶
Return child notes attached to a specific parent item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent_item_id
|
int
|
Numeric ID of the parent Zotero item. |
required |
Returns:
| Type | Description |
|---|---|
list[ZoteroNote]
|
List of |
Source code in zocrates/rag/zotero_db.py
get_stale_item_keys ¶
Identify cached items whose Zotero metadata has been modified since conversion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
converted_map
|
dict[str, str]
|
Mapping of attachment/note item_key -> converted_at ISO string. |
required |
key_to_atts
|
dict[str, list[str]]
|
Mapping of query key (parent_item_key or direct item_key) to list of associated attachment/note item_keys. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Sorted, unique list of attachment/note item_keys whose metadata is stale. |
Source code in zocrates/rag/zotero_db.py
get_standalone_attachments ¶
get_standalone_attachments(content_types=('application/pdf', 'text/html', 'text/markdown', 'text/plain'))
Return standalone file attachments (attachments without a parent item).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_types
|
tuple[str, ...]
|
Tuple of MIME types to include. |
('application/pdf', 'text/html', 'text/markdown', 'text/plain')
|
Returns:
| Type | Description |
|---|---|
list[ZoteroAttachment]
|
List of |
Source code in zocrates/rag/zotero_db.py
get_standalone_notes ¶
Return top-level standalone notes (notes without a parent item).
Returns:
| Type | Description |
|---|---|
list[ZoteroNote]
|
List of |
Source code in zocrates/rag/zotero_db.py
invalidate_schema_cache ¶
is_item_active ¶
Check if a single item key exists and is not deleted (or parent-deleted).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the item exists and is active, False otherwise. |
Source code in zocrates/rag/zotero_db.py
ZoteroNote
dataclass
¶
A Zotero note item (standalone or attached to a parent item).
chunk_markdown ¶
chunk_markdown(markdown, max_chars=CHUNK_MAX_CHARS, overlap_chars=CHUNK_OVERLAP_CHARS, document_title='')
Chunk a Markdown document into semantically coherent pieces.
Splits by ATX headers (H1–H3) first, isolates figures and tables as
standalone atomic chunks with outer max_chars.
Preserves document order via anchor interleaving.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
markdown
|
str
|
Raw Markdown string to chunk. |
required |
max_chars
|
int
|
Maximum character count per chunk. Defaults to
|
CHUNK_MAX_CHARS
|
overlap_chars
|
int
|
Characters repeated at chunk boundaries for context
continuity. Defaults to |
CHUNK_OVERLAP_CHARS
|
document_title
|
str
|
Zotero item title attached to every |
''
|
Returns:
| Type | Description |
|---|---|
list[ChunkResult]
|
Ordered list of |
Source code in zocrates/rag/chunker.py
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 | |
convert_note_html_to_markdown ¶
Convert Zotero note HTML content to clean, structured Markdown.
Handles headings (h1–h6), paragraphs, ordered/unordered lists, blockquotes, pre/code blocks, tables, inline strong/em/code, anchors, and horizontal rules. Falls back to basic regex stripping if BeautifulSoup is unavailable.
Source code in zocrates/rag/document_processor.py
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 | |
get_chroma_dir ¶
Return (and create if needed) the ChromaDB storage directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zotero_data_dir
|
Path
|
Root Zotero data directory. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to |
Source code in zocrates/config.py
get_corpus_dir ¶
Return (and create if needed) the document corpus directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zotero_data_dir
|
Path
|
Root Zotero data directory. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to |
Source code in zocrates/config.py
get_datalab_non_pdf_fallback ¶
Return the fallback converter ID for non-PDF items when Datalab is active ('markitdown' or 'docling_local').
Checks DATALAB_NON_PDF_FALLBACK environment variable first;
if unset, falls back to datalab_non_pdf_fallback in zocrates_config.json
(defaulting to "markitdown").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zotero_data_dir
|
Path | None
|
Path to the Zotero data directory. |
None
|
cfg
|
dict[str, Any] | None
|
Optional in-memory config dict. If provided, bypasses disk read. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Fallback converter ID string ( |
Source code in zocrates/config.py
get_document_converter_id ¶
Return the document converter ID from the Zocrates config.
Checks DOCUMENT_CONVERTER_ID environment variable first;
if unset, falls back to document_converter_id in zocrates_config.json
(defaulting to "markitdown").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zotero_data_dir
|
Path | None
|
Path to the Zotero data directory. |
None
|
cfg
|
dict[str, Any] | None
|
Optional in-memory config dict. If provided, bypasses disk read. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Converter ID string (e.g., |
Source code in zocrates/config.py
get_embedder ¶
Return the configured embedding function.
Reads the active mode from the EMBEDDING_MODE environment variable,
falling back to the persisted config file value, then to "local".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str | None
|
Override for the embedding mode ( |
None
|
Returns:
| Type | Description |
|---|---|
LocalEmbedder | OpenRouterEmbedder
|
A |
LocalEmbedder | OpenRouterEmbedder
|
|
Source code in zocrates/rag/embeddings.py
get_fts_db_path ¶
Return the path to the FTS5 SQLite database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zotero_data_dir
|
Path
|
Root Zotero data directory. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to |
Source code in zocrates/config.py
get_sync_auto_abort_failures ¶
Return the consecutive-failure auto-abort threshold for full-library sync jobs.
Reads sync_auto_abort_failures from zocrates_config.json (defaulting
to DEFAULT_SYNC_AUTO_ABORT_FAILURES). Values <= 0 disable auto-abort.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
zotero_data_dir
|
Path | None
|
Path to the Zotero data directory. |
None
|
cfg
|
dict[str, Any] | None
|
Optional in-memory config dict. If provided, bypasses disk read. |
None
|
Source code in zocrates/config.py
materialize_note_bundle ¶
Create or refresh an OKF bundle for a Zotero note in the markdown cache.
The bundle is placed at <cache_dir>/<note.note_key>/ and contains
document.md, toc.json, and toc.md. Staleness is detected via
a SHA-256 hash of the raw HTML stored in conversion_meta.db.
Summary policy: never calls an LLM. - Notes < 100 plain-text words → no abstract. - Notes ≥ 100 plain-text words → deterministic extractive lead paragraph (first non-heading paragraph, capped at 300 characters).
Thread-safe: uses a process-wide lock and an atomic directory swap
(write to .tmp_{key} then rename).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
note
|
Any
|
A |
required |
syncer
|
Any
|
The active |
required |
cache_dir
|
Path | None
|
Override for the cache directory; defaults to
|
None
|
Returns:
| Type | Description |
|---|---|
tuple[Path, str]
|
Tuple of |
Source code in zocrates/rag/document_processor.py
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 | |