xberg 1.1.0

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 107 formats and 371 programming languages via tree-sitter code intelligence with async/sync APIs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Structured-extraction call-mode heuristic.
//!
//! Decides whether a document should enter the structured-extraction pipeline
//! as text-only, vision-only, text+vision — or be skipped entirely.  This is
//! the chokepoint that keeps LLM calls minimal: pure-text invoices never see
//! the vision model.
//!
//! # Modules at a glance
//!
//! | Item | Description |
//! |------|-------------|
//! | [`StructuredCallMode`] | Runtime outcome enum (5 variants) |
//! | [`StructuredInput`] | Plain DTO; signals derived from a prior extraction |
//! | [`StructuredThresholds`] | Tunable thresholds with conservative defaults |
//! | [`choose_call_mode`] | Pure decision function |

use serde::{Deserialize, Serialize};

/// Outcome of the structured-extraction call-mode heuristic.
///
/// **Distinct from `crate::core::config::CallMode`** which has three variants
/// and governs extraction-engine behaviour.  This enum governs whether and how
/// an already-extracted document is sent to an LLM structured-extraction
/// pipeline.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "api", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum StructuredCallMode {
    /// Document is unsupported or not worth invoking the pipeline.
    Skip,
    /// Send extracted text only; no vision model call.
    TextOnly,
    /// Send page rasters only; no extracted text payload.
    VisionOnly,
    /// Fuse extracted text with page rasters in a single multimodal call.
    TextPlusVision,
    /// Try text-only first; escalate to vision on low confidence score.
    TextOnlyWithVisionFallback,
}

/// Signals available to structured call-mode policies.
///
/// All fields derive from a prior xberg extraction — no double-work.
/// This is a plain DTO; it intentionally has no dependency on internal
/// xberg extraction types so it can be constructed from any source.
/// The built-in [`choose_call_mode`] policy intentionally uses only the MIME
/// type, average characters per page, and explicit vision override. Custom
/// [`StructuredPolicy`](crate::engine::seams::StructuredPolicy)
/// implementations may use the remaining signals.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StructuredInput {
    /// MIME type, canonicalised to lowercase by the caller.
    pub mime_type: String,
    /// Number of pages available to custom policies.
    pub page_count: u32,
    /// Fraction of pages with a real text layer (0.0..=1.0), available to custom policies.
    pub text_coverage: f64,
    /// Average extracted characters per page.
    pub avg_chars_per_page: f64,
    /// Count of embedded images available to custom policies.
    pub embedded_image_count: u32,
    /// When `true`, promote the result to at least [`StructuredCallMode::TextPlusVision`].
    pub user_force_vision: bool,
}

/// Thresholds for the structured-extraction call-mode heuristic.
///
/// All defaults are **conservative starting points**.  Deployments should
/// measure their own document corpus and override via their own config;
/// these values are chosen to be safe-by-default, not to be optimal for
/// any particular workload.
///
/// Construct custom thresholds with struct-update syntax:
/// ```rust
/// use xberg::heuristics::StructuredThresholds;
/// let t = StructuredThresholds {
///     enable_vision_fallback: true,
///     ..StructuredThresholds::default()
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct StructuredThresholds {
    /// Scan-coverage boundary available to custom policies.
    ///
    /// The built-in policy intentionally starts PDFs text-first and does not use this value.
    pub scan_max_coverage: f64,
    /// Digital-PDF coverage boundary available to custom policies.
    ///
    /// The built-in policy intentionally starts PDFs text-first and does not use this value.
    pub digital_min_coverage: f64,
    /// DOCX / HTML / text documents with `avg_chars_per_page` above this
    /// route to [`StructuredCallMode::TextOnly`].
    ///
    /// **Conservative default: 200.0** — deployments override via their own
    /// config after measuring their document corpus.
    pub docx_text_min_density: f64,
    /// When `true`, emit [`StructuredCallMode::TextOnlyWithVisionFallback`]
    /// instead of [`StructuredCallMode::TextOnly`] so the orchestrator can
    /// escalate to vision on low confidence.
    ///
    /// **Conservative default: `false`** — must be explicitly enabled per
    /// deployment after bench validation; deployments override via their own
    /// config.
    pub enable_vision_fallback: bool,
}

impl Default for StructuredThresholds {
    fn default() -> Self {
        Self {
            scan_max_coverage: 0.10,
            digital_min_coverage: 0.90,
            docx_text_min_density: 200.0,
            enable_vision_fallback: false,
        }
    }
}

