xberg 1.0.3

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 98 formats and 306 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
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
//! Build system + user prompts for the vision-LLM call.
//!
//! This is the generic prompt-assembly mechanism: it substitutes `{{var}}`
//! placeholders, fences untrusted content with a per-call nonce, truncates the
//! extracted-text excerpt at a caller-supplied limit, and optionally appends a
//! caller-supplied citation instruction. All policy text (system prompt, context
//! template, citation instruction) and all limits are **parameters** — no preset
//! type, no embedded instruction template, and no environment read live here.

use crate::heuristics::StructuredCallMode;

/// Build a per-call random nonce used to fence untrusted content (extracted
/// text + prior LLM JSON) inside the prompt. An attacker who can plant content
/// in the document cannot predict the nonce, so they cannot close the fence
/// and inject instructions at the same nesting level as the legitimate ones.
///
/// Randomness is drawn from `std`'s [`std::collections::hash_map::RandomState`],
/// which is seeded from OS entropy and randomized per instance, so the mechanism
/// stays dependency-light (no `uuid`/`rand`) while preserving unpredictability.
fn fence_nonce() -> String {
    use std::hash::{BuildHasher, Hasher};
    let hi = std::collections::hash_map::RandomState::new().build_hasher().finish();
    let lo = std::collections::hash_map::RandomState::new().build_hasher().finish();
    format!("{hi:016x}{lo:016x}")
}

/// Truncate `text` to at most `max_bytes` bytes, backing off to the nearest
/// UTF-8 char boundary so the slice never panics on multi-byte input.
///
/// `max_bytes` is a byte budget (it bounds the excerpt's serialized size); when
/// the cut point lands inside a multi-byte character the excerpt is shortened to
/// the preceding boundary rather than panicking.
fn truncate_to_char_boundary(text: &str, max_bytes: usize) -> &str {
    if text.len() <= max_bytes {
        return text;
    }
    let mut end = max_bytes;
    while end > 0 && !text.is_char_boundary(end) {
        end -= 1;
    }
    &text[..end]
}

/// Built prompt components ready to send to the vision model.
#[derive(Debug, Clone)]
pub struct BuiltPrompt {
    /// System instruction primer.
    pub system: String,
    /// User content (text + images context). None for VisionOnly / Skip modes.
    pub user_text: Option<String>,
}

/// Build system + user prompts from caller-supplied text and context.
///
/// - System prompt is `system_prompt` with `{{var}}` substitution from `user_context`.
/// - `citation_instruction` is appended to the system prompt when `Some`.
/// - User text includes `context_template` (if present) + the extracted excerpt,
///   truncated at `max_excerpt_bytes`.
/// - VisionOnly / Skip modes set `user_text` to None.
pub fn build_prompt(
    system_prompt: &str,
    context_template: Option<&str>,
    extracted_text_excerpt: &str,
    user_context: Option<&serde_json::Map<String, serde_json::Value>>,
    call_mode: StructuredCallMode,
    citation_instruction: Option<&str>,
    max_excerpt_bytes: usize,
) -> BuiltPrompt {
    let mut system = substitute_vars(system_prompt, user_context);

    if let Some(instruction) = citation_instruction {
        system.push_str(instruction);
    }

    let user_text = match call_mode {
        StructuredCallMode::VisionOnly => None,
        StructuredCallMode::Skip => {
            tracing::warn!("build_prompt called with Skip mode; treating as no content");
            None
        }
        StructuredCallMode::TextOnly
        | StructuredCallMode::TextPlusVision
        | StructuredCallMode::TextOnlyWithVisionFallback => {
            let mut content = String::new();

            if let Some(template) = context_template {
                let rendered = substitute_vars(template, user_context);
                content.push_str(&rendered);
            }

            let excerpt = truncate_to_char_boundary(extracted_text_excerpt, max_excerpt_bytes);

            if !content.is_empty() && !excerpt.is_empty() {
                content.push_str("\n\n---\n\n");
            }
            content.push_str(excerpt);

            Some(content)
        }
    };

    BuiltPrompt { system, user_text }
}

