@absolutejs/documents 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/LICENSE +88 -0
  3. package/README.md +41 -2
  4. package/changelog.json +20 -0
  5. package/dist/archive/containers.d.ts +5 -0
  6. package/dist/binary.d.ts +13 -0
  7. package/dist/define.d.ts +6 -0
  8. package/dist/email/body.d.ts +20 -0
  9. package/dist/email/headers.d.ts +20 -0
  10. package/dist/email/mailboxContainer.d.ts +19 -0
  11. package/dist/email/mailboxMetadata.d.ts +25 -0
  12. package/dist/email/maildir.d.ts +29 -0
  13. package/dist/email/mbox.d.ts +5 -0
  14. package/dist/email/message.d.ts +21 -0
  15. package/dist/email/mime.d.ts +24 -0
  16. package/dist/email/threads.d.ts +2 -0
  17. package/dist/epub.d.ts +2 -0
  18. package/dist/extractors/archive.d.ts +5 -0
  19. package/dist/extractors/email.d.ts +2 -0
  20. package/dist/extractors/epub.d.ts +2 -0
  21. package/dist/extractors/image.d.ts +2 -0
  22. package/dist/extractors/legacy.d.ts +2 -0
  23. package/dist/extractors/mailbox.d.ts +2 -0
  24. package/dist/extractors/media.d.ts +2 -0
  25. package/dist/extractors/office.d.ts +2 -0
  26. package/dist/extractors/pdf.d.ts +2 -0
  27. package/dist/extractors/pdfOCR.d.ts +2 -0
  28. package/dist/extractors/supports.d.ts +11 -0
  29. package/dist/extractors/text.d.ts +2 -0
  30. package/dist/formats.d.ts +37 -0
  31. package/dist/helpers.d.ts +11 -0
  32. package/dist/helpers.js +39 -0
  33. package/dist/html.d.ts +4 -0
  34. package/dist/identity.d.ts +3 -0
  35. package/dist/index-6cd9rbkj.js +3677 -0
  36. package/dist/index.d.ts +19 -0
  37. package/dist/index.js +1126 -0
  38. package/dist/legacy.d.ts +3 -0
  39. package/dist/media.d.ts +22 -0
  40. package/dist/ocr.d.ts +22 -0
  41. package/dist/office/presentation.d.ts +10 -0
  42. package/dist/office/spreadsheet.d.ts +26 -0
  43. package/dist/office/word.d.ts +20 -0
  44. package/dist/pdf/bounded.d.ts +33 -0
  45. package/dist/pdf/checkpoint.d.ts +28 -0
  46. package/dist/pdf/native.d.ts +39 -0
  47. package/dist/pdf/paged.d.ts +17 -0
  48. package/dist/read.d.ts +25 -0
  49. package/dist/registry.d.ts +5 -0
  50. package/dist/structured.d.ts +2 -0
  51. package/dist/text.d.ts +1 -0
  52. package/dist/types.d.ts +123 -0
  53. package/package.json +55 -4
