document_processor
zocrates.rag.document_processor ¶
Document processor: converts PDF/HTML attachments to Markdown and caches them.
Supports pluggable converter strategies via BaseDocumentConverter.
Default converters include:
- DirectReadConverter: Fast UTF-8 reader for .md and .txt files.
- MarkItDownConverter: Uses MarkItDown for document conversion with page-by-page
PDF image extraction via system utility pdfimages.
The cache directory lives in
Each item gets an item-centric bundle directory:
corpus/
CONVERTER_REGISTRY
module-attribute
¶
CONVERTER_REGISTRY = {'direct_read': DirectReadConverter, 'markitdown': MarkItDownConverter, 'docling_local': DoclingLocalConverter, 'datalab': DatalabConverter}
_META_SCHEMA
module-attribute
¶
_META_SCHEMA = '\nCREATE TABLE IF NOT EXISTS conversion_meta (\n item_key TEXT PRIMARY KEY,\n title TEXT,\n converter_id TEXT NOT NULL,\n converter_version TEXT NOT NULL,\n source_path TEXT NOT NULL,\n content_type TEXT NOT NULL,\n parent_item_type TEXT,\n converted_at TEXT NOT NULL,\n indexed_at TEXT,\n source_hash TEXT NOT NULL, -- SHA-256 of the source file\n char_count INTEGER NOT NULL,\n parent_item_key TEXT,\n parse_quality_score REAL,\n checkpoint_id TEXT,\n converter_mode TEXT, -- Datalab processing mode (NULL for other converters)\n abstract TEXT,\n abstract_source TEXT,\n keywords TEXT,\n updated_at TEXT\n);\n\nCREATE TABLE IF NOT EXISTS conversion_failures (\n item_key TEXT NOT NULL,\n converter_id TEXT NOT NULL,\n error_message TEXT NOT NULL,\n source_hash TEXT NOT NULL,\n failed_at TEXT NOT NULL,\n PRIMARY KEY (item_key, converter_id)\n);\n\nCREATE TABLE IF NOT EXISTS item_relations (\n source_key TEXT NOT NULL,\n target_key TEXT NOT NULL,\n score REAL NOT NULL,\n relation_type TEXT NOT NULL,\n match_evidence TEXT,\n updated_at TEXT NOT NULL,\n PRIMARY KEY (source_key, target_key, relation_type)\n);\nCREATE INDEX IF NOT EXISTS idx_relations_source_score ON item_relations (source_key, score DESC);\nCREATE INDEX IF NOT EXISTS idx_relations_target ON item_relations (target_key);\n'
BaseDocumentConverter ¶
Bases: ABC
Abstract base class for document conversion engines.
converter_id
abstractmethod
property
¶
Unique identifier for the converter strategy (e.g., 'markitdown', 'direct_read').
converter_version
property
¶
Version string for cache invalidation. Override in subclasses when needed.
is_remote
property
¶
Indicate whether this converter relies on an external/remote network service.
convert
abstractmethod
¶
Convert source document to Markdown text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key used for identification. |
required |
source_path
|
Path
|
Path to the source file on disk. |
required |
content_type
|
str
|
MIME type of the source file. |
required |
cache_dir
|
Path
|
Item-level bundle directory ( |
required |
options
|
dict[str, Any] | None
|
Optional converter-agnostic options dictionary resolved by
|
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Converted Markdown text, or None if conversion failed or produced empty content. |
Source code in zocrates/rag/document_converters.py
supports
abstractmethod
¶
Return True if this converter can process the given document.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_type
|
str
|
MIME type of the document (e.g., 'application/pdf'). |
required |
source_path
|
Path
|
Path to the source file on disk. |
required |
Source code in zocrates/rag/document_converters.py
DatalabConverter ¶
Bases: BaseDocumentConverter
Remote document conversion engine via Datalab OCR API.
Initialize the remote Datalab converter instance.
Source code in zocrates/rag/document_converters.py
convert ¶
Convert document via Datalab API with adaptive retry and asset unpacking.
Includes an extension safeguard for valid PDFs lacking a .pdf extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
source_path
|
Path
|
Source file path on disk. |
required |
content_type
|
str
|
MIME type string. |
required |
cache_dir
|
Path
|
Bundle directory (or temporary staging directory). |
required |
options
|
dict[str, Any] | None
|
Optional converter options resolved by |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Converted Markdown string with |
Source code in zocrates/rag/document_converters.py
get_metadata ¶
Return the latest conversion metadata for an item key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing parse_quality_score, checkpoint_id, mode, etc. |
Source code in zocrates/rag/document_converters.py
supports ¶
Check if document format is a PDF supported by Datalab.
Strictly limits conversion to PDF documents only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_type
|
str
|
MIME type string. |
required |
source_path
|
Path
|
File path on disk. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if file is a PDF, False otherwise. |
Source code in zocrates/rag/document_converters.py
DirectReadConverter ¶
Bases: BaseDocumentConverter
Direct UTF-8 reader for Markdown and plain text files (<1ms bypass).
convert ¶
Read Markdown or text content directly from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
source_path
|
Path
|
Path to the source file. |
required |
content_type
|
str
|
MIME type of the file. |
required |
cache_dir
|
Path
|
Item-level bundle directory (unused by this converter). |
required |
options
|
dict[str, Any] | None
|
Optional converter options (unused by this converter). |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
UTF-8 content string, or None if empty. |
Source code in zocrates/rag/document_converters.py
supports ¶
Check if content type or file extension corresponds to plain text or Markdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_type
|
str
|
MIME type string. |
required |
source_path
|
Path
|
File path on disk. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if file is plain text or Markdown, False otherwise. |
Source code in zocrates/rag/document_converters.py
DoclingLocalConverter ¶
Bases: BaseDocumentConverter
Document converter using IBM Docling for deep layout analysis and table extraction.
Initialize the DoclingLocalConverter strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
converter_instance
|
Any | None
|
Optional pre-configured Docling DocumentConverter instance. |
None
|
Source code in zocrates/rag/document_converters.py
converter_version
property
¶
Return the installed docling package version with mode indicator.
convert ¶
Convert document using Docling and save AST, images, and tables to bundle.
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 file. |
required |
cache_dir
|
Path
|
Item-level bundle directory ( |
required |
options
|
dict[str, Any] | None
|
Optional converter options (unused by this converter). |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Converted Markdown string, or None if conversion failed or returned empty text. |
Source code in zocrates/rag/document_converters.py
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 | |
supports ¶
Check if content type or file extension is supported by Docling.
Supports PDF, HTML, DOCX, PPTX, XLSX, etc.
Source code in zocrates/rag/document_converters.py
DocumentProcessor ¶
DocumentProcessor(cache_dir, converters=None, document_converter_id=None, datalab_non_pdf_fallback='markitdown', zotero_data_dir=None)
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'
|
zotero_data_dir
|
Path | None
|
Optional Zotero data directory used to resolve the configured Datalab processing mode; falls back to environment discovery when None. |
None
|
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, options=None, skip_converted=False)
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
|
options
|
dict[str, Any] | None
|
Optional converter-agnostic options dictionary. For the
Datalab converter, |
None
|
skip_converted
|
bool
|
If True, re-check for an already converted item
inside the per-item lock and return the cached Markdown
instead of converting. Endpoints use this for non-forced
requests so two concurrent requests for the same item cannot
both pay for a duplicate conversion. Unattended callers
( |
False
|
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
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 | |
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
has_child_attachments ¶
Return True if any attachment bundle exists in conversion_meta for parent_item_key.
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
MarkItDownConverter ¶
Bases: BaseDocumentConverter
Document converter using Microsoft MarkItDown and pdfimages for embedded images.
Initialize the MarkItDownConverter strategy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
markitdown_instance
|
Any | None
|
Optional pre-configured MarkItDown instance. |
None
|
Source code in zocrates/rag/document_converters.py
convert ¶
Convert document using MarkItDown and inject extracted PDF images if applicable.
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 file. |
required |
cache_dir
|
Path
|
Item-level bundle directory ( |
required |
options
|
dict[str, Any] | None
|
Optional converter options (unused by this converter). |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Converted Markdown string, or None if conversion failed or returned empty text. |
Source code in zocrates/rag/document_converters.py
supports ¶
MarkItDown serves as the default fallback converter for all supported documents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_type
|
str
|
MIME type of the document. |
required |
source_path
|
Path
|
Path to the source file on disk. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Always returns True. |
Source code in zocrates/rag/document_converters.py
UnsupportedConversionError ¶
Bases: Exception
Raised when a converter does not support the given content type.
_file_sha256 ¶
Compute the SHA-256 hex digest of a file.
Reads in 64 KiB chunks to keep memory usage constant for large PDFs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Absolute path to the file to hash. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Lowercase hex string of the SHA-256 digest. |
Source code in zocrates/rag/document_processor.py
_item_conversion_lock ¶
Return the per-item conversion lock for item_key within cache_dir.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path
|
Root corpus directory owning the item bundle. |
required |
item_key
|
str
|
Zotero item key being converted. |
required |
Returns:
| Type | Description |
|---|---|
Lock
|
The (lazily created) |
Source code in zocrates/rag/document_processor.py
_migrate_storage_paths_to_relative ¶
Migrate legacy absolute source_path entries to storage-relative paths.
Idempotent and atomic. Upgrades PRAGMA user_version from 0 to 1. Creates a pre-migration snapshot backup if not already present.
Source code in zocrates/rag/document_processor.py
_open_meta_db ¶
Open (or create) the conversion metadata SQLite database.
Creates the conversion_meta table if it does not already exist.
Enables WAL mode for concurrent readers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path
|
Directory containing the |
required |
Returns:
| Type | Description |
|---|---|
Connection
|
An open |
Connection
|
|
Source code in zocrates/rag/document_processor.py
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 | |
_relativize_staging_markdown_links ¶
Rewrite any temporary staging directory paths/URIs to bundle-relative images/ links.
Source code in zocrates/rag/document_processor.py
_sanitize_cached_bundle_image_links ¶
Sanitize legacy file:///.../images/ absolute links in cached document.md files.
Ensures strict line-count invariance to preserve ToC and chunk line offsets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path
|
Root corpus directory. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Count of sanitized document bundles. |
Source code in zocrates/rag/document_processor.py
build_frontmatter ¶
build_frontmatter(item_key, parent_item_type, title, collections, source_path, source_hash, content_type, converter_id, converter_version, converted_at, parent_item_key=None, parse_quality_score=None, checkpoint_id=None, authors=None, year=None, publication=None, doi=None, url=None, keywords=None, bundle_dir=None)
Build OKF-0.2 YAML frontmatter string.
Args:
item_key: Zotero item key.
parent_item_type: Zotero item type (e.g., 'journalArticle'). Defaults to 'document'.
title: Title of the parent item.
collections: List of ZoteroCollection objects or dicts with name and key/collection_key.
source_path: Absolute path to the source file on disk.
source_hash: SHA-256 hex digest of the source file.
content_type: MIME type of the source file.
converter_id: Converter engine ID.
converter_version: Converter engine version string.
converted_at: ISO-8601 timestamp string.
parent_item_key: Parent item key if item is an attachment.
parse_quality_score: Optional parse quality score (e.g. 0.0 to 5.0 from Datalab).
checkpoint_id: Optional converter checkpoint ID.
authors: Optional list of creator/author name strings.
year: Optional publication year (int or string).
publication: Optional publication venue or publisher string.
doi: Optional normalized DOI string.
url: Optional source URL string.
bundle_dir: Optional bundle directory (e.g. cache_dir / item_key). If provided,
source.path is computed as a relative POSIX path from bundle_dir.
Returns:
Formatted YAML frontmatter block ending with '---
'.
Source code in zocrates/rag/bundle_catalog.py
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 | |
compute_item_content_hash ¶
Compute canonical content and bibliographic SHA-256 hash for a metadata-only Zotero item.
Source code in zocrates/rag/document_processor.py
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
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 | |
generate_item_index_md ¶
Generate item-level index.md catalog inside item_dir.
Catalogs main document as well as all associated image and table assets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_dir
|
Path
|
Bundle directory (corpus/ |
required |
item_key
|
str
|
Zotero item key. |
required |
title
|
str | None
|
Title of the parent item. |
required |
converter_id
|
str
|
Converter engine ID. |
required |
converter_version
|
str
|
Converter engine version string. |
required |
Source code in zocrates/rag/bundle_catalog.py
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 | |
generate_root_index_md ¶
Regenerate root-level index.md catalog from conversion_meta.db.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path
|
Corpus directory containing conversion_meta.db. |
required |
Source code in zocrates/rag/bundle_catalog.py
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 | |
get_datalab_processing_mode ¶
Return the configured default Datalab processing mode.
Reads datalab_processing_mode from the in-memory cfg if provided,
otherwise from zocrates_config.json (seeded and backfilled there under
FileLock). An absent, blank, or invalid stored value is re-validated
here and falls back to "balanced" with a warning for invalid values.
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
|
One of |
Source code in zocrates/config.py
matches_conversion_identity ¶
Return True when stored metadata already satisfies an incremental request.
The recorded Datalab processing mode is part of the conversion identity, so
a fast conversion no longer blocks a balanced request. A stored
NULL mode matches any requested mode: legacy rows (pre-converter_mode)
and every non-Datalab converter record NULL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
meta
|
dict[str, Any] | None
|
Metadata dictionary from |
required |
converter_id
|
str
|
Effective converter ID for the request. |
required |
converter_mode
|
str | None
|
Effective Datalab processing mode (None for non-Datalab). |
required |
source_hash
|
str | None
|
SHA-256 hex digest of the current source file, or None when it could not be computed. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the item counts as already converted for this request. |
Source code in zocrates/rag/document_processor.py
materialize_item_metadata_bundle ¶
Create or refresh an OKF bundle for an attachment-free Zotero item in the markdown cache.
The bundle is placed at <cache_dir>/<item.item_key>/ and contains
document.md, toc.json, toc.md, index.md, and item_relations.md.
Staleness is detected via a SHA-256 hash of the canonical title, abstract, and bibliographic fields.
Zero LLM calls, thread-safe (via _metadata_bundle_lock), and atomic directory
swap (write to .tmp_{key} then rename).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item
|
Any
|
A |
required |
syncer
|
Any
|
The active |
required |
cache_dir
|
Path | None
|
Override for the cache directory; defaults to
|
None
|
force
|
bool
|
If True, bypasses hash fast-path and forces re-materialization. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[Path, str]
|
Tuple of |
Source code in zocrates/rag/document_processor.py
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 | |
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
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 | |
normalize_to_storage_relative ¶
Normalize a source document path to storage-relative format or verbatim URI.
Normalizes storage paths to 'storage/
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_path
|
str | Path | None
|
File path on disk, URI, or storage path string. |
required |
zotero_profile_dir
|
Path | None
|
Optional root Zotero profile directory. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Normalized storage-relative path string, URI, or absolute POSIX path. |
Source code in zocrates/rag/bundle_catalog.py
regenerate_item_relations_md ¶
Regenerate item_relations.md and refresh index.md for a given bundle.
Queries item_relations from conversion_meta.db for source_key = item_key. Also queries conversion_meta for target titles (fallback to target_key if not found). Target directory is target_bundle_dir if provided, else cache_dir / item_key. If target directory does not exist, returns None. Writes relations markdown atomically using .tmp_item_relations.md and os.replace (P1-4 pattern). Refreshes index.md catalog with the new/updated item_relations.md link.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_key
|
str
|
Zotero item key. |
required |
cache_dir
|
Path
|
Markdown cache directory containing conversion_meta.db. |
required |
target_bundle_dir
|
Path | None
|
Optional bundle directory override (e.g. staging dir). |
None
|
Returns:
| Type | Description |
|---|---|
Path | None
|
Path to the materialized item_relations.md, or None if target directory does not exist. |
Source code in zocrates/rag/bundle_catalog.py
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 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 | |
sanitize_image_links_in_markdown ¶
Sanitize legacy file:///.../images/ absolute links in markdown text.
Ensures strict line-count invariance to preserve ToC and chunk line offsets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Raw markdown text. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Markdown text with file:///.../images/ |
Source code in zocrates/rag/document_processor.py
strip_existing_frontmatter ¶
Strip leading YAML frontmatter block if present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
markdown
|
str
|
Input Markdown string. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Markdown content with leading YAML frontmatter removed. |
Source code in zocrates/rag/bundle_catalog.py
validate_datalab_mode ¶
Return the normalized Datalab processing mode, or None when not supported.
Shared validation helper for every mode resolution site: the
DocumentProcessor resolver, the DatalabConverter fallback of last
resort, and the config default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
Any
|
Candidate mode value (any type; non-strings are rejected). |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
|
str | None
|
stripped), or None if the value is not a supported mode. |
Source code in zocrates/config.py
wrap_standalone_markdown_tables ¶
Wrap standalone GFM Markdown tables not already enclosed in
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
markdown_text
|
str
|
Raw or converted Markdown text. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Markdown with standalone tables enclosed in |