/// Build a vision-fallback prompt when text-only extraction was low confidence.
///
/// Composes the system prompt (with optional citation instruction), the context
/// template, the original text excerpt (fenced, truncated at `max_excerpt_bytes`),
/// the prior text-only JSON output, a confidence breakdown, and a fallback
/// instruction.
///
/// Returns the composed `user_text` (text only — images come separately in the request).
#[allow(clippy::too_many_arguments)]
pub fn build_vision_fallback_prompt(
    system_prompt: &str,
    context_template: Option<&str>,
    extracted_text_excerpt: &str,
    user_context: Option<&serde_json::Map<String, serde_json::Value>>,
    prior_json: &serde_json::Value,
    confidence: &crate::heuristics::confidence::ExtractionConfidence,
    citation_instruction: Option<&str>,
    max_excerpt_bytes: usize,
) -> BuiltPrompt {
    let mut system = substitute_vars(system_prompt, user_context);

    if let Some(instruction) = citation_instruction {
        system.push_str(instruction);
    }

    let mut content = String::new();

    if let Some(template) = context_template {
        let rendered = substitute_vars(template, user_context);
        content.push_str(&rendered);
    }

    let excerpt = truncate_to_char_boundary(extracted_text_excerpt, max_excerpt_bytes);

    let nonce = fence_nonce();

    if !excerpt.is_empty() {
        if !content.is_empty() {
            content.push_str("\n\n");
        }
        content.push_str(&format!("--- BEGIN EXTRACTED_TEXT_{nonce} ---\n"));
        content.push_str(excerpt);
        content.push_str(&format!("\n--- END EXTRACTED_TEXT_{nonce} ---\n"));
    }

    content.push_str("\nPrior text-only extraction (low confidence):\n");
    content.push_str(&format!("--- BEGIN PRIOR_JSON_{nonce} ---\n"));
    match serde_json::to_string_pretty(prior_json) {
        Ok(pretty) => content.push_str(&pretty),
        Err(_) => {
            let fallback = serde_json::json!({"error": "prior extraction unavailable"});
            if let Ok(s) = serde_json::to_string_pretty(&fallback) {
                content.push_str(&s);
            }
        }
    }
    content.push_str(&format!("\n--- END PRIOR_JSON_{nonce} ---\n\n"));

    content.push_str("Confidence breakdown of prior extraction:\n");
    content.push_str(&format!(
        "  - Text coverage: {:.2}%\n",
        confidence.text_coverage * 100.0
    ));
    if let Some(ocr) = confidence.ocr_aggregate {
        content.push_str(&format!("  - OCR confidence: {:.2}%\n", ocr * 100.0));
    }
    content.push_str(&format!("  - Schema compliance: {:?}\n", confidence.schema_compliance));
    content.push_str(&format!("  - Combined score: {:.2}%\n", confidence.combined * 100.0));

    content.push_str(
        "\nPlease review the source pages directly and produce a corrected/completed JSON output matching the schema. Focus on details the text extraction may have missed.",
    );

    BuiltPrompt {
        system,
        user_text: Some(content),
    }
}