/// Decide which call mode best fits this document.
///
/// Rules applied in order:
///
/// 1. `image/*` → [`StructuredCallMode::VisionOnly`] (no text layer to start from).
/// 2. `application/pdf` → [`StructuredCallMode::TextOnly`] regardless of
///    `text_coverage` or embedded image count.  Xberg's OCR + text-layer
///    extraction produces text for scanned PDFs; the orchestrator's
///    post-call confidence gate handles any vision escalation actually needed.
/// 3. DOCX / `text/html` / `text/*` / `application/json` / `application/xml` /
///    `application/rtf` with `avg_chars_per_page > docx_text_min_density`
///    → [`StructuredCallMode::TextOnly`].
/// 4. Anything else → [`StructuredCallMode::Skip`].
///
/// After rule selection two post-rule promotions apply (in order):
///
/// - `user_force_vision` promotes `TextOnly` → `TextPlusVision`
///   (`Skip` stays `Skip` — caller meant to opt out).
/// - `enable_vision_fallback` promotes `TextOnly` →
///   `TextOnlyWithVisionFallback` (does **not** upgrade `TextPlusVision` or
///   `Skip`).
pub fn choose_call_mode(input: &StructuredInput, t: &StructuredThresholds) -> StructuredCallMode {
    let mime = input.mime_type.to_ascii_lowercase();
    let is_text_mime = mime.starts_with("text/")
        || mime == "application/json"
        || mime == "application/xml"
        || mime == "application/rtf";
    let is_text_bearing = mime == "application/pdf"
        || (is_docx_or_html(&mime) && input.avg_chars_per_page > t.docx_text_min_density)
        || (is_text_mime && input.avg_chars_per_page > t.docx_text_min_density);

    let raw = if mime.starts_with("image/") {
        StructuredCallMode::VisionOnly
    } else if is_text_bearing {
        StructuredCallMode::TextOnly
    } else {
        StructuredCallMode::Skip
    };

    let mode = if input.user_force_vision {
        match raw {
            StructuredCallMode::TextOnly => StructuredCallMode::TextPlusVision,
            other => other,
        }
    } else {
        raw
    };

    if t.enable_vision_fallback && mode == StructuredCallMode::TextOnly {
        StructuredCallMode::TextOnlyWithVisionFallback
    } else {
        mode
    }
}

