docground 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.
- package/LICENSE +21 -0
- package/README.md +217 -0
- package/dist/src/compiler/zod-compiler.d.ts +31 -0
- package/dist/src/compiler/zod-compiler.d.ts.map +1 -0
- package/dist/src/compiler/zod-compiler.js +150 -0
- package/dist/src/compiler/zod-compiler.js.map +1 -0
- package/dist/src/core/extractor.d.ts +17 -0
- package/dist/src/core/extractor.d.ts.map +1 -0
- package/dist/src/core/extractor.js +268 -0
- package/dist/src/core/extractor.js.map +1 -0
- package/dist/src/core/types.d.ts +102 -0
- package/dist/src/core/types.d.ts.map +1 -0
- package/dist/src/core/types.js +2 -0
- package/dist/src/core/types.js.map +1 -0
- package/dist/src/grounding/bbox.d.ts +40 -0
- package/dist/src/grounding/bbox.d.ts.map +1 -0
- package/dist/src/grounding/bbox.js +98 -0
- package/dist/src/grounding/bbox.js.map +1 -0
- package/dist/src/index.d.ts +16 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +16 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/normalizers/currency.d.ts +14 -0
- package/dist/src/normalizers/currency.d.ts.map +1 -0
- package/dist/src/normalizers/currency.js +96 -0
- package/dist/src/normalizers/currency.js.map +1 -0
- package/dist/src/normalizers/tabular.d.ts +16 -0
- package/dist/src/normalizers/tabular.d.ts.map +1 -0
- package/dist/src/normalizers/tabular.js +81 -0
- package/dist/src/normalizers/tabular.js.map +1 -0
- package/dist/src/providers/anthropic-provider.d.ts +13 -0
- package/dist/src/providers/anthropic-provider.d.ts.map +1 -0
- package/dist/src/providers/anthropic-provider.js +62 -0
- package/dist/src/providers/anthropic-provider.js.map +1 -0
- package/dist/src/providers/factory.d.ts +11 -0
- package/dist/src/providers/factory.d.ts.map +1 -0
- package/dist/src/providers/factory.js +32 -0
- package/dist/src/providers/factory.js.map +1 -0
- package/dist/src/providers/gemini-provider.d.ts +13 -0
- package/dist/src/providers/gemini-provider.d.ts.map +1 -0
- package/dist/src/providers/gemini-provider.js +48 -0
- package/dist/src/providers/gemini-provider.js.map +1 -0
- package/dist/src/providers/openai-provider.d.ts +14 -0
- package/dist/src/providers/openai-provider.d.ts.map +1 -0
- package/dist/src/providers/openai-provider.js +48 -0
- package/dist/src/providers/openai-provider.js.map +1 -0
- package/dist/src/providers/types.d.ts +45 -0
- package/dist/src/providers/types.d.ts.map +1 -0
- package/dist/src/providers/types.js +2 -0
- package/dist/src/providers/types.js.map +1 -0
- package/dist/src/stitcher/table-stitcher.d.ts +19 -0
- package/dist/src/stitcher/table-stitcher.d.ts.map +1 -0
- package/dist/src/stitcher/table-stitcher.js +129 -0
- package/dist/src/stitcher/table-stitcher.js.map +1 -0
- package/dist/src/tiling/cutter.d.ts +14 -0
- package/dist/src/tiling/cutter.d.ts.map +1 -0
- package/dist/src/tiling/cutter.js +46 -0
- package/dist/src/tiling/cutter.js.map +1 -0
- package/dist/src/tiling/tiler.d.ts +41 -0
- package/dist/src/tiling/tiler.d.ts.map +1 -0
- package/dist/src/tiling/tiler.js +82 -0
- package/dist/src/tiling/tiler.js.map +1 -0
- package/dist/src/validators/invariants.d.ts +30 -0
- package/dist/src/validators/invariants.d.ts.map +1 -0
- package/dist/src/validators/invariants.js +164 -0
- package/dist/src/validators/invariants.js.map +1 -0
- package/dist/tests/compiler.test.d.ts +2 -0
- package/dist/tests/compiler.test.d.ts.map +1 -0
- package/dist/tests/compiler.test.js +110 -0
- package/dist/tests/compiler.test.js.map +1 -0
- package/dist/tests/extractor.test.d.ts +2 -0
- package/dist/tests/extractor.test.d.ts.map +1 -0
- package/dist/tests/extractor.test.js +195 -0
- package/dist/tests/extractor.test.js.map +1 -0
- package/dist/tests/fixtures.d.ts +9 -0
- package/dist/tests/fixtures.d.ts.map +1 -0
- package/dist/tests/fixtures.js +20 -0
- package/dist/tests/fixtures.js.map +1 -0
- package/dist/tests/grounding.test.d.ts +2 -0
- package/dist/tests/grounding.test.d.ts.map +1 -0
- package/dist/tests/grounding.test.js +82 -0
- package/dist/tests/grounding.test.js.map +1 -0
- package/dist/tests/multi-domain.test.d.ts +2 -0
- package/dist/tests/multi-domain.test.d.ts.map +1 -0
- package/dist/tests/multi-domain.test.js +135 -0
- package/dist/tests/multi-domain.test.js.map +1 -0
- package/dist/tests/normalizer.test.d.ts +2 -0
- package/dist/tests/normalizer.test.d.ts.map +1 -0
- package/dist/tests/normalizer.test.js +70 -0
- package/dist/tests/normalizer.test.js.map +1 -0
- package/dist/tests/providers.test.d.ts +2 -0
- package/dist/tests/providers.test.d.ts.map +1 -0
- package/dist/tests/providers.test.js +110 -0
- package/dist/tests/providers.test.js.map +1 -0
- package/dist/tests/stitcher.test.d.ts +2 -0
- package/dist/tests/stitcher.test.d.ts.map +1 -0
- package/dist/tests/stitcher.test.js +85 -0
- package/dist/tests/stitcher.test.js.map +1 -0
- package/dist/tests/tiling.test.d.ts +2 -0
- package/dist/tests/tiling.test.d.ts.map +1 -0
- package/dist/tests/tiling.test.js +85 -0
- package/dist/tests/tiling.test.js.map +1 -0
- package/dist/tests/validators.test.d.ts +2 -0
- package/dist/tests/validators.test.d.ts.map +1 -0
- package/dist/tests/validators.test.js +125 -0
- package/dist/tests/validators.test.js.map +1 -0
- package/dist/vitest.config.d.ts +3 -0
- package/dist/vitest.config.d.ts.map +1 -0
- package/dist/vitest.config.js +13 -0
- package/dist/vitest.config.js.map +1 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 docground contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# docground
|
|
2
|
+
|
|
3
|
+
> **Extract with sight. Prove with math.**
|
|
4
|
+
|
|
5
|
+
[](https://github.com/docground/docground/actions/workflows/ci.yml)
|
|
6
|
+
[](https://www.npmjs.com/package/docground)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
|
|
9
|
+
`docground` is a TypeScript SDK that extracts structured data from documents using **multimodal vision-language models**. Define your output as a [Zod](https://zod.dev) schema, pass a PNG, JPEG or a real **PDF**, and receive typed data, per-field visual groundings, extracted tables, and optional invariant checks.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## One-line demo
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import { DocumentExtractor } from "docground";
|
|
18
|
+
|
|
19
|
+
const Invoice = z.object({ invoiceNumber: z.string(), total: z.number() });
|
|
20
|
+
const result = await new DocumentExtractor().extract("invoice.pdf", Invoice, {
|
|
21
|
+
provider: "gemini",
|
|
22
|
+
prove: true,
|
|
23
|
+
invariants: [{ id: "balance", expression: "subtotal + tax === total" }],
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
You get `result.data`, `result.groundings`, `result.tables`, `result.invariants` and `result.provenance`.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Why docground?
|
|
32
|
+
|
|
33
|
+
| Capability | Classic OCR | docground |
|
|
34
|
+
|---|---|---|
|
|
35
|
+
| Schema-guaranteed output | ❌ | ✅ via Zod |
|
|
36
|
+
| Native PDF multimodal input | ❌ | ✅ (Gemini, Anthropic) |
|
|
37
|
+
| Per-field visual grounding | ❌ | ✅ `[ymin, xmin, ymax, xmax]` |
|
|
38
|
+
| Multi-page table stitching | ❌ | ✅ across tiles & pages |
|
|
39
|
+
| Invariant checks / auto-repair | ❌ | ✅ built-in math engine |
|
|
40
|
+
| Provider switch (Gemini/OpenAI/Anthropic) | ❌ | ✅ one-line |
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Architecture
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
┌─────────────┐ ┌──────────────┐ ┌─────────────────┐
|
|
48
|
+
│ Input │ │ Document │ │ VLM Provider │
|
|
49
|
+
│ PNG/JPG/PDF │─────▶│ Extractor │─────▶│ Gemini / OpenAI │
|
|
50
|
+
└─────────────┘ └──────┬───────┘ │ / Anthropic │
|
|
51
|
+
│ └────────┬────────┘
|
|
52
|
+
│ │
|
|
53
|
+
┌─────────────┼─────────────┐ │
|
|
54
|
+
│ │ │ │
|
|
55
|
+
▼ ▼ ▼ │
|
|
56
|
+
┌──────────┐ ┌──────────┐ ┌──────────┐ │
|
|
57
|
+
│ Tiler │ │ Cutter │ │ Invariant│◀────┘
|
|
58
|
+
└──────────┘ │ (sharp) │ │ Engine │
|
|
59
|
+
└──────────┘ └────┬─────┘
|
|
60
|
+
│
|
|
61
|
+
▼
|
|
62
|
+
┌─────────────────┐
|
|
63
|
+
│ Table Stitcher │
|
|
64
|
+
└─────────────────┘
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## Installation
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
npm install docground
|
|
73
|
+
# or
|
|
74
|
+
yarn add docground
|
|
75
|
+
# or
|
|
76
|
+
pnpm add docground
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Quick start
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { z } from "zod";
|
|
85
|
+
import { DocumentExtractor } from "docground";
|
|
86
|
+
|
|
87
|
+
const LabReport = z.object({
|
|
88
|
+
patientName: z.string(),
|
|
89
|
+
biomarkers: z.array(
|
|
90
|
+
z.object({ marker: z.string(), value: z.number(), unit: z.string() })
|
|
91
|
+
),
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const result = await new DocumentExtractor().extract(
|
|
95
|
+
"lab-report.pdf",
|
|
96
|
+
LabReport,
|
|
97
|
+
{ provider: "gemini" }
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
console.log(result.data);
|
|
101
|
+
console.log(result.groundings);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## Public API Reference
|
|
107
|
+
|
|
108
|
+
### `new DocumentExtractor(config?)`
|
|
109
|
+
|
|
110
|
+
Creates an extractor with an optional default provider.
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
new DocumentExtractor({
|
|
114
|
+
provider: "gemini",
|
|
115
|
+
apiKey: process.env.GEMINI_API_KEY,
|
|
116
|
+
model: "gemini-3.1-flash-lite",
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### `extract(input, schema, options?)`
|
|
121
|
+
|
|
122
|
+
Extracts structured data from a file path, `Buffer`, `Uint8Array` or data URI.
|
|
123
|
+
|
|
124
|
+
| Option | Description |
|
|
125
|
+
|---|---|
|
|
126
|
+
| `provider` | `gemini`, `openai`, `anthropic` or `custom` |
|
|
127
|
+
| `model` | Model ID; falls back to provider default |
|
|
128
|
+
| `apiKey` | API key override |
|
|
129
|
+
| `prove` | Enable invariant checks |
|
|
130
|
+
| `invariants` | Array of `{ id, expression, tolerance? }` checks |
|
|
131
|
+
| `autoRepair` | Re-run extraction when invariants fail |
|
|
132
|
+
| `tiling` | `auto`, `off`, or explicit `TileOptions` |
|
|
133
|
+
| `prompt` | Extra instructions for the model |
|
|
134
|
+
| `maxTokens` | Max output tokens |
|
|
135
|
+
| `temperature` | Sampling temperature |
|
|
136
|
+
| `seed` | Deterministic seed |
|
|
137
|
+
| `onProgress` | Progress callback `(p) => void` |
|
|
138
|
+
|
|
139
|
+
### `ExtractionResult<T>`
|
|
140
|
+
|
|
141
|
+
| Field | Description |
|
|
142
|
+
|---|---|
|
|
143
|
+
| `data: T` | Typed payload (parsed with your Zod schema) |
|
|
144
|
+
| `confidence: number` | Global confidence in `[0, 1]` |
|
|
145
|
+
| `fields` | Per-field value + confidence + bbox |
|
|
146
|
+
| `tables` | Extracted and stitched tables |
|
|
147
|
+
| `groundings` | Visual grounding records |
|
|
148
|
+
| `invariants` | Invariant report when `prove: true` |
|
|
149
|
+
| `provenance` | Provider, model, duration, tiles used, repairs |
|
|
150
|
+
|
|
151
|
+
### Invariants
|
|
152
|
+
|
|
153
|
+
Use plain JavaScript expressions with comparisons or aggregates:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
invariants: [
|
|
157
|
+
{ id: "balance", expression: "subtotal + tax === total" },
|
|
158
|
+
{ id: "items_sum", expression: "sum(items.lineTotal) === subtotal" },
|
|
159
|
+
{ id: "vat_positive", expression: "tax > 0" },
|
|
160
|
+
];
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## Supported providers & formats
|
|
166
|
+
|
|
167
|
+
| Provider | Images | PDFs | Notes |
|
|
168
|
+
|---|---|---|---|
|
|
169
|
+
| Gemini | ✅ | ✅ Native | Send `application/pdf` inline |
|
|
170
|
+
| Anthropic | ✅ | ✅ Native | Document block with `application/pdf` |
|
|
171
|
+
| OpenAI | ✅ | ❌ | Convert PDF to images before sending |
|
|
172
|
+
| Custom | ✅ | ✅ | Implement `VLMProvider` interface |
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
## Environment variables
|
|
177
|
+
|
|
178
|
+
Create a `.env` file from `.env.example`:
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
GEMINI_API_KEY=...
|
|
182
|
+
OPENAI_API_KEY=...
|
|
183
|
+
ANTHROPIC_API_KEY=...
|
|
184
|
+
|
|
185
|
+
# Optional model overrides
|
|
186
|
+
GEMINI_MODEL=gemini-3.1-flash-lite
|
|
187
|
+
OPENAI_MODEL=gpt-5.6-luna
|
|
188
|
+
ANTHROPIC_MODEL=claude-opus-5
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## Examples
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
npx tsx examples/01_invoice_with_grounding.ts
|
|
197
|
+
npx tsx examples/02_clinical_lab_report.ts
|
|
198
|
+
npx tsx examples/03_legal_pdf_extraction.ts
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## Development
|
|
204
|
+
|
|
205
|
+
```bash
|
|
206
|
+
npm install
|
|
207
|
+
npm run generate:fixtures
|
|
208
|
+
npm run typecheck
|
|
209
|
+
npm run test
|
|
210
|
+
npm run build
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
## License
|
|
216
|
+
|
|
217
|
+
MIT © DocGround
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* Generic OpenAPI 3.0.3 / JSON Schema object.
|
|
4
|
+
*/
|
|
5
|
+
export type JsonSchema = Record<string, unknown>;
|
|
6
|
+
/**
|
|
7
|
+
* Gemini's `responseSchema` accepts an OpenAPI 3.0.3 compatible JSON Schema.
|
|
8
|
+
* @deprecated alias for JsonSchema; kept for backwards compatibility.
|
|
9
|
+
*/
|
|
10
|
+
export type GeminiResponseSchema = JsonSchema;
|
|
11
|
+
/**
|
|
12
|
+
* Compile a Zod schema to a generic OpenAPI/JSON Schema compatible with
|
|
13
|
+
* OpenAI, Anthropic and the Gemini REST API.
|
|
14
|
+
*/
|
|
15
|
+
export declare function compileZodToJsonSchema(schema: z.ZodType<any>): JsonSchema;
|
|
16
|
+
/**
|
|
17
|
+
* Compile an arbitrary Zod schema into a Gemini-compatible `responseSchema`.
|
|
18
|
+
* Supports primitives, arrays, nested objects, optionals and enums.
|
|
19
|
+
*
|
|
20
|
+
* @param schema - A Zod schema describing the desired response shape.
|
|
21
|
+
* @returns A JSON Schema object ready to be passed to Gemini's responseSchema.
|
|
22
|
+
*/
|
|
23
|
+
export declare function compileZodToGeminiSchema(schema: z.ZodType<any>): JsonSchema;
|
|
24
|
+
/**
|
|
25
|
+
* Convert a generic JSON Schema into the native `@google/genai` `Schema` object.
|
|
26
|
+
* This maps lowercase JSON Schema types to the SDK's `Type` enum values, strips
|
|
27
|
+
* unsupported fields such as `additionalProperties`, and folds `null` branches
|
|
28
|
+
* into `nullable`.
|
|
29
|
+
*/
|
|
30
|
+
export declare function toGeminiSDKSchema(schema: JsonSchema): Record<string, unknown>;
|
|
31
|
+
//# sourceMappingURL=zod-compiler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-compiler.d.ts","sourceRoot":"","sources":["../../../src/compiler/zod-compiler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAEjD;;;GAGG;AACH,MAAM,MAAM,oBAAoB,GAAG,UAAU,CAAC;AAS9C;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,UAAU,CAMzE;AAED;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,UAAU,CAE3E;AA2CD;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAsF7E"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
2
|
+
function cleanJsonSchema(schema) {
|
|
3
|
+
// Remove unsupported keys for strict provider subsets.
|
|
4
|
+
delete schema.$schema;
|
|
5
|
+
delete schema.$id;
|
|
6
|
+
return schema;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Compile a Zod schema to a generic OpenAPI/JSON Schema compatible with
|
|
10
|
+
* OpenAI, Anthropic and the Gemini REST API.
|
|
11
|
+
*/
|
|
12
|
+
export function compileZodToJsonSchema(schema) {
|
|
13
|
+
const jsonSchema = zodToJsonSchema(schema, {
|
|
14
|
+
target: "openApi3",
|
|
15
|
+
$refStrategy: "none",
|
|
16
|
+
});
|
|
17
|
+
return cleanJsonSchema(jsonSchema);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Compile an arbitrary Zod schema into a Gemini-compatible `responseSchema`.
|
|
21
|
+
* Supports primitives, arrays, nested objects, optionals and enums.
|
|
22
|
+
*
|
|
23
|
+
* @param schema - A Zod schema describing the desired response shape.
|
|
24
|
+
* @returns A JSON Schema object ready to be passed to Gemini's responseSchema.
|
|
25
|
+
*/
|
|
26
|
+
export function compileZodToGeminiSchema(schema) {
|
|
27
|
+
return compileZodToJsonSchema(schema);
|
|
28
|
+
}
|
|
29
|
+
const geminiTypeMap = {
|
|
30
|
+
string: "STRING",
|
|
31
|
+
number: "NUMBER",
|
|
32
|
+
integer: "INTEGER",
|
|
33
|
+
boolean: "BOOLEAN",
|
|
34
|
+
array: "ARRAY",
|
|
35
|
+
object: "OBJECT",
|
|
36
|
+
};
|
|
37
|
+
function isSchemaObject(value) {
|
|
38
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
39
|
+
}
|
|
40
|
+
function isNullSchema(value) {
|
|
41
|
+
if (!isSchemaObject(value))
|
|
42
|
+
return false;
|
|
43
|
+
if (value.type === "null")
|
|
44
|
+
return true;
|
|
45
|
+
if (Array.isArray(value.type) && value.type.length === 1 && value.type[0] === "null") {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
function mapType(typeValue) {
|
|
51
|
+
if (typeof typeValue === "string") {
|
|
52
|
+
if (typeValue === "null")
|
|
53
|
+
return {};
|
|
54
|
+
return { type: geminiTypeMap[typeValue] ?? typeValue.toUpperCase() };
|
|
55
|
+
}
|
|
56
|
+
if (Array.isArray(typeValue)) {
|
|
57
|
+
const nonNull = typeValue.filter((t) => t !== "null");
|
|
58
|
+
const result = {};
|
|
59
|
+
if (nonNull.length > 0) {
|
|
60
|
+
result.type = geminiTypeMap[nonNull[0]] ?? nonNull[0].toUpperCase();
|
|
61
|
+
}
|
|
62
|
+
if (typeValue.includes("null")) {
|
|
63
|
+
result.nullable = true;
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
return { type: typeValue };
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Convert a generic JSON Schema into the native `@google/genai` `Schema` object.
|
|
71
|
+
* This maps lowercase JSON Schema types to the SDK's `Type` enum values, strips
|
|
72
|
+
* unsupported fields such as `additionalProperties`, and folds `null` branches
|
|
73
|
+
* into `nullable`.
|
|
74
|
+
*/
|
|
75
|
+
export function toGeminiSDKSchema(schema) {
|
|
76
|
+
if (!isSchemaObject(schema)) {
|
|
77
|
+
return {};
|
|
78
|
+
}
|
|
79
|
+
// Resolve root $ref from definitions if present.
|
|
80
|
+
if (typeof schema.$ref === "string" &&
|
|
81
|
+
isSchemaObject(schema.definitions)) {
|
|
82
|
+
const ref = schema.$ref.replace(/^#\//, "").replace(/^definitions\//, "");
|
|
83
|
+
const definitions = schema.definitions;
|
|
84
|
+
if (definitions && ref in definitions) {
|
|
85
|
+
return toGeminiSDKSchema(definitions[ref]);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// Fold anyOf [A, null] into A with nullable: true.
|
|
89
|
+
if (Array.isArray(schema.anyOf)) {
|
|
90
|
+
const hasNull = schema.anyOf.some(isNullSchema);
|
|
91
|
+
const nonNull = schema.anyOf.filter((s) => !isNullSchema(s));
|
|
92
|
+
if (nonNull.length === 1) {
|
|
93
|
+
const folded = toGeminiSDKSchema(nonNull[0]);
|
|
94
|
+
if (hasNull) {
|
|
95
|
+
folded.nullable = true;
|
|
96
|
+
}
|
|
97
|
+
return folded;
|
|
98
|
+
}
|
|
99
|
+
if (nonNull.length > 1) {
|
|
100
|
+
const out = {
|
|
101
|
+
anyOf: nonNull.map((s) => toGeminiSDKSchema(s)),
|
|
102
|
+
};
|
|
103
|
+
if (hasNull) {
|
|
104
|
+
out.nullable = true;
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
if (hasNull) {
|
|
109
|
+
return { nullable: true };
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const out = {};
|
|
113
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
114
|
+
if (key === "$schema" || key === "$id" || key === "$ref" || key === "additionalProperties") {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (key === "type") {
|
|
118
|
+
const { type, nullable } = mapType(value);
|
|
119
|
+
if (type)
|
|
120
|
+
out.type = type;
|
|
121
|
+
if (nullable)
|
|
122
|
+
out.nullable = true;
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (key === "properties" && isSchemaObject(value)) {
|
|
126
|
+
const mapped = {};
|
|
127
|
+
for (const [k, v] of Object.entries(value)) {
|
|
128
|
+
mapped[k] = toGeminiSDKSchema(v);
|
|
129
|
+
}
|
|
130
|
+
out.properties = mapped;
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (key === "items" && isSchemaObject(value)) {
|
|
134
|
+
out.items = toGeminiSDKSchema(value);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (key === "enum" && Array.isArray(value)) {
|
|
138
|
+
out.enum = value.map((v) => (v === null ? "null" : typeof v === "object" ? JSON.stringify(v) : String(v)));
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (isSchemaObject(value)) {
|
|
142
|
+
out[key] = toGeminiSDKSchema(value);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
out[key] = value;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return out;
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=zod-compiler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-compiler.js","sourceRoot":"","sources":["../../../src/compiler/zod-compiler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAarD,SAAS,eAAe,CAAC,MAA+B;IACtD,uDAAuD;IACvD,OAAO,MAAM,CAAC,OAAO,CAAC;IACtB,OAAO,MAAM,CAAC,GAAG,CAAC;IAClB,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CAAC,MAAsB;IAC3D,MAAM,UAAU,GAAG,eAAe,CAAC,MAAM,EAAE;QACzC,MAAM,EAAE,UAAU;QAClB,YAAY,EAAE,MAAM;KACrB,CAA4B,CAAC;IAC9B,OAAO,eAAe,CAAC,UAAU,CAAC,CAAC;AACrC,CA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nB,CAAC;IACH,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { type ProviderFactoryConfig } from "../providers/factory.js";
|
|
3
|
+
import { type ExtractionOptions, type ExtractionResult } from "./types.js";
|
|
4
|
+
export interface ExtractorConfig extends ProviderFactoryConfig {
|
|
5
|
+
}
|
|
6
|
+
export declare class DocumentExtractor {
|
|
7
|
+
private baseConfig;
|
|
8
|
+
constructor(config?: ExtractorConfig);
|
|
9
|
+
private resolveProvider;
|
|
10
|
+
private ingestPage;
|
|
11
|
+
private getTileOptions;
|
|
12
|
+
private prepareTiles;
|
|
13
|
+
private runInvariants;
|
|
14
|
+
private callProvider;
|
|
15
|
+
extract<T>(input: Buffer | Uint8Array | string, schema: z.ZodType<T>, options?: ExtractionOptions): Promise<ExtractionResult<T>>;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=extractor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extractor.d.ts","sourceRoot":"","sources":["../../../src/core/extractor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAmB,KAAK,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAGtF,OAAO,EACL,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EAKtB,MAAM,YAAY,CAAC;AAOpB,MAAM,WAAW,eAAgB,SAAQ,qBAAqB;CAAG;AAyIjE,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,UAAU,CAAkB;gBAExB,MAAM,GAAE,eAAoB;IAIxC,OAAO,CAAC,eAAe;YAWT,UAAU;IAqBxB,OAAO,CAAC,cAAc;YAQR,YAAY;IAoB1B,OAAO,CAAC,aAAa;YAcP,YAAY;IAmBpB,OAAO,CAAC,CAAC,EACb,KAAK,EAAE,MAAM,GAAG,UAAU,GAAG,MAAM,EACnC,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,GAAE,iBAAsB,GAC9B,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC;CAkEhC"}
|