@heripo/document-processor 0.1.27 → 0.1.29

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.
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { LoggerMethods } from '@heripo/logger';
2
- import { TokenUsageReport, DoclingDocument, PageRange, DocumentProcessResult, Caption, DoclingTextItem, DoclingPictureItem, DoclingTableItem, DoclingGroupItem, ProcessedImage, ProcessedTable, ProcessedFootnote, Chapter } from '@heripo/model';
2
+ import { TokenUsageReport, DoclingDocument, PageRange, ProcessedDocumentSource, DocumentProcessResult, Caption, DoclingTextItem, DoclingPictureItem, DoclingTableItem, DoclingGroupItem, ProcessedImage, ProcessedTable, ProcessedFootnote, Chapter, DoclingReference } from '@heripo/model';
3
3
  import { LanguageModel } from 'ai';
4
4
  import { LLMTokenUsageAggregator, ExtendedTokenUsage } from '@heripo/shared';
5
5
  import { z } from 'zod';
@@ -22,6 +22,10 @@ interface TocEntry {
22
22
  * Starting page number
23
23
  */
24
24
  pageNo: number;
25
+ /**
26
+ * Docling references for source nodes used to derive this TOC entry
27
+ */
28
+ sourceRefs?: string[];
25
29
  /**
26
30
  * Child TOC entries
27
31
  */
@@ -58,6 +62,8 @@ interface PageSizeGroup {
58
62
  pageNos: number[];
59
63
  }
60
64
 
65
+ declare const PROCESSED_DOCUMENT_SCHEMA_VERSION = "processed-document.v2";
66
+ type SourceRefValidationMode = 'off' | 'warn' | 'error';
61
67
  /**
62
68
  * DocumentProcessor Options
63
69
  */
@@ -148,6 +154,30 @@ interface DocumentProcessorProcessOptions {
148
154
  * When provided, automatic TOC extraction pipeline execution is skipped.
149
155
  */
150
156
  tocEntries?: TocEntry[];
157
+ /**
158
+ * Caller-supplied source artifact metadata for the Docling input.
159
+ */
160
+ source?: ProcessedDocumentSource;
161
+ /**
162
+ * Validate generated source references against the input Docling document.
163
+ *
164
+ * When true, missing references are treated as errors unless
165
+ * `sourceRefValidationMode` is provided.
166
+ *
167
+ * @experimental
168
+ */
169
+ validateSourceRefs?: boolean;
170
+ /**
171
+ * Controls how missing generated source references are handled.
172
+ *
173
+ * Explicit mode takes precedence over `validateSourceRefs`. Use `warn` for
174
+ * rollout visibility without failing processing, and `error` for strict
175
+ * provenance checks.
176
+ *
177
+ * @default 'off'
178
+ * @experimental
179
+ */
180
+ sourceRefValidationMode?: SourceRefValidationMode;
151
181
  }
152
182
  /**
153
183
  * DocumentProcessor
@@ -292,6 +322,9 @@ declare class DocumentProcessor {
292
322
  * Token usage is automatically tracked by PageRangeParser into the shared aggregator.
293
323
  */
294
324
  private parsePageRanges;
325
+ private resolveSourceRefValidationMode;
326
+ private validateProcessedDocumentSourceRefs;
327
+ private collectMissingSourceRefs;
295
328
  /**
296
329
  * Assemble the final ProcessedDocument
297
330
  *
@@ -789,6 +822,18 @@ declare class RefResolver {
789
822
  * Build an index mapping self_ref to the actual item
790
823
  */
791
824
  private buildIndex;
825
+ /**
826
+ * Check whether a Docling self reference exists in the indexed document.
827
+ */
828
+ hasRef(ref: string): boolean;
829
+ /**
830
+ * Assert that a Docling self reference exists in the indexed document.
831
+ */
832
+ assertRef(ref: string, context: string): void;
833
+ /**
834
+ * Assert that every Docling self reference exists in the indexed document.
835
+ */
836
+ assertRefs(refs: string[], context: string): void;
792
837
  /**
793
838
  * Resolve a $ref string to the actual item
794
839
  * @param ref - Reference string (e.g., "#/texts/0")
@@ -836,6 +881,7 @@ declare class RefResolver {
836
881
  * - Chapters: ch-001, ch-002, ...
837
882
  * - Images: img-001, img-002, ...
838
883
  * - Tables: tbl-001, tbl-002, ...
884
+ * - Text blocks: txt-001, txt-002, ...
839
885
  *
840
886
  * Each type maintains its own independent counter.
841
887
  */
@@ -844,6 +890,7 @@ declare class IdGenerator {
844
890
  private imageCounter;
845
891
  private tableCounter;
846
892
  private footnoteCounter;
893
+ private textBlockCounter;
847
894
  /**
848
895
  * Generate a chapter ID
849
896
  * @returns A chapter ID in the format "ch-001"
@@ -864,6 +911,11 @@ declare class IdGenerator {
864
911
  * @returns A footnote ID in the format "ftn-001"
865
912
  */
866
913
  generateFootnoteId(): string;
914
+ /**
915
+ * Generate a text block ID
916
+ * @returns A text block ID in the format "txt-001"
917
+ */
918
+ generateTextBlockId(): string;
867
919
  /**
868
920
  * Reset all counters to zero
869
921
  */
@@ -876,6 +928,7 @@ declare class IdGenerator {
876
928
  image: number;
877
929
  table: number;
878
930
  footnote: number;
931
+ textBlock: number;
879
932
  };
880
933
  /**
881
934
  * Pad a number to 3 digits with leading zeros
@@ -1500,6 +1553,17 @@ interface CaptionProcessingPipelineDeps {
1500
1553
  usageAggregator: LLMTokenUsageAggregator;
1501
1554
  abortSignal?: AbortSignal;
1502
1555
  }
1556
+ /**
1557
+ * Caption text and source references extracted from Docling caption links.
1558
+ *
1559
+ * `text` is omitted when none of the captions resolved to non-empty text;
1560
+ * `sourceRefs` is always present and contains every `$ref` caption in order
1561
+ * (string captions contribute to `text` only).
1562
+ */
1563
+ interface CaptionSourceExtraction {
1564
+ text?: string;
1565
+ sourceRefs: string[];
1566
+ }
1503
1567
  /**
1504
1568
  * CaptionProcessingPipeline
1505
1569
  *
@@ -1510,6 +1574,7 @@ interface CaptionProcessingPipelineDeps {
1510
1574
  * 4. Reparse failed captions with fallback model
1511
1575
  */
1512
1576
  declare class CaptionProcessingPipeline {
1577
+ private static readonly CAPTION_JOIN_SEPARATOR;
1513
1578
  private readonly logger;
1514
1579
  private readonly captionParser;
1515
1580
  private readonly captionValidator;
@@ -1536,13 +1601,25 @@ declare class CaptionProcessingPipeline {
1536
1601
  */
1537
1602
  processResourceCaptions(captionTexts: Array<string | undefined>, resourceType: string): Promise<Map<number, Caption>>;
1538
1603
  /**
1539
- * Extract caption text from resource
1604
+ * Extract combined caption text from resource captions.
1540
1605
  *
1541
- * Handles both string references and $ref resolution
1606
+ * Returns every caption joined with a single space; empty/whitespace-only
1607
+ * entries are skipped. Returns `undefined` when nothing resolves to text.
1608
+ *
1609
+ * @deprecated Use {@link extractCaptionSource} to also receive the
1610
+ * caption `$ref` list. This helper is retained as a thin wrapper for
1611
+ * legacy callers.
1542
1612
  */
1543
- extractCaptionText(captions: Array<string | {
1544
- $ref: string;
1545
- }> | undefined): string | undefined;
1613
+ extractCaptionText(captions: Array<string | DoclingReference> | undefined): string | undefined;
1614
+ /**
1615
+ * Extract caption text and source references from resource captions.
1616
+ *
1617
+ * Iterates captions in order. String captions contribute to text only;
1618
+ * `$ref` captions also push the ref onto `sourceRefs` regardless of whether
1619
+ * the resolver finds text. Resolved/raw text is trimmed, and empty parts
1620
+ * are skipped before joining.
1621
+ */
1622
+ extractCaptionSource(captions: Array<string | DoclingReference> | undefined): CaptionSourceExtraction;
1546
1623
  }
1547
1624
 
1548
1625
  /**
@@ -1622,4 +1699,4 @@ declare class ResourceConverter {
1622
1699
  convertFootnotes(doclingDoc: DoclingDocument): ProcessedFootnote[];
1623
1700
  }
1624
1701
 
1625
- export { BaseLLMComponent, type BaseLLMComponentOptions, BaseValidator, type BaseValidatorOptions, CONTINUATION_MARKERS, CaptionParseError, CaptionParser, type CaptionParserOptions, CaptionProcessingPipeline, type CaptionProcessingPipelineDeps, CaptionValidationError, CaptionValidator, type CaptionValidatorOptions, ChapterConverter, DocumentProcessor, type DocumentProcessorOptions, type DocumentProcessorProcessOptions, type ImageContent, PAGE_NUMBER_PATTERN, PagePattern, PageRangeParseError, PageRangeParser, type PageSizeGroup, ResourceConverter, TOC_KEYWORDS, TextLLMComponent, type TocAreaResult, type TocContentValidationResult, TocContentValidationSchema, TocContentValidator, type TocContentValidatorOptions, type TocEntry, TocEntrySchema, TocExtractError, TocExtractionPipeline, type TocExtractionPipelineDeps, TocExtractor, type TocExtractorOptions, TocFinder, type TocFinderOptions, TocNotFoundError, TocParseError, type TocResponse, TocResponseSchema, VisionLLMComponent, type VisionLLMComponentOptions, type VisionTocExtractionResult, VisionTocExtractionSchema, VisionTocExtractor, type VisionTocExtractorOptions, extractMaxPageNumber };
1702
+ export { BaseLLMComponent, type BaseLLMComponentOptions, BaseValidator, type BaseValidatorOptions, CONTINUATION_MARKERS, CaptionParseError, CaptionParser, type CaptionParserOptions, CaptionProcessingPipeline, type CaptionProcessingPipelineDeps, type CaptionSourceExtraction, CaptionValidationError, CaptionValidator, type CaptionValidatorOptions, ChapterConverter, DocumentProcessor, type DocumentProcessorOptions, type DocumentProcessorProcessOptions, type ImageContent, PAGE_NUMBER_PATTERN, PROCESSED_DOCUMENT_SCHEMA_VERSION, PagePattern, PageRangeParseError, PageRangeParser, type PageSizeGroup, ResourceConverter, type SourceRefValidationMode, TOC_KEYWORDS, TextLLMComponent, type TocAreaResult, type TocContentValidationResult, TocContentValidationSchema, TocContentValidator, type TocContentValidatorOptions, type TocEntry, TocEntrySchema, TocExtractError, TocExtractionPipeline, type TocExtractionPipelineDeps, TocExtractor, type TocExtractorOptions, TocFinder, type TocFinderOptions, TocNotFoundError, TocParseError, type TocResponse, TocResponseSchema, VisionLLMComponent, type VisionLLMComponentOptions, type VisionTocExtractionResult, VisionTocExtractionSchema, VisionTocExtractor, type VisionTocExtractorOptions, extractMaxPageNumber };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { LoggerMethods } from '@heripo/logger';
2
- import { TokenUsageReport, DoclingDocument, PageRange, DocumentProcessResult, Caption, DoclingTextItem, DoclingPictureItem, DoclingTableItem, DoclingGroupItem, ProcessedImage, ProcessedTable, ProcessedFootnote, Chapter } from '@heripo/model';
2
+ import { TokenUsageReport, DoclingDocument, PageRange, ProcessedDocumentSource, DocumentProcessResult, Caption, DoclingTextItem, DoclingPictureItem, DoclingTableItem, DoclingGroupItem, ProcessedImage, ProcessedTable, ProcessedFootnote, Chapter, DoclingReference } from '@heripo/model';
3
3
  import { LanguageModel } from 'ai';
4
4
  import { LLMTokenUsageAggregator, ExtendedTokenUsage } from '@heripo/shared';
5
5
  import { z } from 'zod';
@@ -22,6 +22,10 @@ interface TocEntry {
22
22
  * Starting page number
23
23
  */
24
24
  pageNo: number;
25
+ /**
26
+ * Docling references for source nodes used to derive this TOC entry
27
+ */
28
+ sourceRefs?: string[];
25
29
  /**
26
30
  * Child TOC entries
27
31
  */
@@ -58,6 +62,8 @@ interface PageSizeGroup {
58
62
  pageNos: number[];
59
63
  }
60
64
 
65
+ declare const PROCESSED_DOCUMENT_SCHEMA_VERSION = "processed-document.v2";
66
+ type SourceRefValidationMode = 'off' | 'warn' | 'error';
61
67
  /**
62
68
  * DocumentProcessor Options
63
69
  */
@@ -148,6 +154,30 @@ interface DocumentProcessorProcessOptions {
148
154
  * When provided, automatic TOC extraction pipeline execution is skipped.
149
155
  */
150
156
  tocEntries?: TocEntry[];
157
+ /**
158
+ * Caller-supplied source artifact metadata for the Docling input.
159
+ */
160
+ source?: ProcessedDocumentSource;
161
+ /**
162
+ * Validate generated source references against the input Docling document.
163
+ *
164
+ * When true, missing references are treated as errors unless
165
+ * `sourceRefValidationMode` is provided.
166
+ *
167
+ * @experimental
168
+ */
169
+ validateSourceRefs?: boolean;
170
+ /**
171
+ * Controls how missing generated source references are handled.
172
+ *
173
+ * Explicit mode takes precedence over `validateSourceRefs`. Use `warn` for
174
+ * rollout visibility without failing processing, and `error` for strict
175
+ * provenance checks.
176
+ *
177
+ * @default 'off'
178
+ * @experimental
179
+ */
180
+ sourceRefValidationMode?: SourceRefValidationMode;
151
181
  }
152
182
  /**
153
183
  * DocumentProcessor
@@ -292,6 +322,9 @@ declare class DocumentProcessor {
292
322
  * Token usage is automatically tracked by PageRangeParser into the shared aggregator.
293
323
  */
294
324
  private parsePageRanges;
325
+ private resolveSourceRefValidationMode;
326
+ private validateProcessedDocumentSourceRefs;
327
+ private collectMissingSourceRefs;
295
328
  /**
296
329
  * Assemble the final ProcessedDocument
297
330
  *
@@ -789,6 +822,18 @@ declare class RefResolver {
789
822
  * Build an index mapping self_ref to the actual item
790
823
  */
791
824
  private buildIndex;
825
+ /**
826
+ * Check whether a Docling self reference exists in the indexed document.
827
+ */
828
+ hasRef(ref: string): boolean;
829
+ /**
830
+ * Assert that a Docling self reference exists in the indexed document.
831
+ */
832
+ assertRef(ref: string, context: string): void;
833
+ /**
834
+ * Assert that every Docling self reference exists in the indexed document.
835
+ */
836
+ assertRefs(refs: string[], context: string): void;
792
837
  /**
793
838
  * Resolve a $ref string to the actual item
794
839
  * @param ref - Reference string (e.g., "#/texts/0")
@@ -836,6 +881,7 @@ declare class RefResolver {
836
881
  * - Chapters: ch-001, ch-002, ...
837
882
  * - Images: img-001, img-002, ...
838
883
  * - Tables: tbl-001, tbl-002, ...
884
+ * - Text blocks: txt-001, txt-002, ...
839
885
  *
840
886
  * Each type maintains its own independent counter.
841
887
  */
@@ -844,6 +890,7 @@ declare class IdGenerator {
844
890
  private imageCounter;
845
891
  private tableCounter;
846
892
  private footnoteCounter;
893
+ private textBlockCounter;
847
894
  /**
848
895
  * Generate a chapter ID
849
896
  * @returns A chapter ID in the format "ch-001"
@@ -864,6 +911,11 @@ declare class IdGenerator {
864
911
  * @returns A footnote ID in the format "ftn-001"
865
912
  */
866
913
  generateFootnoteId(): string;
914
+ /**
915
+ * Generate a text block ID
916
+ * @returns A text block ID in the format "txt-001"
917
+ */
918
+ generateTextBlockId(): string;
867
919
  /**
868
920
  * Reset all counters to zero
869
921
  */
@@ -876,6 +928,7 @@ declare class IdGenerator {
876
928
  image: number;
877
929
  table: number;
878
930
  footnote: number;
931
+ textBlock: number;
879
932
  };
880
933
  /**
881
934
  * Pad a number to 3 digits with leading zeros
@@ -1500,6 +1553,17 @@ interface CaptionProcessingPipelineDeps {
1500
1553
  usageAggregator: LLMTokenUsageAggregator;
1501
1554
  abortSignal?: AbortSignal;
1502
1555
  }
1556
+ /**
1557
+ * Caption text and source references extracted from Docling caption links.
1558
+ *
1559
+ * `text` is omitted when none of the captions resolved to non-empty text;
1560
+ * `sourceRefs` is always present and contains every `$ref` caption in order
1561
+ * (string captions contribute to `text` only).
1562
+ */
1563
+ interface CaptionSourceExtraction {
1564
+ text?: string;
1565
+ sourceRefs: string[];
1566
+ }
1503
1567
  /**
1504
1568
  * CaptionProcessingPipeline
1505
1569
  *
@@ -1510,6 +1574,7 @@ interface CaptionProcessingPipelineDeps {
1510
1574
  * 4. Reparse failed captions with fallback model
1511
1575
  */
1512
1576
  declare class CaptionProcessingPipeline {
1577
+ private static readonly CAPTION_JOIN_SEPARATOR;
1513
1578
  private readonly logger;
1514
1579
  private readonly captionParser;
1515
1580
  private readonly captionValidator;
@@ -1536,13 +1601,25 @@ declare class CaptionProcessingPipeline {
1536
1601
  */
1537
1602
  processResourceCaptions(captionTexts: Array<string | undefined>, resourceType: string): Promise<Map<number, Caption>>;
1538
1603
  /**
1539
- * Extract caption text from resource
1604
+ * Extract combined caption text from resource captions.
1540
1605
  *
1541
- * Handles both string references and $ref resolution
1606
+ * Returns every caption joined with a single space; empty/whitespace-only
1607
+ * entries are skipped. Returns `undefined` when nothing resolves to text.
1608
+ *
1609
+ * @deprecated Use {@link extractCaptionSource} to also receive the
1610
+ * caption `$ref` list. This helper is retained as a thin wrapper for
1611
+ * legacy callers.
1542
1612
  */
1543
- extractCaptionText(captions: Array<string | {
1544
- $ref: string;
1545
- }> | undefined): string | undefined;
1613
+ extractCaptionText(captions: Array<string | DoclingReference> | undefined): string | undefined;
1614
+ /**
1615
+ * Extract caption text and source references from resource captions.
1616
+ *
1617
+ * Iterates captions in order. String captions contribute to text only;
1618
+ * `$ref` captions also push the ref onto `sourceRefs` regardless of whether
1619
+ * the resolver finds text. Resolved/raw text is trimmed, and empty parts
1620
+ * are skipped before joining.
1621
+ */
1622
+ extractCaptionSource(captions: Array<string | DoclingReference> | undefined): CaptionSourceExtraction;
1546
1623
  }
1547
1624
 
1548
1625
  /**
@@ -1622,4 +1699,4 @@ declare class ResourceConverter {
1622
1699
  convertFootnotes(doclingDoc: DoclingDocument): ProcessedFootnote[];
1623
1700
  }
1624
1701
 
1625
- export { BaseLLMComponent, type BaseLLMComponentOptions, BaseValidator, type BaseValidatorOptions, CONTINUATION_MARKERS, CaptionParseError, CaptionParser, type CaptionParserOptions, CaptionProcessingPipeline, type CaptionProcessingPipelineDeps, CaptionValidationError, CaptionValidator, type CaptionValidatorOptions, ChapterConverter, DocumentProcessor, type DocumentProcessorOptions, type DocumentProcessorProcessOptions, type ImageContent, PAGE_NUMBER_PATTERN, PagePattern, PageRangeParseError, PageRangeParser, type PageSizeGroup, ResourceConverter, TOC_KEYWORDS, TextLLMComponent, type TocAreaResult, type TocContentValidationResult, TocContentValidationSchema, TocContentValidator, type TocContentValidatorOptions, type TocEntry, TocEntrySchema, TocExtractError, TocExtractionPipeline, type TocExtractionPipelineDeps, TocExtractor, type TocExtractorOptions, TocFinder, type TocFinderOptions, TocNotFoundError, TocParseError, type TocResponse, TocResponseSchema, VisionLLMComponent, type VisionLLMComponentOptions, type VisionTocExtractionResult, VisionTocExtractionSchema, VisionTocExtractor, type VisionTocExtractorOptions, extractMaxPageNumber };
1702
+ export { BaseLLMComponent, type BaseLLMComponentOptions, BaseValidator, type BaseValidatorOptions, CONTINUATION_MARKERS, CaptionParseError, CaptionParser, type CaptionParserOptions, CaptionProcessingPipeline, type CaptionProcessingPipelineDeps, type CaptionSourceExtraction, CaptionValidationError, CaptionValidator, type CaptionValidatorOptions, ChapterConverter, DocumentProcessor, type DocumentProcessorOptions, type DocumentProcessorProcessOptions, type ImageContent, PAGE_NUMBER_PATTERN, PROCESSED_DOCUMENT_SCHEMA_VERSION, PagePattern, PageRangeParseError, PageRangeParser, type PageSizeGroup, ResourceConverter, type SourceRefValidationMode, TOC_KEYWORDS, TextLLMComponent, type TocAreaResult, type TocContentValidationResult, TocContentValidationSchema, TocContentValidator, type TocContentValidatorOptions, type TocEntry, TocEntrySchema, TocExtractError, TocExtractionPipeline, type TocExtractionPipelineDeps, TocExtractor, type TocExtractorOptions, TocFinder, type TocFinderOptions, TocNotFoundError, TocParseError, type TocResponse, TocResponseSchema, VisionLLMComponent, type VisionLLMComponentOptions, type VisionTocExtractionResult, VisionTocExtractionSchema, VisionTocExtractor, type VisionTocExtractorOptions, extractMaxPageNumber };