fn is_docx_or_html(mime: &str) -> bool {
    matches!(
        mime,
        "text/html" | "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    fn t() -> StructuredThresholds {
        StructuredThresholds::default()
    }

    fn input(mime: &str) -> StructuredInput {
        StructuredInput {
            mime_type: mime.into(),
            page_count: 1,
            text_coverage: 0.0,
            avg_chars_per_page: 0.0,
            embedded_image_count: 0,
            user_force_vision: false,
        }
    }

    #[test]
    fn image_mime_chooses_vision_only() {
        assert_eq!(
            choose_call_mode(&input("image/png"), &t()),
            StructuredCallMode::VisionOnly
        );
        assert_eq!(
            choose_call_mode(&input("image/jpeg"), &t()),
            StructuredCallMode::VisionOnly
        );
    }

    #[test]
    fn pdf_low_coverage_chooses_text_only() {
        let mut i = input("application/pdf");
        i.text_coverage = 0.05;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::TextOnly);
    }

    #[test]
    fn pdf_high_coverage_pure_text_chooses_text_only() {
        let mut i = input("application/pdf");
        i.text_coverage = 0.95;
        i.embedded_image_count = 0;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::TextOnly);
    }

    #[test]
    fn pdf_high_coverage_with_images_chooses_text_only() {
        let mut i = input("application/pdf");
        i.text_coverage = 0.95;
        i.embedded_image_count = 3;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::TextOnly);
    }

    #[test]
    fn pdf_mid_coverage_chooses_text_only() {
        let mut i = input("application/pdf");
        i.text_coverage = 0.5;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::TextOnly);
    }

    #[test]
    fn custom_policy_pdf_signals_do_not_change_builtin_policy() {
        let mut low = input("application/pdf");
        low.page_count = 1;
        low.text_coverage = 0.0;
        low.embedded_image_count = 0;
        let mut high = input("application/pdf");
        high.page_count = u32::MAX;
        high.text_coverage = 1.0;
        high.embedded_image_count = u32::MAX;

        let low_thresholds = StructuredThresholds {
            scan_max_coverage: 0.0,
            digital_min_coverage: 0.0,
            ..StructuredThresholds::default()
        };
        let high_thresholds = StructuredThresholds {
            scan_max_coverage: 1.0,
            digital_min_coverage: 1.0,
            ..StructuredThresholds::default()
        };

        assert_eq!(choose_call_mode(&low, &low_thresholds), StructuredCallMode::TextOnly);
        assert_eq!(choose_call_mode(&high, &high_thresholds), StructuredCallMode::TextOnly);
    }

    #[test]
    fn docx_dense_text_chooses_text_only() {
        let mut i = input("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        i.avg_chars_per_page = 800.0;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::TextOnly);
    }

    #[test]
    fn html_dense_text_chooses_text_only() {
        let mut i = input("text/html");
        i.avg_chars_per_page = 500.0;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::TextOnly);
    }

    #[test]
    fn html_sparse_text_chooses_skip() {
        let mut i = input("text/html");
        i.avg_chars_per_page = 50.0;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::Skip);
    }

    #[test]
    fn text_plain_dense_chooses_text_only() {
        let mut i = input("text/plain");
        i.avg_chars_per_page = 500.0;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::TextOnly);
    }

    #[test]
    fn text_plain_sparse_chooses_skip() {
        let mut i = input("text/plain");
        i.avg_chars_per_page = 50.0;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::Skip);
    }

    #[test]
    fn text_csv_dense_chooses_text_only() {
        let mut i = input("text/csv");
        i.avg_chars_per_page = 500.0;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::TextOnly);
    }

    #[test]
    fn application_json_dense_chooses_text_only() {
        let mut i = input("application/json");
        i.avg_chars_per_page = 500.0;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::TextOnly);
    }

    #[test]
    fn application_xml_dense_chooses_text_only() {
        let mut i = input("application/xml");
        i.avg_chars_per_page = 500.0;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::TextOnly);
    }

    #[test]
    fn application_rtf_sparse_chooses_skip() {
        let mut i = input("application/rtf");
        i.avg_chars_per_page = 50.0;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::Skip);
    }

    #[test]
    fn unsupported_mime_chooses_skip() {
        assert_eq!(
            choose_call_mode(&input("application/octet-stream"), &t()),
            StructuredCallMode::Skip
        );
    }

    #[test]
    fn user_force_vision_promotes_text_only_to_text_plus_vision() {
        let mut i = input("application/pdf");
        i.text_coverage = 0.95;
        i.user_force_vision = true;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::TextPlusVision);
    }

    #[test]
    fn user_force_vision_does_not_promote_skip() {
        let mut i = input("application/octet-stream");
        i.user_force_vision = true;
        assert_eq!(choose_call_mode(&i, &t()), StructuredCallMode::Skip);
    }

    #[test]
    fn case_insensitive_mime_match() {
        assert_eq!(
            choose_call_mode(&input("IMAGE/PNG"), &t()),
            StructuredCallMode::VisionOnly
        );
    }

    #[test]
    fn enable_vision_fallback_promotes_text_only_to_fallback() {
        let mut i = input("application/pdf");
        i.text_coverage = 0.95;
        let thresholds = StructuredThresholds {
            enable_vision_fallback: true,
            ..StructuredThresholds::default()
        };
        assert_eq!(
            choose_call_mode(&i, &thresholds),
            StructuredCallMode::TextOnlyWithVisionFallback
        );
    }

    #[test]
    fn enable_vision_fallback_does_not_upgrade_text_plus_vision() {
        let mut i = input("application/pdf");
        i.user_force_vision = true;
        let thresholds = StructuredThresholds {
            enable_vision_fallback: true,
            ..StructuredThresholds::default()
        };
        assert_eq!(choose_call_mode(&i, &thresholds), StructuredCallMode::TextPlusVision);
    }

    #[test]
    fn serde_round_trip_all_variants() {
        let variants = [
            StructuredCallMode::Skip,
            StructuredCallMode::TextOnly,
            StructuredCallMode::VisionOnly,
            StructuredCallMode::TextPlusVision,
            StructuredCallMode::TextOnlyWithVisionFallback,
        ];
        for variant in variants {
            let json = serde_json::to_string(&variant).expect("serialize");
            let decoded: StructuredCallMode = serde_json::from_str(&json).expect("deserialize");
            assert_eq!(decoded, variant, "round-trip failed for {:?}", variant);
        }
    }

    #[test]
    fn serde_uses_snake_case_names() {
        assert_eq!(serde_json::to_string(&StructuredCallMode::Skip).unwrap(), r#""skip""#);
        assert_eq!(
            serde_json::to_string(&StructuredCallMode::TextOnly).unwrap(),
            r#""text_only""#
        );
        assert_eq!(
            serde_json::to_string(&StructuredCallMode::VisionOnly).unwrap(),
            r#""vision_only""#
        );
        assert_eq!(
            serde_json::to_string(&StructuredCallMode::TextPlusVision).unwrap(),
            r#""text_plus_vision""#
        );
        assert_eq!(
            serde_json::to_string(&StructuredCallMode::TextOnlyWithVisionFallback).unwrap(),
            r#""text_only_with_vision_fallback""#
        );
    }
}