@xberg-io/opencode-xberg 1.3.7 → 1.3.8

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 (40) hide show
  1. package/.opencode/ai-rulez-bundle.json +51 -0
  2. package/.opencode/ai-rulez-content.js +167 -0
  3. package/.opencode/commands/xberg-extract.md +14 -0
  4. package/.opencode/commands/xberg-ocr.md +14 -0
  5. package/.opencode/plugins/xberg.js +75 -62
  6. package/.opencode/skills/batch-extraction/SKILL.md +168 -0
  7. package/.opencode/skills/chunking/SKILL.md +163 -0
  8. package/.opencode/skills/extracting-keywords/SKILL.md +171 -0
  9. package/.opencode/skills/extracting-tables/SKILL.md +156 -0
  10. package/.opencode/skills/extracting-with-ocr/SKILL.md +130 -0
  11. package/.opencode/skills/picking-a-format/SKILL.md +104 -0
  12. package/.opencode/skills/xberg/SKILL.md +427 -0
  13. package/.opencode/skills/xberg/references/advanced-features.md +628 -0
  14. package/.opencode/skills/xberg/references/cli-reference.md +462 -0
  15. package/.opencode/skills/xberg/references/configuration.md +587 -0
  16. package/.opencode/skills/xberg/references/nodejs-api.md +485 -0
  17. package/.opencode/skills/xberg/references/other-bindings.md +345 -0
  18. package/.opencode/skills/xberg/references/python-api.md +657 -0
  19. package/.opencode/skills/xberg/references/rust-api.md +412 -0
  20. package/.opencode/skills/xberg/references/supported-formats.md +252 -0
  21. package/.pi/prompts/xberg-extract.md +14 -0
  22. package/.pi/prompts/xberg-ocr.md +14 -0
  23. package/.pi/skills/batch-extraction/SKILL.md +168 -0
  24. package/.pi/skills/chunking/SKILL.md +163 -0
  25. package/.pi/skills/extracting-keywords/SKILL.md +171 -0
  26. package/.pi/skills/extracting-tables/SKILL.md +156 -0
  27. package/.pi/skills/extracting-with-ocr/SKILL.md +130 -0
  28. package/.pi/skills/picking-a-format/SKILL.md +104 -0
  29. package/.pi/skills/xberg/SKILL.md +427 -0
  30. package/.pi/skills/xberg/references/advanced-features.md +628 -0
  31. package/.pi/skills/xberg/references/cli-reference.md +462 -0
  32. package/.pi/skills/xberg/references/configuration.md +587 -0
  33. package/.pi/skills/xberg/references/nodejs-api.md +485 -0
  34. package/.pi/skills/xberg/references/other-bindings.md +345 -0
  35. package/.pi/skills/xberg/references/python-api.md +657 -0
  36. package/.pi/skills/xberg/references/rust-api.md +412 -0
  37. package/.pi/skills/xberg/references/supported-formats.md +252 -0
  38. package/README.md +40 -0
  39. package/package.json +69 -16
  40. package/scripts/mcp-launch.sh +188 -0