@@ -0,0 +1,3 @@
1
+ export declare const stripRTF: (value: string) => string;
2
+ export declare const extractOrderedPrintableStrings: (data: Uint8Array) => string;
3
+ export declare const extractPrintableStrings: (data: Uint8Array) => string;
@@ -0,0 +1,22 @@
1
+ import { type TranscriptionResult } from "./types";
2
+ export declare const formatMediaTimestampForIngest: (value: number | undefined) => string | undefined;
3
+ export declare const normalizeMediaSpeaker: (value: unknown) => string | undefined;
4
+ export type TranscriptSegmentWithText = NonNullable<TranscriptionResult["segments"]>[number];
5
+ type MediaSegmentGroup = {
6
+ channel?: string;
7
+ endMs?: number;
8
+ segments: TranscriptSegmentWithText[];
9
+ speaker?: string;
10
+ speakerKey?: string;
11
+ startMs?: number;
12
+ };
13
+ export declare const buildMediaTimestampBoundary: (segments: MediaSegmentGroup["segments"]) => {
14
+ endMs: number | undefined;
15
+ startMs: number | undefined;
16
+ };
17
+ export declare const buildMediaSegmentGapFromPrevious: (startMs: number | undefined, previousGroupEndMs: number | undefined) => number | undefined;
18
+ export declare const buildMediaSegmentGapToNext: (endMs: number | undefined, nextGroupStartMs: number | undefined) => number | undefined;
19
+ export declare const normalizeMediaSegment: (segment: NonNullable<TranscriptionResult["segments"]>[number]) => TranscriptSegmentWithText;
20
+ export declare const sortMediaTranscriptSegments: (segments: TranscriptSegmentWithText[]) => import("./types").TranscriptSegment[];
21
+ export declare const groupTranscriptSegments: (segments: TranscriptSegmentWithText[]) => MediaSegmentGroup[];
22
+ export {};
package/dist/ocr.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { type ExtractedDocument, type ExtractionInput, type OCRResult } from "./types";
2
+ export declare const ocrMetadata: (result: OCRResult) => {
3
+ ocrMaxConfidence?: number | undefined;
4
+ ocrMinConfidence?: number | undefined;
5
+ ocrPageNumbers: number[];
6
+ ocrRegionCount: number | undefined;
7
+ ocrRegions: import("./types").OCRRegion[] | undefined;
8
+ ocrAverageConfidence: number | undefined;
9
+ ocrPageEnd?: number | undefined;
10
+ ocrPageStart?: number | undefined;
11
+ ocrConfidence: number | undefined;
12
+ ocrPageCount: number;
13
+ };
14
+ export declare const buildOCRSummaryText: (result: OCRResult) => {
15
+ lowConfidenceRegionCount: number;
16
+ strongRegionCount: number;
17
+ summaryConfidenceThreshold: number;
18
+ text: string;
19
+ usedStrongRegionsOnly: boolean;
20
+ };
21
+ export declare const ocrPageDocuments: (result: OCRResult, input: ExtractionInput, baseMetadata: Record<string, unknown>) => ExtractedDocument[];
22
+ export declare const ocrRegionDocuments: (result: OCRResult, input: ExtractionInput, baseMetadata: Record<string, unknown>) => ExtractedDocument[];
@@ -0,0 +1,10 @@
1
+ import { type ArchiveEntry } from "../types";
2
+ export declare const presentationSlides: (entries: ArchiveEntry[]) => {
3
+ index: number;
4
+ slideBodyText: string;
5
+ slideTitle: string | undefined;
6
+ notesText: string | undefined;
7
+ text: string;
8
+ }[];
9
+ export declare const presentationText: (entries: ArchiveEntry[]) => string;
10
+ export declare const presentationSlideCount: (entries: ArchiveEntry[]) => number;
@@ -0,0 +1,26 @@
1
+ import { type ArchiveEntry } from "../types";
2
+ export type SpreadsheetTableColumnRange = {
3
+ tableIndex: number;
4
+ spreadsheetColumnStart?: string;
5
+ spreadsheetColumnEnd?: string;
6
+ };
7
+ export type SpreadsheetTableHeaders = {
8
+ tableIndex: number;
9
+ spreadsheetHeaders: string[];
10
+ };
11
+ type SpreadsheetSheetText = {
12
+ headers: string[];
13
+ name: string;
14
+ repeatedHeaderRowNumbers: number[];
15
+ rowCount: number;
16
+ sheetTableHeaders: SpreadsheetTableHeaders[];
17
+ sheetTableColumnRanges: SpreadsheetTableColumnRange[];
18
+ spreadsheetColumnEnd?: string;
19
+ spreadsheetColumnStart?: string;
20
+ tableCount: number;
21
+ text: string;
22
+ };
23
+ export declare const spreadsheetSheetTexts: (entries: ArchiveEntry[]) => SpreadsheetSheetText[];
24
+ export declare const spreadsheetText: (entries: ArchiveEntry[]) => string;
25
+ export declare const spreadsheetSheetNames: (entries: ArchiveEntry[]) => string[];
26
+ export {};
@@ -0,0 +1,20 @@
1
+ import { type ArchiveEntry } from "../types";
2
+ type OfficeDocumentBlock = {
3
+ blockNumber: number;
4
+ blockKind: "title" | "heading" | "paragraph" | "list" | "table";
5
+ text: string;
6
+ style?: string;
7
+ headingLevel?: number;
8
+ listLevel?: number;
9
+ tableBodyRowCount?: number;
10
+ tableColumnCount?: number;
11
+ tableHeaderText?: string;
12
+ tableHeaders?: string[];
13
+ tableRowCount?: number;
14
+ tableSignature?: string;
15
+ };
16
+ export declare const extractXmlText: (value: string) => string;
17
+ export declare const officeDocumentBlocks: (entries: ArchiveEntry[]) => OfficeDocumentBlock[];
18
+ export declare const officeDocumentText: (entries: ArchiveEntry[]) => string;
19
+ export declare const officeDocumentSectionCount: (entries: ArchiveEntry[]) => number | undefined;
20
+ export {};
@@ -0,0 +1,33 @@
1
+ /** PDF parsing runs in killable Linux processes with no application credentials.
2
+ * Poppler and util-linux prlimit must be installed by the host. No unsafe fallback. */
3
+ export type BoundedPDFOptions = {
4
+ signal?: AbortSignal;
5
+ maxFileBytes?: number;
6
+ maxPages?: number;
7
+ maxOutputBytes?: number;
8
+ memoryBytes?: number;
9
+ timeoutMs?: number;
10
+ executableDirectory?: string;
11
+ };
12
+ export declare class PDFSafetyError extends Error {
13
+ readonly code: "size" | "pages" | "password" | "invalid" | "resources" | "output";
14
+ diagnostics?: {
15
+ tool: string;
16
+ exitCode?: number;
17
+ elapsedMs: number;
18
+ timedOut: boolean;
19
+ memoryBytes: number;
20
+ maxOutputBytes: number;
21
+ };
22
+ constructor(code: "size" | "pages" | "password" | "invalid" | "resources" | "output", message: string);
23
+ }
24
+ export declare const inspectBoundedPDF: (path: string, options?: BoundedPDFOptions) => Promise<{
25
+ pages: number;
26
+ imagePages: number[];
27
+ readPage: (page: number) => Promise<{
28
+ page: number;
29
+ text: string;
30
+ needsOCR: boolean;
31
+ }>;
32
+ renderPage: (page: number) => Promise<Uint8Array<ArrayBuffer>>;
33
+ }>;
@@ -0,0 +1,28 @@
1
+ import { z } from "zod";
2
+ export declare const pdfCheckpointSchema: z.ZodObject<{
3
+ version: z.ZodLiteral<1>;
4
+ fingerprint: z.ZodString;
5
+ pageCount: z.ZodNumber;
6
+ pages: z.ZodArray<z.ZodObject<{
7
+ page: z.ZodNumber;
8
+ text: z.ZodString;
9
+ mode: z.ZodEnum<{
10
+ native: "native";
11
+ ocr: "ocr";
12
+ blank: "blank";
13
+ }>;
14
+ hybrid: z.ZodBoolean;
15
+ }, z.core.$strip>>;
16
+ }, z.core.$strip>;
17
+ export type PDFCheckpoint = z.infer<typeof pdfCheckpointSchema>;
18
+ export type PDFCheckpointStore = {
19
+ load: (fingerprint: string) => Promise<unknown>;
20
+ save: (checkpoint: PDFCheckpoint) => Promise<void>;
21
+ };
22
+ /** A failed extraction remains a failure, but completed evidence is recoverable. */
23
+ export declare class PDFIncompleteError extends Error {
24
+ readonly checkpoint: PDFCheckpoint;
25
+ readonly failedPage: number;
26
+ readonly nativeText: string;
27
+ constructor(checkpoint: PDFCheckpoint, failedPage: number, nativeText: string, cause: unknown);
28
+ }
@@ -0,0 +1,39 @@
1
+ type PDFNativeTextBlock = {
2
+ blockNumber: number;
3
+ lineCount: number;
4
+ pageNumber: number;
5
+ semanticRole?: "figure_caption" | "figure_body";
6
+ text: string;
7
+ textKind: "paragraph" | "table_like";
8
+ };
9
+ export type PDFNativeStructureBlockEntry = {
10
+ baseSectionTitle: string;
11
+ pageNumber?: number;
12
+ pdfBlockNumber?: number;
13
+ pdfFigureCaptionBlockNumber?: number;
14
+ pdfFigureLabel?: string;
15
+ pdfSemanticRole?: "figure_caption" | "figure_body";
16
+ pdfTableBodyRowEnd?: number;
17
+ pdfTableBodyRowCount?: number;
18
+ pdfTableBodyRowStart?: number;
19
+ pdfTableChunkKind?: "full_table" | "table_slice";
20
+ pdfTableColumnCount?: number;
21
+ pdfTableHeaderText?: string;
22
+ pdfTableHeaders?: string[];
23
+ pdfTableRowCount?: number;
24
+ pdfTableSignature?: string;
25
+ pdfTextKind: "paragraph" | "table_like";
26
+ text: string;
27
+ };
28
+ type PDFNativeTextExtraction = {
29
+ pageCount: number;
30
+ text: string;
31
+ textBlockCount: number;
32
+ textBlocks: PDFNativeTextBlock[];
33
+ };
34
+ export declare const OCR_SUMMARY_CONFIDENCE_THRESHOLD = 0.75;
35
+ export declare const OCR_SUMMARY_MIN_STRONG_TEXT_RATIO = 0.6;
36
+ export declare const inferPDFBlockHeading: (text: string) => string | undefined;
37
+ export declare const extractPDFFigureLabel: (text: string) => string | undefined;
38
+ export declare const extractNativePDFText: (data: Uint8Array) => Promise<PDFNativeTextExtraction>;
39
+ export {};
@@ -0,0 +1,17 @@
1
+ import { type PDFCheckpointStore } from "./checkpoint";
2
+ import type { FileExtractor, OCRProvider } from "../types";
3
+ export type PagedPDFExtractorOptions = {
4
+ provider: OCRProvider;
5
+ /** Scope the store to the caller. Provider name must change when OCR semantics change. */
6
+ checkpoints?: PDFCheckpointStore;
7
+ signal?: AbortSignal;
8
+ onProgress?: (progress: {
9
+ page: number;
10
+ pageCount: number;
11
+ mode: "native" | "ocr" | "blank";
12
+ }) => void;
13
+ };
14
+ /** Preserve page evidence and OCR image-bearing pages individually, including
15
+ * scans inside otherwise readable PDFs. The provider must reject truncated OCR.
16
+ * Nothing is returned until every page has been processed successfully. */
17
+ export declare const createPagedPDFExtractor: (options: PagedPDFExtractorOptions) => FileExtractor;
package/dist/read.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ import type { DocumentContent, DocumentUpload, ExtractionInput, ExtractorRegistryLike, FileExtractor } from "./types";
2
+ /** Which extractors to try, beyond (or instead of) the built-in ones. */
3
+ export type ReadOptions = {
4
+ extractors?: FileExtractor[];
5
+ registry?: ExtractorRegistryLike;
6
+ };
7
+ /** Every document a file holds: one for most files, one per message for a
8
+ * mailbox, one per entry for an archive. */
9
+ export declare const readDocuments: (input: ExtractionInput, options?: ReadOptions) => Promise<DocumentContent[]>;
10
+ /** The document a file holds (the first, for a file that holds several). */
11
+ export declare const readDocument: (input: ExtractionInput, options?: ReadOptions) => Promise<DocumentContent>;
12
+ /** Read a file from disk. */
13
+ export declare const readDocumentFile: (path: string, input?: Omit<ExtractionInput, "data" | "path">, options?: ReadOptions) => Promise<DocumentContent>;
14
+ /** Read uploaded files, linking replies across them into email threads. */
15
+ export declare const readDocumentUploads: (uploads: DocumentUpload[], options?: ReadOptions) => Promise<DocumentContent[]>;
16
+ /** A document reader for @absolutejs/search: pass it as `readDocument` and
17
+ * PDFs, Office files and the rest behind a URL are read too. */
18
+ export declare const readWebDocument: (input: {
19
+ body: Uint8Array;
20
+ contentType: string;
21
+ url: string;
22
+ }) => Promise<{
23
+ text: string;
24
+ title: string | null;
25
+ }>;
@@ -0,0 +1,5 @@
1
+ import { type DocumentContent, type ExtractionInput, type ExtractorRegistryLike, type FileExtractor } from "./types";
2
+ export declare const DEFAULT_FILE_EXTRACTORS: FileExtractor[];
3
+ export declare const extractDocuments: (input: ExtractionInput, extractors?: FileExtractor[], registry?: ExtractorRegistryLike) => Promise<DocumentContent[]>;
4
+ export declare const getFirstExtractedDocument: (documents: DocumentContent[], label: string) => DocumentContent;
5
+ export declare const loadExtractedDocuments: (input: ExtractionInput, extractors?: FileExtractor[], registry?: ExtractorRegistryLike) => Promise<DocumentContent[]>;
@@ -0,0 +1,2 @@
1
+ import { type DocumentFormat } from "./types";
2
+ export declare const validateStructuredTextInput: (value: string, format: DocumentFormat) => void;
package/dist/text.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const normalizeWhitespace: (value: string) => string;
@@ -0,0 +1,123 @@
1
+ /** The text formats a document can be read as. */
2
+ export type DocumentFormat = "text" | "markdown" | "html" | "jsonl" | "tsv" | "csv" | "xml" | "yaml";
3
+ /** Text read from a file, with where it came from. */
4
+ export type DocumentContent = {
5
+ text: string;
6
+ id?: string;
7
+ title?: string;
8
+ source?: string;
9
+ format?: DocumentFormat;
10
+ metadata?: Record<string, unknown>;
11
+ };
12
+ /** A file to read: its bytes, and whatever is known about it. */
13
+ export type ExtractionInput = {
14
+ data: Uint8Array;
15
+ path?: string;
16
+ name?: string;
17
+ source?: string;
18
+ title?: string;
19
+ format?: DocumentFormat;
20
+ contentType?: string;
21
+ metadata?: Record<string, unknown>;
22
+ extractorRegistry?: ExtractorRegistryLike;
23
+ };
24
+ export type ExtractedDocument = DocumentContent & {
25
+ contentType?: string;
26
+ extractor?: string;
27
+ };
28
+ export type FileExtractor = {
29
+ name: string;
30
+ supports: (input: ExtractionInput) => boolean | Promise<boolean>;
31
+ extract: (input: ExtractionInput) => ExtractedDocument | ExtractedDocument[] | Promise<ExtractedDocument | ExtractedDocument[]>;
32
+ };
33
+ export type ExtractorRegistryInput = ExtractionInput & {
34
+ inferredContentType?: string | null;
35
+ inferredExtension?: string | null;
36
+ inferredFormat?: DocumentFormat;
37
+ };
38
+ export type ExtractorRegistration = {
39
+ extractor: FileExtractor;
40
+ name?: string;
41
+ priority?: number;
42
+ contentTypes?: string[];
43
+ extensions?: string[];
44
+ formats?: DocumentFormat[];
45
+ names?: string[];
46
+ match?: (input: ExtractorRegistryInput) => boolean | Promise<boolean>;
47
+ };
48
+ export type ExtractorRegistry = {
49
+ registrations: ExtractorRegistration[];
50
+ includeDefaults?: boolean;
51
+ defaultOrder?: "registry_first" | "defaults_first";
52
+ };
53
+ export type ExtractorRegistryLike = ExtractorRegistry | ExtractorRegistration[];
54
+ export type TranscriptSegment = {
55
+ text: string;
56
+ startMs?: number;
57
+ endMs?: number;
58
+ speaker?: string;
59
+ channel?: string;
60
+ };
61
+ export type TranscriptionResult = {
62
+ text: string;
63
+ title?: string;
64
+ metadata?: Record<string, unknown>;
65
+ segments?: TranscriptSegment[];
66
+ };
67
+ /** Turns audio or video into text; the host supplies the model. */
68
+ export type MediaTranscriber = {
69
+ name: string;
70
+ transcribe: (input: ExtractionInput) => TranscriptionResult | Promise<TranscriptionResult>;
71
+ };
72
+ export type OCRRegion = {
73
+ text: string;
74
+ confidence?: number;
75
+ page?: number;
76
+ x?: number;
77
+ y?: number;
78
+ width?: number;
79
+ height?: number;
80
+ };
81
+ export type OCRResult = {
82
+ text: string;
83
+ title?: string;
84
+ metadata?: Record<string, unknown>;
85
+ confidence?: number;
86
+ regions?: OCRRegion[];
87
+ };
88
+ /** Reads text from images; the host supplies the model or service. */
89
+ export type OCRProvider = {
90
+ name: string;
91
+ extractText: (input: ExtractionInput) => OCRResult | Promise<OCRResult>;
92
+ };
93
+ export type PDFOCRExtractorOptions = {
94
+ provider: OCRProvider;
95
+ alwaysOCR?: boolean;
96
+ minExtractedTextLength?: number;
97
+ };
98
+ export type ArchiveEntry = {
99
+ data: Uint8Array;
100
+ path: string;
101
+ contentType?: string;
102
+ format?: DocumentFormat;
103
+ metadata?: Record<string, unknown>;
104
+ };
105
+ export type ArchiveExpansionResult = {
106
+ entries: ArchiveEntry[];
107
+ metadata?: Record<string, unknown>;
108
+ };
109
+ export type ArchiveExpander = {
110
+ name: string;
111
+ expand: (input: ExtractionInput) => ArchiveExpansionResult | Promise<ArchiveExpansionResult>;
112
+ };
113
+ /** A file sent as text: base64 or UTF-8 content with its name. */
114
+ export type DocumentUpload = {
115
+ name: string;
116
+ content: string;
117
+ contentType?: string;
118
+ encoding?: "base64" | "utf8";
119
+ format?: DocumentFormat;
120
+ source?: string;
121
+ title?: string;
122
+ metadata?: Record<string, unknown>;
123
+ };
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "@absolutejs/documents",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Read files into text: PDF, Office, EPUB, email and mailboxes, archives, plus pluggable OCR and transcription",
6
+ "license": "BSL-1.1",
7
+ "author": "Alex Kahn",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/absolutejs/documents.git"
11
+ },
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md",
18
+ "LICENSE",
19
+ "CHANGELOG.md",
20
+ "changelog.json"
21
+ ],
22
+ "main": "./dist/index.js",
23
+ "types": "./dist/index.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "default": "./dist/index.js"
28
+ },
29
+ "./helpers": {
30
+ "types": "./dist/helpers.d.ts",
31
+ "default": "./dist/helpers.js"
32
+ }
33
+ },
34
+ "scripts": {
35
+ "build": "rm -rf dist && bun build src/index.ts src/helpers.ts --outdir dist --root src --target bun --splitting --external pdfjs-dist --external pdf-lib --external zod && tsc --project tsconfig.build.json",
36
+ "test": "bun test",
37
+ "typecheck": "tsc --noEmit",
38
+ "lint": "eslint . --max-warnings 0",
39
+ "check:package": "absolute-changelog check",
40
+ "prepublishOnly": "bun run check:package"
41
+ },
42
+ "dependencies": {
43
+ "pdf-lib": "1.17.1",
44
+ "pdfjs-dist": "6.3.289",
45
+ "zod": "^4.0.0"
46
+ },
47
+ "devDependencies": {
48
+ "@absolutejs/changelog": "^0.6.0",
49
+ "@types/bun": "1.3.14",
50
+ "eslint": "^10.0.3",
51
+ "eslint-plugin-absolute": "^0.2.6",
52
+ "eslint-plugin-promise": "^7.2.1",
53
+ "globals": "^17.4.0",
54
+ "typescript": "5.9.3",
55
+ "typescript-eslint": "^8.56.1"
56
+ }
57
+ }