@gemina/sdk 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 +209 -0
- package/dist/index.cjs +10048 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +9456 -0
- package/dist/index.d.ts +9456 -0
- package/dist/index.js +9356 -0
- package/dist/index.js.map +1 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gemina
|
|
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,209 @@
|
|
|
1
|
+
# Gemina TypeScript SDK
|
|
2
|
+
|
|
3
|
+
The official TypeScript/JavaScript client for the [Gemina API](https://gemina.co) — invoice OCR and document intelligence. Upload invoices and financial documents, get typed structured data back, then search, aggregate, and chat over everything you've processed. Works in Node.js (18+) and the browser, with zero runtime dependencies (native `fetch`).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i @gemina/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Requires Node.js >= 18 (or any modern browser). Ships ESM and CommonJS builds with full type declarations.
|
|
12
|
+
|
|
13
|
+
## Authenticate
|
|
14
|
+
|
|
15
|
+
Get an API key from the [Gemina Console](https://console.gemina.co). The client sends it as the `X-API-Key` header on every request:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { GeminaClient } from "@gemina/sdk";
|
|
19
|
+
|
|
20
|
+
const client = new GeminaClient(process.env.GEMINA_API_KEY!);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Never ship the API key in browser or mobile code. For browser embedding, mint short-lived session tokens server-side (`POST /v1/sessions/token`) and use `GeminaClient.withSessionToken(...)` — see [Session tokens](#session-tokens-browser-embedding) below and the Document Intelligence guide in the [docs](https://console.gemina.co/docs).
|
|
24
|
+
|
|
25
|
+
## Quickstart — process an invoice in one call
|
|
26
|
+
|
|
27
|
+
`processDocument` submits the document through the async endpoint, polls with exponential backoff until processing completes, and returns the final typed result — one call, no manual polling:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { readFile } from "node:fs/promises";
|
|
31
|
+
import { GeminaClient } from "@gemina/sdk";
|
|
32
|
+
|
|
33
|
+
const client = new GeminaClient(process.env.GEMINA_API_KEY!);
|
|
34
|
+
|
|
35
|
+
// Node: wrap a Buffer in a Blob. In the browser, pass a File from an
|
|
36
|
+
// <input type="file"> directly.
|
|
37
|
+
const buf = await readFile("./invoice.png");
|
|
38
|
+
const result = await client.processDocument(
|
|
39
|
+
new Blob([buf], { type: "image/png" }),
|
|
40
|
+
["invoice_headers"],
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const values = result.data?.extractions?.[0]?.values;
|
|
44
|
+
console.log("supplier:", values?.vendorName?.value);
|
|
45
|
+
console.log("total:", values?.totalAmount?.value);
|
|
46
|
+
console.log("date:", values?.invoiceDate?.value);
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Processing a document by URL works the same way — pass `{ url }` instead of a file:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
const result = await client.processDocument(
|
|
53
|
+
{ url: "https://example.com/invoice.pdf" },
|
|
54
|
+
["invoice_headers"],
|
|
55
|
+
);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## What you get back
|
|
59
|
+
|
|
60
|
+
`processDocument` resolves to a `DocumentProcessingResultOutDTO`:
|
|
61
|
+
|
|
62
|
+
- `status` — `"success" | "partial" | "empty" | "failed"`. `partial` and `empty` still resolve (they carry usable data/meta); `failed` throws a `GeminaProcessingError` instead.
|
|
63
|
+
- `data.extractions` — one entry per requested extraction type. Each entry has `meta.extractionType`, its own `status`, and `values` — the extracted payload. For `invoice_headers`, `values` fields (e.g. `vendorName`, `invoiceNumber`, `invoiceDate`, `totalAmount`, `currency`) are each `{ value, coordinates, confidence }` or `null` when not found.
|
|
64
|
+
- `meta.documentId` — the stored document's ID (use it with `client.documents`).
|
|
65
|
+
- `meta.correlationId` — the async processing correlation ID.
|
|
66
|
+
|
|
67
|
+
Extraction types (`ExtractionTypeModel`):
|
|
68
|
+
|
|
69
|
+
| Type | What it extracts |
|
|
70
|
+
|------|------------------|
|
|
71
|
+
| `ocr` | Raw OCR text |
|
|
72
|
+
| `invoice_headers` | Invoice header fields (vendor, buyer, dates, amounts, taxes) |
|
|
73
|
+
| `invoice_line_items` | Invoice line items |
|
|
74
|
+
| `document_details_hebrew` | Hebrew document details |
|
|
75
|
+
| `document_line_items_hebrew` | Hebrew document line items |
|
|
76
|
+
| `custom_template` | Fields defined by your custom template (pass `templateId`) |
|
|
77
|
+
| `filetag` | File classification metadata (FileTag) |
|
|
78
|
+
|
|
79
|
+
## Search & aggregate your documents
|
|
80
|
+
|
|
81
|
+
Everything you process is indexed for retrieval. Use the `retrieval` group to query with natural language plus structured filters — results carry `documentId` / `documentExtractionId` citations back to the underlying documents:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
const { items, meta } = await client.retrieval.retrievalQuery({
|
|
85
|
+
retrievalQueryInDTO: {
|
|
86
|
+
text: "cleaning services invoices from August",
|
|
87
|
+
filters: { totalAmountMin: 1000, currency: "ILS" },
|
|
88
|
+
limit: 10,
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
for (const item of items ?? []) {
|
|
93
|
+
console.log(item.vendorName, item.totalAmount, item.issueDate, item.documentId);
|
|
94
|
+
}
|
|
95
|
+
console.log(`${meta.count} matches (mode: ${meta.mode})`);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Aggregate across your documents with metrics and group-by:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
const { rows } = await client.retrieval.retrievalAggregate({
|
|
102
|
+
retrievalAggregateInDTO: {
|
|
103
|
+
metrics: [{ op: "sum", field: "total_amount" }, { op: "count" }],
|
|
104
|
+
groupBy: ["vendor_name"],
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
for (const row of rows ?? []) {
|
|
109
|
+
console.log(row.group, row.values);
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Check how many of your documents are indexed with `client.retrieval.retrievalStatus()` (returns `{ indexedDocuments }`).
|
|
114
|
+
|
|
115
|
+
## Chat with your documents
|
|
116
|
+
|
|
117
|
+
Ask free-form questions over everything you've processed. Answers come back with a confidence signal and citations to the source documents:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
const reply = await client.chat.chatQuery({
|
|
121
|
+
chatQueryInDTO: { message: "How much did we spend on cleaning in 2020?" },
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
console.log(reply.answer);
|
|
125
|
+
console.log("confident:", reply.confident);
|
|
126
|
+
console.log("citations:", reply.citations);
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Chat requires a plan with Document Intelligence enabled — see [pricing](https://gemina.co/pricing). Without it the API responds `402`/`403`.
|
|
130
|
+
|
|
131
|
+
## Session tokens (browser embedding)
|
|
132
|
+
|
|
133
|
+
To embed search or chat in your own frontend, mint a short-lived session token **server-side** and hand that to the browser — never the API key:
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
// Server-side (holds the API key)
|
|
137
|
+
const session = await client.sessions.mintRetrievalToken({
|
|
138
|
+
sessionTokenInDTO: { endUserId: "user-42", ttlSeconds: 900 },
|
|
139
|
+
});
|
|
140
|
+
// -> { token, expiresAt, expiresIn, tokenType }
|
|
141
|
+
|
|
142
|
+
// Browser (token only)
|
|
143
|
+
import { GeminaClient } from "@gemina/sdk";
|
|
144
|
+
const browserClient = GeminaClient.withSessionToken(session.token);
|
|
145
|
+
const results = await browserClient.retrieval.retrievalQuery({
|
|
146
|
+
retrievalQueryInDTO: { text: "last month's invoices" },
|
|
147
|
+
});
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
For a drop-in chat UI, see `@gemina/elements` on npm.
|
|
151
|
+
|
|
152
|
+
## Going deeper
|
|
153
|
+
|
|
154
|
+
**Full API surface.** The convenience layer sits on top of a complete generated client. Every API group is available on the client as a lazy accessor: `documents`, `retrieval`, `chat`, `templates`, `files`, `fileTag`, `sessions`, `subscriptions`, `billing`. For example, list your stored documents:
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
const view = await client.documents.findDocuments({ limit: 10 });
|
|
158
|
+
for (const doc of view.data?.documents ?? []) {
|
|
159
|
+
console.log(doc.meta.documentId, doc.meta.filename, doc.meta.createdAt);
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
**Polling knobs.** `processDocument` accepts `timeoutSeconds` (default 300), `initialIntervalSeconds` (default 2, grows ×1.5 per attempt), and `maxIntervalSeconds` (default 15). Transient poll errors (connection blips, 5xx) are retried automatically on the same schedule — up to 3 in a row — since the document is already submitted. On timeout it throws `GeminaTimeoutError` carrying the `correlationId`, so you can resume polling yourself:
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
import { GeminaTimeoutError } from "@gemina/sdk";
|
|
167
|
+
|
|
168
|
+
try {
|
|
169
|
+
await client.processDocument(file, ["invoice_headers"], { timeoutSeconds: 60 });
|
|
170
|
+
} catch (err) {
|
|
171
|
+
if (err instanceof GeminaTimeoutError) {
|
|
172
|
+
// Still processing — poll later with the correlation ID:
|
|
173
|
+
const result = await client.documents.getDocumentProcessingResultByCorrelationId({
|
|
174
|
+
correlationId: err.correlationId,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
**Error handling.** A terminal `failed` status throws `GeminaProcessingError` — its `result.errors` lists the failure details. Transport/HTTP errors from the generated client (`ResponseError`, `FetchError`) pass through unwrapped:
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
import { GeminaProcessingError, ResponseError } from "@gemina/sdk";
|
|
184
|
+
|
|
185
|
+
try {
|
|
186
|
+
const result = await client.processDocument(file, ["invoice_headers"]);
|
|
187
|
+
} catch (err) {
|
|
188
|
+
if (err instanceof GeminaProcessingError) {
|
|
189
|
+
console.error("processing failed:", err.result.errors);
|
|
190
|
+
} else if (err instanceof ResponseError) {
|
|
191
|
+
console.error("HTTP error:", err.response.status);
|
|
192
|
+
} else {
|
|
193
|
+
throw err;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
**Custom base URL.** Point the client at a staging or self-hosted deployment via the second constructor argument:
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
const staging = new GeminaClient(apiKey, "https://api.staging.gemina.co");
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Requirements & support
|
|
205
|
+
|
|
206
|
+
- Node.js >= 18, or any browser with `fetch` (the SDK has zero runtime dependencies).
|
|
207
|
+
- Docs: https://console.gemina.co/docs
|
|
208
|
+
- Issues: https://github.com/tommyil/gemina-sdk/issues
|
|
209
|
+
- Email: support@gemina.co
|