@@ -0,0 +1,14 @@
1
+ ---
2
+ description: Extract scanned documents with OCR
3
+ ---
4
+
5
+ <!--
6
+ AI-RULEZ :: GENERATED FILE — DO NOT EDIT
7
+ Content-Hash: blake3:1715ebd3d000212eb2e4dbfa814d6d5990bcc0c636bae67734a503f2c3ae8be4
8
+ Source-Hash: blake3:3fca38eb564d0ed9c9194d070fd8dec96de727edc92e82954cb4b9af52cddbaf
9
+ Schema-Version: v1
10
+ -->
11
+
12
+ OCR this document: $ARGUMENTS
13
+
14
+ Read the extracting-with-ocr skill. Confirm the document needs OCR and language packs are installed. Use `xberg extract <file> --force-ocr=true` when forced OCR is appropriate. Preserve paths as separate arguments and report OCR failures.
@@ -0,0 +1,168 @@
1
+ ---
2
+ name: batch-extraction
3
+ description: Use when extracting from many files at once with shared config, bounded parallelism, per-file overrides, and error recovery. Covers the `batch` command, `--file-configs`, `--max-concurrent`, and output layout.
4
+ ---
5
+
6
+ <!--
7
+ AI-RULEZ :: GENERATED FILE — DO NOT EDIT
8
+ Content-Hash: blake3:3a70b47dd77fcd3f37e83bed3d32b1ad1ea9d6c1642ae8e0ff7b09b55939858a
9
+ Source-Hash: blake3:3fca38eb564d0ed9c9194d070fd8dec96de727edc92e82954cb4b9af52cddbaf
10
+ Schema-Version: v1
11
+ -->
12
+
13
+ # Batch extraction
14
+
15
+ Use this when processing a directory or glob of documents in one pass.
16
+ `xberg batch` shares one extraction config across every file, runs
17
+ extractions concurrently, and returns one structured array — failures on
18
+ individual files do not abort the run.
19
+
20
+ ## Basic usage
21
+
22
+ ```bash
23
+ # Glob expands to many paths; results come back as a JSON array (default)
24
+ xberg batch *.pdf
25
+
26
+ # Mixed formats, markdown content for LLM ingestion
27
+ xberg batch docs/*.docx --content-format markdown
28
+
29
+ # Recurse with the shell, then extract
30
+ xberg batch $(find ./corpus -name '*.pdf')
31
+ ```
32
+
33
+ `batch` defaults to `--format json` (vs `--format text` for single
34
+ `extract`). Each array entry is a full extraction result, so downstream
35
+ code can index by position into the input path list.
36
+
37
+ ```bash
38
+ xberg batch reports/*.pdf \
39
+ | jq '.[] | {chars: (.content | length), mime: .mime_type}'
40
+ ```
41
+
42
+ ## Parallelism
43
+
44
+ `--max-concurrent` caps how many files extract at once. When omitted, the scheduler derives document concurrency from the total thread budget. Lower it on memory-constrained hosts or when OCR/ML
45
+ models are active, since each in-flight extraction holds its own buffers.
46
+ Layout-heavy batches are further limited (1 concurrent extraction for
47
+ all-PDF-layout batches, 2 for mixed layout):
48
+
49
+ ```bash
50
+ # Cap at 4 concurrent extractions
51
+ xberg batch scans/*.pdf --ocr true --max-concurrent 4
52
+ ```
53
+
54
+ `--max-threads` additionally caps *total* internal threads (Rayon, ONNX
55
+ intra-op, the batch semaphore) for tightly constrained environments:
56
+
57
+ ```bash
58
+ xberg batch *.pdf --max-concurrent 2 --max-threads 4
59
+ ```
60
+
61
+ ## Per-file config overrides
62
+
63
+ A single shared config does not always fit. `--file-configs` points at a
64
+ JSON file mapping each path to its own override object, merged on top of
65
+ the shared config for that file only:
66
+
67
+ ```json
68
+ {
69
+ "scan.pdf": { "force_ocr": true },
70
+ "report.pdf": { "output_format": "markdown" },
71
+ "data.xlsx": { "output_format": "json" }
72
+ }
73
+ ```
74
+
75
+ ```bash
76
+ xberg batch scan.pdf report.pdf data.xlsx --file-configs overrides.json
77
+ ```
78
+
79
+ Keys are file paths (matching the paths passed on the command line);
80
+ values are per-file extraction config objects in snake_case, the same
81
+ shape as a config file.
82
+
83
+ ## Output layout
84
+
85
+ For text/toon output with image extraction, `--output-dir` controls where
86
+ referenced image files (e.g. `image_0.png`) are written; the directory
87
+ must already exist. JSON output embeds image bytes inline and ignores
88
+ `--output-dir`.
89
+
90
+ ```bash
91
+ mkdir -p out/images
92
+ xberg batch slides/*.pptx --extract-images true --output-dir out/images --format text
93
+ ```
94
+
95
+ ## Error recovery
96
+
97
+ Batch extraction is fault-tolerant per file: one unreadable or corrupt
98
+ document does not stop the rest. Inspect results for partial content and
99
+ surfaced errors rather than relying on the process exit code alone. Pair
100
+ with `--max-concurrent` to avoid exhausting memory when a few large files
101
+ sit in a big batch.
102
+
103
+ ## Shared config
104
+
105
+ Every `extract` flag also applies to `batch` (OCR, chunking, layout,
106
+ content format, etc.) and is shared across all files unless a
107
+ `--file-configs` entry overrides it:
108
+
109
+ ```bash
110
+ xberg batch invoices/*.pdf \
111
+ --layout --layout-table-model slanet_wireless \
112
+ --content-format markdown --max-concurrent 8
113
+ ```
114
+
115
+ A config file works too and auto-discovers from the cwd upward:
116
+
117
+ ```toml
118
+ output_format = "markdown"
119
+
120
+ [ocr]
121
+ backend = "tesseract"
122
+ language = "eng"
123
+ ```
124
+
125
+ ```bash
126
+ xberg batch corpus/*.pdf --config xberg.toml
127
+ ```
128
+
129
+ ## Programmatic access
130
+
131
+ From Python, `extract_batch` takes a list of `ExtractInput`s and returns one
132
+ envelope whose `results` array holds a document per input:
133
+
134
+ ```python
135
+ from xberg import ExtractInput, extract_batch, ExtractionConfig
136
+
137
+ config = ExtractionConfig(output_format="markdown")
138
+
139
+ inputs = [ExtractInput(uri=p) for p in ["a.pdf", "b.docx", "c.xlsx"]]
140
+ output = await extract_batch(inputs, config)
141
+
142
+ for doc in output.results:
143
+ print(len(doc.content))
144
+ ```
145
+
146
+ Per-input overrides go on `ExtractInput.config` (a `FileExtractionConfig`).
147
+ Node.js mirrors this with `extractBatch`; Rust uses `extract_batch(inputs, &config)`.
148
+ See `references/python-api.md`, `references/nodejs-api.md`, and
149
+ `references/rust-api.md` in the sibling `xberg` skill.
150
+
151
+ ## MCP
152
+
153
+ When the `xberg` MCP server is registered, prefer the
154
+ `extract_batch` tool over shelling out — it takes an array of input objects
155
+ and a config object and returns structured results directly.
156
+
157
+ ## Common pitfalls
158
+
159
+ - **Default format differs** — `batch` defaults to `--format json`,
160
+ `extract` to `--format text`. Set `--format` explicitly if a script
161
+ depends on one shape.
162
+ - **`--output-dir` must exist** — the CLI does not create it.
163
+ - **Memory blowups** — large batches with OCR/layout active may need an explicit
164
+ `--max-concurrent` ceiling.
165
+ - **`--file-configs` path keys** — must match the paths as passed on the
166
+ command line, not absolute-resolved variants.
167
+
168
+ See `references/cli-reference.md` for the full `batch` flag set.
@@ -0,0 +1,163 @@
1
+ ---
2
+ name: chunking
3
+ description: Use when splitting extracted text into chunks for LLM context windows or RAG ingestion. Covers chunk size, overlap, markdown/yaml/semantic chunkers, tokenizer-based sizing, and the standalone `chunk` command.
4
+ ---
5
+
6
+ <!--
7
+ AI-RULEZ :: GENERATED FILE — DO NOT EDIT
8
+ Content-Hash: blake3:0b5cd4bec9d2a8f3452e07139ef56d6b87f9f7990714d15483a03435e751af59
9
+ Source-Hash: blake3:3fca38eb564d0ed9c9194d070fd8dec96de727edc92e82954cb4b9af52cddbaf
10
+ Schema-Version: v1
11
+ -->
12
+
13
+ # Chunking
14
+
15
+ Use this when feeding documents into an LLM context window or a vector
16
+ store. Xberg chunks two ways: inline during extraction (chunks land on
17
+ each document's `chunks` field), or standalone via the `chunk` command for text you
18
+ already have. Sizing is character-based by default, or token-based when a
19
+ tokenizer model is supplied.
20
+
21
+ ## Inline during extraction
22
+
23
+ Turn on chunking with `--chunk` and the chunks appear on the structured
24
+ result under `chunks`:
25
+
26
+ ```bash
27
+ # 1000-char chunks, 200-char overlap (defaults when --chunk is on)
28
+ xberg extract report.pdf --chunk --format json | jq '.chunks | length'
29
+
30
+ # Explicit size + overlap
31
+ xberg extract report.pdf --chunk --chunk-size 1500 --chunk-overlap 300 --format json
32
+ ```
33
+
34
+ Overlap must be smaller than chunk size — the CLI rejects
35
+ `--chunk-overlap >= --chunk-size`. When you set only `--chunk-overlap`
36
+ against an existing config, an overlap that exceeds the size is clamped to
37
+ `chunk_size / 4`.
38
+
39
+ ## Standalone `chunk` command
40
+
41
+ Chunk text you already have, from `--text` or stdin. Output defaults to
42
+ JSON:
43
+
44
+ ```bash
45
+ # From a flag
46
+ xberg chunk --text "long document text ..." --chunk-size 800 --chunk-overlap 100
47
+
48
+ # From stdin (pipe extracted content straight in)
49
+ xberg extract notes.md | xberg chunk --chunk-size 500 --format json
50
+ ```
51
+
52
+ JSON output carries `chunks` (array of strings), `chunk_count`, the
53
+ resolved `config` (`max_characters`, `overlap`, `chunker_type`), and
54
+ `input_size_bytes`. Use `--format text` for a human-readable dump with
55
+ `--- chunk N ---` separators.
56
+
57
+ > Note: in the JSON output, `chunker_type` is rendered capitalized (`"Text"`,
58
+ > `"Markdown"`, `"Yaml"`, `"Semantic"`) because it is emitted via Rust's Debug
59
+ > formatting, whereas the `--chunker-type` input flag is lowercase
60
+ > (`text`, `markdown`, `yaml`, `semantic`). Lowercase the value before
61
+ > comparing if you parse it back.
62
+
63
+ ## Chunker types
64
+
65
+ `--chunker-type` selects the splitting strategy (standalone `chunk`
66
+ command):
67
+
68
+ | Type | Behavior |
69
+ | ---------- | ------------------------------------------------------------------- |
70
+ | `text` | **Default.** Plain character-window splitting with overlap. |
71
+ | `markdown` | Markdown-aware — splits on structure (headings, blocks) where possible. |
72
+ | `yaml` | YAML-aware splitting for structured config/data documents. |
73
+ | `semantic` | Topic-boundary splitting driven by `--topic-threshold` (0.0–1.0, default 0.75). |
74
+
75
+ ```bash
76
+ # Markdown-aware chunking keeps headings and blocks intact
77
+ xberg chunk --text "$(cat README.md)" --chunker-type markdown
78
+
79
+ # Semantic chunking — lower threshold = more, smaller topic chunks
80
+ xberg chunk --text "$(cat transcript.txt)" --chunker-type semantic --topic-threshold 0.6
81
+ ```
82
+
83
+ ## Token-based sizing
84
+
85
+ By default `--chunk-size` counts characters. To size chunks by tokens for
86
+ a specific model, pass `--chunking-tokenizer` with a HuggingFace tokenizer
87
+ id. On the `extract` command this implicitly enables chunking. Requires the
88
+ `chunking-tokenizers` feature (present in the default CLI build).
89
+
90
+ ```bash
91
+ # Size chunks by GPT-4o tokens during extraction
92
+ xberg extract report.pdf --chunking-tokenizer Xenova/gpt-4o --format json
93
+
94
+ # Or on the standalone command
95
+ xberg chunk --text "$(cat doc.txt)" --chunking-tokenizer Xenova/gpt-4o --chunk-size 512
96
+ ```
97
+
98
+ With a tokenizer set, `--chunk-size` is interpreted in tokens, not
99
+ characters.
100
+
101
+ ## Config file alternative
102
+
103
+ Field names in config files are snake_case under `[chunking]`:
104
+
105
+ ```toml
106
+ [chunking]
107
+ max_characters = 1000
108
+ overlap = 200
109
+ chunker_type = "markdown"
110
+ ```
111
+
112
+ ```bash
113
+ xberg extract report.pdf --config xberg.toml --format json
114
+ ```
115
+
116
+ > CLI flags map to config fields as `--chunk-size` → `max_characters` and
117
+ > `--chunk-overlap` → `overlap`. In config files use the snake_case names.
118
+
119
+ ## Programmatic access
120
+
121
+ From Python, enable chunking on the config and read the chunks off the
122
+ document in the result envelope (`result.results[0].chunks`):
123
+
124
+ ```python
125
+ from xberg import ExtractInput, extract, ExtractionConfig, ChunkingConfig
126
+
127
+ config = ExtractionConfig(
128
+ chunking=ChunkingConfig(max_characters=1000, overlap=200),
129
+ )
130
+ result = await extract(ExtractInput(uri="report.pdf"), config)
131
+ for chunk in result.results[0].chunks or []:
132
+ print(len(chunk.content))
133
+ ```
134
+
135
+ > The public Python `ChunkingConfig` (a dataclass) uses constructor kwargs
136
+ > `max_characters` / `overlap`; the Rust core struct fields are also
137
+ > `max_characters` / `overlap`. TOML/JSON config keys are `max_chars` /
138
+ > `max_overlap` (with `max_characters` / `overlap` accepted as serde aliases),
139
+ > and dict-form config passed to `ExtractionConfig` likewise accepts the
140
+ > `max_chars` / `max_overlap` aliases; Node's `ChunkingConfig` interface uses
141
+ > `maxCharacters` / `overlap`. See `references/python-api.md` and
142
+ > `references/rust-api.md` in the sibling `xberg` skill.
143
+
144
+ ## Picking parameters
145
+
146
+ - **RAG / vector store** — 500–1000 chars (or 256–512 tokens) with
147
+ 10–20% overlap. Use `markdown` chunking for docs to keep sections whole.
148
+ - **LLM summarization** — larger chunks (1500–4000 chars) with small
149
+ overlap; size by tokens to stay under the model window.
150
+ - **Topic segmentation** — `semantic` chunker; tune `--topic-threshold`
151
+ down for finer splits, up for coarser ones.
152
+
153
+ ## Common pitfalls
154
+
155
+ - **Overlap ≥ size** — rejected on `extract`; clamped to `size / 4` when
156
+ only overlap is changed against an existing config.
157
+ - **Tokenizer without the feature** — `--chunking-tokenizer` errors if the
158
+ CLI was built without `chunking-tokenizers`. The default build includes it.
159
+ - **Empty input** — the standalone `chunk` command bails on empty text;
160
+ provide `--text` or pipe non-empty stdin.
161
+
162
+ See `references/configuration.md` for the full `[chunking]` schema and
163
+ `references/cli-reference.md` for every chunk flag.
@@ -0,0 +1,171 @@
1
+ ---
2
+ name: extracting-keywords
3
+ description: Use when extracting keywords (YAKE/RAKE) from documents — and, secondarily, when detecting document language or generating embeddings for RAG and search. Covers the keyword config (and its feature gating), `--detect-language`, and the standalone `embed` command with real flags.
4
+ ---
5
+
6
+ <!--
7
+ AI-RULEZ :: GENERATED FILE — DO NOT EDIT
8
+ Content-Hash: blake3:0da13ce50f1fef8c192e912e7c2b0d039209a7b676829abe13f4fc86b5ea20bc
9
+ Source-Hash: blake3:3fca38eb564d0ed9c9194d070fd8dec96de727edc92e82954cb4b9af52cddbaf
10
+ Schema-Version: v1
11
+ -->
12
+
13
+ # Extracting keywords, language, and embeddings
14
+
15
+ Use this for the enrichment surface around extraction: statistical keyword
16
+ extraction, language detection, and vector embeddings. Keywords and
17
+ language detection ride along with extraction and land on the result;
18
+ embeddings are produced by a dedicated `embed` command.
19
+
20
+ ## Keywords (YAKE / RAKE)
21
+
22
+ Keyword extraction is configured via the `[keywords]` config block (or
23
+ inline JSON) — there is no single `--keywords` CLI flag. When enabled,
24
+ extracted keywords appear on `result.extracted_keywords` (`extractedKeywords`
25
+ in Node.js; the CLI JSON field is `extracted_keywords`). Two algorithms are
26
+ available:
27
+
28
+ - **YAKE** (`"yake"`) — statistical, unsupervised single-document
29
+ extraction. Good general default.
30
+ - **RAKE** (`"rake"`) — co-occurrence / phrase-based. Favors multi-word
31
+ key phrases.
32
+
33
+ > Feature-gated: keyword extraction requires the CLI to be built with the
34
+ > `keywords-yake` and/or `keywords-rake` Cargo features (both are in the
35
+ > default/`full` build). If the CLI was built without them, the `[keywords]`
36
+ > config block is silently ignored — `result.extracted_keywords` simply stays empty
37
+ > rather than erroring. The `"yake"` algorithm needs `keywords-yake`; `"rake"`
38
+ > needs `keywords-rake`.
39
+
40
+ Enable via inline JSON on the CLI:
41
+
42
+ ```bash
43
+ xberg extract paper.pdf --format json \
44
+ --config-json '{"keywords":{"algorithm":"yake","max_keywords":15,"language":"en"}}' \
45
+ | jq '.extracted_keywords'
46
+ ```
47
+
48
+ Or in a config file:
49
+
50
+ ```toml
51
+ [keywords]
52
+ algorithm = "rake" # "yake" or "rake"
53
+ max_keywords = 10 # default 10
54
+ min_score = 0.0 # filter below this score (normalized 0.0-1.0 for both algorithms)
55
+ ngram_range = [1, 3] # unigrams..trigrams (default); config-file only
56
+ language = "en" # stopword language; omit to skip stopword filtering
57
+ ```
58
+
59
+ ```bash
60
+ xberg extract report.pdf --config xberg.toml --format json | jq '.extracted_keywords'
61
+ ```
62
+
63
+ Field notes:
64
+
65
+ - `max_keywords` caps how many keywords are returned (default 10).
66
+ - `min_score` filters low-scoring keywords. Both YAKE and RAKE normalize
67
+ their scores to the `0.0`-`1.0` range with *higher-is-better*, so
68
+ `min_score` retains keywords with `score >= min_score` identically for
69
+ either algorithm.
70
+ - `ngram_range` is `[min, max]`: `[1,1]` unigrams only, `[1,2]` adds
71
+ bigrams, `[1,3]` (default) adds trigrams. Config-file only — it is not a
72
+ field on the language bindings' `KeywordConfig`.
73
+ - `language` enables stopword filtering for that language; omit it to
74
+ disable stopword filtering entirely.
75
+
76
+ ## Language detection
77
+
78
+ Language detection is a real CLI flag: `--detect-language`. Detected
79
+ languages appear on `result.detected_languages`:
80
+
81
+ ```bash
82
+ xberg extract multilingual.pdf --detect-language true --format json \
83
+ | jq '.detected_languages'
84
+ ```
85
+
86
+ In a config file it lives under `[language_detection]`:
87
+
88
+ ```toml
89
+ [language_detection]
90
+ enabled = true
91
+ min_confidence = 0.8
92
+ detect_multiple = false
93
+ ```
94
+
95
+ The CLI flag enables detection with `min_confidence = 0.8` and
96
+ single-language mode; use the config block to detect multiple languages or
97
+ tune confidence.
98
+
99
+ ## Embeddings (`embed` command)
100
+
101
+ The standalone `embed` command produces vector embeddings for text from
102
+ `--text` (repeatable) or stdin. It does not run extraction — pipe
103
+ extracted content in if you want document embeddings.
104
+
105
+ ```bash
106
+ # Local ONNX preset model (default provider)
107
+ xberg embed --text "first passage" --text "second passage" --preset balanced
108
+
109
+ # Embed extracted document text
110
+ xberg extract report.pdf | xberg embed --preset quality
111
+ ```
112
+
113
+ Presets for the local provider: `fast`, `balanced` (default), `quality`,
114
+ `multilingual`. Output defaults to JSON (`--format json`).
115
+
116
+ `--provider` selects the embedding source:
117
+
118
+ | Provider | Flag | Notes |
119
+ | -------- | ------------------------------------- | --------------------------------------------- |
120
+ | `local` | `--preset <fast\|balanced\|quality\|multilingual>` | **Default.** ONNX model, no API key. |
121
+ | `llm` | `--model <id>` `--api-key <key>` | liter-llm routing, e.g. `openai/text-embedding-3-small`. |
122
+ | `plugin` | `--plugin <name>` | A backend pre-registered in-process via the plugin API. |
123
+
124
+ ```bash
125
+ # Provider-hosted embeddings via an LLM
126
+ xberg embed --text "query text" \
127
+ --provider llm --model openai/text-embedding-3-small --api-key "$OPENAI_API_KEY"
128
+ ```
129
+
130
+ Local embedding presets must be downloaded first if not cached. Pre-warm
131
+ them with the cache command:
132
+
133
+ ```bash
134
+ xberg cache warm --embedding-model balanced # one preset
135
+ xberg cache warm --all-embeddings # all available presets (currently 8)
136
+ ```
137
+
138
+ ## Programmatic access
139
+
140
+ Keywords and detected languages live on the document in the result envelope:
141
+
142
+ ```python
143
+ from xberg import ExtractInput, extract, ExtractionConfig, KeywordConfig, KeywordAlgorithm
144
+
145
+ config = ExtractionConfig(
146
+ keywords=KeywordConfig(algorithm=KeywordAlgorithm.YAKE, max_keywords=15, language="en"),
147
+ )
148
+ result = await extract(ExtractInput(uri="paper.pdf"), config)
149
+ doc = result.results[0]
150
+ print(doc.extracted_keywords) # extracted keywords (when enabled)
151
+ print(doc.detected_languages) # detected languages (when enabled)
152
+ ```
153
+
154
+ See `references/python-api.md` and `references/configuration.md` in the
155
+ sibling `xberg` skill for the keyword / language-detection config
156
+ classes and the embedding presets.
157
+
158
+ ## Common pitfalls
159
+
160
+ - **No `--keywords` flag** — keyword extraction is config-only. Use
161
+ `--config-json '{"keywords":{...}}'` or a `[keywords]` config block.
162
+ - **`min_score` direction** — scores are normalized to `0.0`-`1.0` with
163
+ higher-is-better for both YAKE and RAKE, so the same threshold behaves
164
+ identically for either algorithm.
165
+ - **Embeddings ≠ extraction** — `embed` only takes raw text. Pipe
166
+ `xberg extract` output into it for document vectors.
167
+ - **Cold embedding models** — first local run downloads the preset; run
168
+ `xberg cache warm --all-embeddings` to pre-populate.
169
+
170
+ See `references/advanced-features.md` for the embeddings pipeline and
171
+ `references/cli-reference.md` for the `embed` and `cache warm` flag sets.
@@ -0,0 +1,156 @@
1
+ ---
2
+ name: extracting-tables
3
+ description: Use when extracting tabular data from PDFs, spreadsheets, or images. Covers layout-aware table detection, table model selection, output formats (markdown / JSON cells), and known limits.
4
+ ---
5
+
6
+ <!--
7
+ AI-RULEZ :: GENERATED FILE — DO NOT EDIT
8
+ Content-Hash: blake3:7667a52a8674605a45cc61b67e7879a0104d5e86c0d82b4bde5ced9e6e3463a8
9
+ Source-Hash: blake3:3fca38eb564d0ed9c9194d070fd8dec96de727edc92e82954cb4b9af52cddbaf
10
+ Schema-Version: v1
11
+ -->
12
+
13
+ # Extracting tables
14
+
15
+ Use this when the user wants structured tabular data — financial
16
+ statements, scientific tables, invoices, spreadsheet-style PDFs. Xberg
17
+ detects tables via a layout model (RT-DETR v2) and reconstructs cell
18
+ structure with a configurable table model.
19
+
20
+ ## Basic usage
21
+
22
+ ```bash
23
+ # Markdown tables embedded in the content stream
24
+ xberg extract report.pdf --layout --content-format markdown
25
+
26
+ # Structured JSON output, tables appear under result.tables
27
+ xberg extract report.pdf --layout --format json
28
+ ```
29
+
30
+ `--layout` turns on layout-aware extraction; without it, tables fall back
31
+ to plain text reflow and you lose cell boundaries.
32
+
33
+ ## Output shapes
34
+
35
+ Two surfaces, picked via `--format` (CLI shape) and `--content-format`
36
+ (content rendering):
37
+
38
+ - **Markdown tables in `content`** — `--content-format markdown`. Tables
39
+ appear inline as `| col | col |` blocks. Good for LLM ingestion.
40
+ - **Structured `tables` array** — `--format json`. Each entry has
41
+ `cells[][]` (rows × cols), `markdown` (pre-rendered), `page_number`,
42
+ `bounding_box`. Use this when downstream code needs exact cell access.
43
+ (`bounding_box` is omitted when no position data is available.)
44
+
45
+ Both are populated at once when `--layout` is on. The `tables` array is
46
+ always structured; the `content` stream switches representation.
47
+
48
+ ```bash
49
+ xberg extract financials.pdf --layout --format json \
50
+ | jq '.result.tables[] | {page: .page_number, rows: (.cells | length)}'
51
+ ```
52
+
53
+ ## Table models
54
+
55
+ `--layout-table-model` picks the reconstruction backend:
56
+
57
+ | Model | Best for | Notes |
58
+ | ------------------ | ----------------------------------------------------- | ------------------------------------------- |
59
+ | `tatr` | dense complex tables (academic, financial) | **Default.** Heaviest, highest accuracy. |
60
+ | `slanet_auto` | dispatches per-table to wired/wireless | Good when table styles are mixed. |
61
+ | `slanet_wired` | tables with visible borders | Faster than tatr. |
62
+ | `slanet_wireless` | tables without borders (whitespace-separated) | For invoices, simple grids. |
63
+ | `slanet_plus` | hybrid wired / wireless | Lighter than `slanet_auto`. |
64
+ | `disabled` | layout detection only, no table structure | Use to skip table model cost. |
65
+
66
+ ```bash
67
+ xberg extract bank-statement.pdf \
68
+ --layout --layout-table-model tatr --content-format markdown
69
+ ```
70
+
71
+ Drop `--layout-confidence` when the layout model misses tables (default
72
+ threshold ~0.5):
73
+
74
+ ```bash
75
+ xberg extract noisy-scan.pdf --layout --layout-confidence 0.3
76
+ ```
77
+
78
+ ## Spreadsheets
79
+
80
+ `.xlsx`, `.ods`, `.csv`, `.tsv` are extracted by dedicated parsers — no
81
+ layout model needed. Each sheet becomes a markdown table (or structured
82
+ table) automatically:
83
+
84
+ ```bash
85
+ xberg extract workbook.xlsx --content-format markdown
86
+ xberg extract data.csv --format json
87
+ ```
88
+
89
+ Pass `--no-cache=true` only when iterating on the same file with different
90
+ configs.
91
+
92
+ ## Config file alternative
93
+
94
+ ```toml
95
+ # `output_format` in config files equals `--content-format` on the CLI.
96
+ output_format = "markdown"
97
+
98
+ [layout]
99
+ confidence_threshold = 0.5
100
+ table_model = "tatr"
101
+ ```
102
+
103
+ Then:
104
+
105
+ ```bash
106
+ xberg extract report.pdf --format json
107
+ ```
108
+
109
+ ## Programmatic access
110
+
111
+ From Python, structured tables live on the document in the result envelope
112
+ (`result.results[0].tables`):
113
+
114
+ ```python
115
+ from xberg import ExtractInput, extract, ExtractionConfig, LayoutDetectionConfig
116
+
117
+ config = ExtractionConfig(
118
+ layout=LayoutDetectionConfig(table_model="tatr"),
119
+ output_format="markdown",
120
+ )
121
+ result = await extract(ExtractInput(uri="report.pdf"), config)
122
+ for table in result.results[0].tables:
123
+ print(table.markdown) # rendered markdown
124
+ print(table.cells[0][0]) # cell access
125
+ ```
126
+
127
+ Node.js mirrors this (`extract`, `output.results[0].tables`, camelCase fields).
128
+ See `references/python-api.md` and `references/nodejs-api.md` in the
129
+ sibling `xberg` skill for full type signatures.
130
+
131
+ ## Known limitations
132
+
133
+ - **Merged cells** — reconstructed as repeated values across the spanned
134
+ region; the merge is not preserved as metadata.
135
+ - **Rotated tables** — enable `--ocr-auto-rotate true` for image-based
136
+ PDFs before extraction.
137
+ - **Nested tables** — flattened. Detection succeeds; structural nesting is
138
+ lost.
139
+ - **Multi-page tables** — each page yields a separate `tables[]` entry.
140
+ Stitch by matching column headers if needed.
141
+ - **ONNX Runtime required** — layout and table models are unavailable in
142
+ WASM builds and on the Android x86_64 emulator; native targets ship
143
+ full support.
144
+
145
+ ## Common failure modes
146
+
147
+ - **Empty `tables` with `--layout` on** — confidence threshold too high or
148
+ table model mismatched. Drop `--layout-confidence` to 0.3, try
149
+ `--layout-table-model tatr`.
150
+ - **Markdown tables look ragged** — switch `--layout-table-model` to
151
+ `slanet_wired` for bordered grids or `slanet_wireless` for invoices.
152
+ - **Slow extraction** — `tatr` is heavy. Use `slanet_auto` or
153
+ `slanet_plus` as a default; reach for `tatr` only when accuracy matters.
154
+
155
+ See `references/cli-reference.md` for the full layout flag set and
156
+ `references/advanced-features.md` for the layout pipeline internals.