/// Substitute {{var}} placeholders from the provided context map.
/// Missing vars leave the placeholder intact (do not error).
fn substitute_vars(template: &str, context: Option<&serde_json::Map<String, serde_json::Value>>) -> String {
    let Some(ctx) = context else {
        return template.to_string();
    };

    let mut result = String::new();
    let mut chars = template.chars().peekable();

    while let Some(ch) = chars.next() {
        if ch == '{' && chars.peek() == Some(&'{') {
            chars.next();

            let mut var_name = String::new();
            let mut found_close = false;

            while let Some(next_ch) = chars.next() {
                if next_ch == '}' && chars.peek() == Some(&'}') {
                    chars.next();
                    found_close = true;
                    break;
                }
                var_name.push(next_ch);
            }

            if found_close {
                if let Some(value) = ctx.get(&var_name) {
                    match value {
                        serde_json::Value::Null => {
                            result.push_str("{{");
                            result.push_str(&var_name);
                            result.push_str("}}");
                        }
                        serde_json::Value::Bool(b) => result.push_str(&b.to_string()),
                        serde_json::Value::Number(n) => result.push_str(&n.to_string()),
                        serde_json::Value::String(s) => result.push_str(s),
                        _ => {
                            if let Ok(s) = serde_json::to_string(value) {
                                result.push_str(&s);
                            } else {
                                result.push_str("{{");
                                result.push_str(&var_name);
                                result.push_str("}}");
                            }
                        }
                    }
                } else {
                    result.push_str("{{");
                    result.push_str(&var_name);
                    result.push_str("}}");
                }
            } else {
                result.push_str("{{");
                result.push_str(&var_name);
            }
        } else {
            result.push(ch);
        }
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::heuristics::confidence::{ExtractionConfidence, SchemaCompliance};

    /// Caller-supplied excerpt cap used across the assembly tests (a
    /// conventional 200k limit, supplied as a plain parameter).
    const TEST_MAX_EXCERPT: usize = 200_000;

    /// A literal citation instruction, supplied as a parameter — no preset, and
    /// not the worker's embedded `CITATION_INSTRUCTION` text.
    const TEST_CITATION_INSTRUCTION: &str = "\n\n---\n\nFORMAT EACH FIELD WITH value/page/bbox/confidence.\n";

    #[test]
    fn context_substitution_all_vars_present() {
        let mut context = serde_json::Map::new();
        context.insert("entity_type".to_string(), serde_json::json!("invoice"));
        context.insert("document_kind".to_string(), serde_json::json!("receipt"));

        let prompt = build_prompt(
            "Extract {{entity_type}} from the {{document_kind}}.",
            Some("Context: {{entity_type}}"),
            "text",
            Some(&context),
            StructuredCallMode::TextOnly,
            None,
            TEST_MAX_EXCERPT,
        );

        assert!(prompt.system.contains("Extract invoice from the receipt."));
        assert!(prompt.user_text.unwrap().contains("Context: invoice"));
    }

    #[test]
    fn context_substitution_missing_var_leaves_placeholder() {
        let mut context = serde_json::Map::new();
        context.insert("entity_type".to_string(), serde_json::json!("invoice"));

        let prompt = build_prompt(
            "Extract {{entity_type}} from {{missing_var}}.",
            None,
            "text",
            Some(&context),
            StructuredCallMode::TextOnly,
            None,
            TEST_MAX_EXCERPT,
        );

        assert!(prompt.system.contains("Extract invoice from {{missing_var}}."));
    }

    #[test]
    fn context_substitution_with_nested_json() {
        let mut context = serde_json::Map::new();
        context.insert("config".to_string(), serde_json::json!({"nested": "value"}));

        let prompt = build_prompt(
            "Use {{config}}",
            None,
            "text",
            Some(&context),
            StructuredCallMode::TextOnly,
            None,
            TEST_MAX_EXCERPT,
        );

        assert!(prompt.system.contains(r#"{"nested":"value"}"#));
    }

    #[test]
    fn context_substitution_with_number() {
        let mut context = serde_json::Map::new();
        context.insert("threshold".to_string(), serde_json::json!(0.95));

        let prompt = build_prompt(
            "Precision: {{threshold}}",
            None,
            "text",
            Some(&context),
            StructuredCallMode::TextOnly,
            None,
            TEST_MAX_EXCERPT,
        );

        assert!(prompt.system.contains("Precision: 0.95"));
    }

    #[test]
    fn context_substitution_with_bool() {
        let mut context = serde_json::Map::new();
        context.insert("strict".to_string(), serde_json::json!(true));

        let prompt = build_prompt(
            "Strict mode: {{strict}}",
            None,
            "text",
            Some(&context),
            StructuredCallMode::TextOnly,
            None,
            TEST_MAX_EXCERPT,
        );

        assert!(prompt.system.contains("Strict mode: true"));
    }

    #[test]
    fn text_only_includes_excerpt() {
        let prompt = build_prompt(
            "You are a helpful extractor.",
            None,
            "Sample extracted text",
            None,
            StructuredCallMode::TextOnly,
            None,
            TEST_MAX_EXCERPT,
        );

        assert_eq!(prompt.system, "You are a helpful extractor.");
        assert!(prompt.user_text.is_some());
        assert!(prompt.user_text.unwrap().contains("Sample extracted text"));
    }

    #[test]
    fn vision_only_has_no_user_text() {
        let prompt = build_prompt(
            "You are a helpful extractor.",
            None,
            "Sample extracted text",
            None,
            StructuredCallMode::VisionOnly,
            None,
            TEST_MAX_EXCERPT,
        );

        assert_eq!(prompt.system, "You are a helpful extractor.");
        assert!(prompt.user_text.is_none());
    }

    #[test]
    fn skip_mode_returns_none_with_warning() {
        let prompt = build_prompt(
            "You are a helpful extractor.",
            None,
            "text",
            None,
            StructuredCallMode::Skip,
            None,
            TEST_MAX_EXCERPT,
        );

        assert!(prompt.user_text.is_none());
        assert_eq!(prompt.system, "You are a helpful extractor.");
    }

    #[test]
    fn text_plus_vision_includes_excerpt() {
        let prompt = build_prompt(
            "You are a helpful extractor.",
            None,
            "Sample text with images",
            None,
            StructuredCallMode::TextPlusVision,
            None,
            TEST_MAX_EXCERPT,
        );

        assert!(prompt.user_text.is_some());
        assert!(prompt.user_text.unwrap().contains("Sample text with images"));
    }

    #[test]
    fn citation_instruction_appended_when_passed() {
        let prompt = build_prompt(
            "You are a helpful extractor.",
            None,
            "text",
            None,
            StructuredCallMode::TextOnly,
            Some(TEST_CITATION_INSTRUCTION),
            TEST_MAX_EXCERPT,
        );

        assert!(prompt.system.contains(TEST_CITATION_INSTRUCTION));
    }

    #[test]
    fn citation_instruction_not_appended_when_none() {
        let prompt = build_prompt(
            "You are a helpful extractor.",
            None,
            "text",
            None,
            StructuredCallMode::TextOnly,
            None,
            TEST_MAX_EXCERPT,
        );

        assert_eq!(prompt.system, "You are a helpful extractor.");
    }

    #[test]
    fn excerpt_truncated_at_max_excerpt_bytes() {
        let long_excerpt = "a".repeat(300_000);

        let prompt = build_prompt(
            "You are a helpful extractor.",
            None,
            &long_excerpt,
            None,
            StructuredCallMode::TextOnly,
            None,
            TEST_MAX_EXCERPT,
        );

        let user_text = prompt.user_text.unwrap();
        assert!(user_text.len() < 300_000);
        assert!(user_text.contains(&"a".repeat(1000)));
    }

    #[test]
    fn context_template_merged_with_excerpt() {
        let prompt = build_prompt(
            "You are a helpful extractor.",
            Some("Document type: invoice"),
            "Invoice number: 12345",
            None,
            StructuredCallMode::TextOnly,
            None,
            TEST_MAX_EXCERPT,
        );

        let user_text = prompt.user_text.unwrap();
        assert!(user_text.contains("Document type: invoice"));
        assert!(user_text.contains("Invoice number: 12345"));
        assert!(user_text.contains("---"));
    }

    #[test]
    fn vision_fallback_includes_excerpt_prior_and_confidence() {
        let confidence = ExtractionConfidence {
            text_coverage: 0.5,
            ocr_aggregate: Some(0.8),
            schema_compliance: SchemaCompliance::PartialValid,
            combined: 0.6,
        };
        let prior = serde_json::json!({"name": "Alice"});

        let prompt = build_vision_fallback_prompt(
            "You are a helpful extractor.",
            None,
            "Sample extracted text",
            None,
            &prior,
            &confidence,
            Some(TEST_CITATION_INSTRUCTION),
            TEST_MAX_EXCERPT,
        );

        assert!(prompt.system.contains(TEST_CITATION_INSTRUCTION));
        let user_text = prompt.user_text.unwrap();
        assert!(user_text.contains("Sample extracted text"));
        assert!(user_text.contains("Prior text-only extraction"));
        assert!(user_text.contains("\"name\": \"Alice\""));
        assert!(user_text.contains("Text coverage: 50.00%"));
        assert!(user_text.contains("OCR confidence: 80.00%"));
        assert!(user_text.contains("Combined score: 60.00%"));
    }

    #[test]
    fn vision_fallback_truncates_excerpt() {
        let confidence = ExtractionConfidence {
            text_coverage: 1.0,
            ocr_aggregate: None,
            schema_compliance: SchemaCompliance::AllValid,
            combined: 1.0,
        };
        let prior = serde_json::json!({});
        let long_excerpt = "b".repeat(300_000);

        let prompt = build_vision_fallback_prompt(
            "system",
            None,
            &long_excerpt,
            None,
            &prior,
            &confidence,
            None,
            TEST_MAX_EXCERPT,
        );

        let user_text = prompt.user_text.unwrap();
        assert!(!user_text.contains(&"b".repeat(300_000)));
        assert!(user_text.contains(&"b".repeat(1000)));
    }

    #[test]
    fn multibyte_excerpt_truncation_does_not_panic() {
        let multibyte = "".repeat(200);
        let max_bytes = 100usize;

        let prompt = build_prompt(
            "system",
            None,
            &multibyte,
            None,
            StructuredCallMode::TextOnly,
            None,
            max_bytes,
        );
        let user_text = prompt.user_text.expect("text-only mode yields user text");
        assert!(user_text.len() <= max_bytes);
        assert!(user_text.chars().all(|c| c == ''));

        let confidence = ExtractionConfidence {
            text_coverage: 1.0,
            ocr_aggregate: None,
            schema_compliance: SchemaCompliance::AllValid,
            combined: 1.0,
        };
        let fallback = build_vision_fallback_prompt(
            "system",
            None,
            &multibyte,
            None,
            &serde_json::json!({}),
            &confidence,
            None,
            max_bytes,
        );
        assert!(fallback.user_text.expect("fallback user text").contains(''));
    }
}