samsar-js 0.48.0 → 0.48.2
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/README.md +47 -1
- package/dist/index.d.ts +106 -0
- package/dist/index.js +76 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,9 +4,22 @@ TypeScript/ESM client for the Samsar Processor public API (`https://api.samsar.o
|
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
+
Requires Node.js `>=18`.
|
|
8
|
+
|
|
7
9
|
```bash
|
|
8
10
|
npm install samsar-js
|
|
9
|
-
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
yarn add samsar-js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add samsar-js
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# optional: install from a private registry mirror
|
|
10
23
|
npm install samsar-js --registry <your-registry-url>
|
|
11
24
|
```
|
|
12
25
|
|
|
@@ -142,6 +155,23 @@ const images = await samsar.extendImageList({
|
|
|
142
155
|
num_images: 4,
|
|
143
156
|
});
|
|
144
157
|
|
|
158
|
+
// Create a reusable receipt template from one sample receipt image (free endpoint)
|
|
159
|
+
const receiptTemplate = await samsar.createReceiptTemplate({
|
|
160
|
+
image_url: 'https://example.com/receipt-template.png',
|
|
161
|
+
template_name: 'kbank-transfer-template',
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// Fetch the structured template JSON for a saved template (free endpoint)
|
|
165
|
+
const receiptTemplateJson = await samsar.getReceiptTemplateJson(receiptTemplate.data.template_id);
|
|
166
|
+
console.log(receiptTemplateJson.data.template_json);
|
|
167
|
+
|
|
168
|
+
// Query a new receipt against the saved template (50 credits/request)
|
|
169
|
+
const receiptResult = await samsar.queryReceiptTemplate({
|
|
170
|
+
image_url: 'https://example.com/receipt-instance.png',
|
|
171
|
+
template_id: receiptTemplate.data.template_id,
|
|
172
|
+
});
|
|
173
|
+
console.log(receiptResult.data.receipt_json, receiptResult.creditsCharged);
|
|
174
|
+
|
|
145
175
|
// Enhance low-res images (if needed) and generate a roll-up banner
|
|
146
176
|
const rollup = await samsar.enhanceAndGenerateRollupBanner({
|
|
147
177
|
images: [
|
|
@@ -195,6 +225,7 @@ console.log(paymentStatus.data.status);
|
|
|
195
225
|
```
|
|
196
226
|
|
|
197
227
|
Video model support notes:
|
|
228
|
+
- `createVideoFromText` image model keys include: `GPTIMAGE1`, `IMAGEN4`, `SEEDREAM`, `HUNYUAN`, `NANOBANANA2`.
|
|
198
229
|
- `createVideoFromText` supports all express video models: `RUNWAYML`, `KLINGIMGTOVID3PRO`, `HAILUO`, `HAILUOPRO`, `SEEDANCEI2V`, `VEO3.1I2V`, `VEO3.1I2VFAST`, `SORA2`, `SORA2PRO`.
|
|
199
230
|
- `createVideoFromImageList` uses a fixed Veo2.1 pipeline model (`VEO3.1I2V`) and does not accept a `video_model` override.
|
|
200
231
|
|
|
@@ -204,6 +235,7 @@ Each method returns `{ data, status, headers, creditsCharged, creditsRemaining,
|
|
|
204
235
|
|
|
205
236
|
- Embedding endpoints (`createEmbedding`, `updateEmbedding`, `searchAgainstEmbedding`, `similarToEmbedding`) are billed by input tokens at $1 per million tokens. `deleteEmbeddings` does not consume tokens.
|
|
206
237
|
- Token counts follow OpenAI tokenization for `text-embedding-3-large`. Credit deductions follow the existing 100 credits per USD rule.
|
|
238
|
+
- Receipt template creation (`createReceiptTemplate`) and template JSON lookup (`getReceiptTemplateJson`) are free; receipt template query (`queryReceiptTemplate`) costs 50 credits per request.
|
|
207
239
|
|
|
208
240
|
## Configuration
|
|
209
241
|
|
|
@@ -231,3 +263,17 @@ npm publish --registry <your-registry-url>
|
|
|
231
263
|
```
|
|
232
264
|
|
|
233
265
|
Ensure `package.json` name/version are set as desired before publishing.
|
|
266
|
+
|
|
267
|
+
## Maintainer Deploy Script
|
|
268
|
+
|
|
269
|
+
`deploy.sh` publishes the current package to npm using `NPM_TOKEN` from env (or `.env`).
|
|
270
|
+
|
|
271
|
+
```bash
|
|
272
|
+
# publish current version
|
|
273
|
+
./deploy.sh
|
|
274
|
+
|
|
275
|
+
# bump version, then publish
|
|
276
|
+
./deploy.sh patch
|
|
277
|
+
./deploy.sh minor
|
|
278
|
+
./deploy.sh major
|
|
279
|
+
```
|
package/dist/index.d.ts
CHANGED
|
@@ -534,6 +534,98 @@ export interface ExtendImageListResponse {
|
|
|
534
534
|
remainingCredits?: number;
|
|
535
535
|
[key: string]: unknown;
|
|
536
536
|
}
|
|
537
|
+
export interface ReceiptTemplateRoi {
|
|
538
|
+
id: string;
|
|
539
|
+
label: string;
|
|
540
|
+
purpose?: string | null;
|
|
541
|
+
left: number;
|
|
542
|
+
top: number;
|
|
543
|
+
width: number;
|
|
544
|
+
height: number;
|
|
545
|
+
}
|
|
546
|
+
export interface ReceiptTemplateField {
|
|
547
|
+
key: string;
|
|
548
|
+
label: string;
|
|
549
|
+
type: string;
|
|
550
|
+
required: boolean;
|
|
551
|
+
roi_id?: string | null;
|
|
552
|
+
description?: string | null;
|
|
553
|
+
}
|
|
554
|
+
export interface ReceiptTemplateDefinition {
|
|
555
|
+
schema_version?: string;
|
|
556
|
+
merchant_hint?: string | null;
|
|
557
|
+
language_hint?: string | null;
|
|
558
|
+
currency_hint?: string | null;
|
|
559
|
+
rois?: ReceiptTemplateRoi[];
|
|
560
|
+
fields?: ReceiptTemplateField[];
|
|
561
|
+
validation_rules?: Record<string, unknown>;
|
|
562
|
+
[key: string]: unknown;
|
|
563
|
+
}
|
|
564
|
+
export interface CreateReceiptTemplateRequest {
|
|
565
|
+
image_url?: string;
|
|
566
|
+
imageUrl?: string;
|
|
567
|
+
receipt_url?: string;
|
|
568
|
+
receiptUrl?: string;
|
|
569
|
+
template_url?: string;
|
|
570
|
+
templateUrl?: string;
|
|
571
|
+
template_name?: string;
|
|
572
|
+
templateName?: string;
|
|
573
|
+
}
|
|
574
|
+
export interface CreateReceiptTemplateResponse {
|
|
575
|
+
template_id: string;
|
|
576
|
+
template_hash?: string;
|
|
577
|
+
template_name?: string | null;
|
|
578
|
+
normalized_template?: ReceiptTemplateDefinition;
|
|
579
|
+
sample_receipt?: Record<string, unknown>;
|
|
580
|
+
created_at?: string;
|
|
581
|
+
[key: string]: unknown;
|
|
582
|
+
}
|
|
583
|
+
export interface QueryReceiptTemplateRequest {
|
|
584
|
+
image_url?: string;
|
|
585
|
+
imageUrl?: string;
|
|
586
|
+
receipt_url?: string;
|
|
587
|
+
receiptUrl?: string;
|
|
588
|
+
template_id?: string;
|
|
589
|
+
templateId?: string;
|
|
590
|
+
receipt_template_id?: string;
|
|
591
|
+
receiptTemplateId?: string;
|
|
592
|
+
}
|
|
593
|
+
export interface QueryReceiptTemplateResponse {
|
|
594
|
+
template_id: string;
|
|
595
|
+
template_hash?: string;
|
|
596
|
+
template_name?: string | null;
|
|
597
|
+
normalized_template?: ReceiptTemplateDefinition;
|
|
598
|
+
receipt_json?: Record<string, unknown>;
|
|
599
|
+
standardized_receipt?: Record<string, unknown>;
|
|
600
|
+
items?: Array<Record<string, unknown>>;
|
|
601
|
+
unreadable_fields?: string[];
|
|
602
|
+
confidence?: number;
|
|
603
|
+
validation?: {
|
|
604
|
+
is_valid?: boolean;
|
|
605
|
+
issues?: string[];
|
|
606
|
+
missing_required_fields?: string[];
|
|
607
|
+
type_mismatch_fields?: string[];
|
|
608
|
+
arithmetic?: Record<string, unknown>;
|
|
609
|
+
[key: string]: unknown;
|
|
610
|
+
};
|
|
611
|
+
attempts?: number;
|
|
612
|
+
creditsCharged?: number;
|
|
613
|
+
remainingCredits?: number | null;
|
|
614
|
+
[key: string]: unknown;
|
|
615
|
+
}
|
|
616
|
+
export interface GetReceiptTemplateJsonResponse {
|
|
617
|
+
template_id: string;
|
|
618
|
+
template_hash?: string;
|
|
619
|
+
template_name?: string | null;
|
|
620
|
+
source_image_url?: string | null;
|
|
621
|
+
normalized_template?: ReceiptTemplateDefinition;
|
|
622
|
+
template_json?: ReceiptTemplateDefinition;
|
|
623
|
+
sample_receipt?: Record<string, unknown>;
|
|
624
|
+
provider?: Record<string, unknown> | null;
|
|
625
|
+
created_at?: string | null;
|
|
626
|
+
updated_at?: string | null;
|
|
627
|
+
[key: string]: unknown;
|
|
628
|
+
}
|
|
537
629
|
export interface RollupBannerOverlay {
|
|
538
630
|
footer?: string | null;
|
|
539
631
|
bottom?: string | null;
|
|
@@ -919,6 +1011,20 @@ export declare class SamsarClient {
|
|
|
919
1011
|
* Add or extend a saved image list for the authenticated API key.
|
|
920
1012
|
*/
|
|
921
1013
|
extendImageList(payload: ExtendImageListRequest, options?: SamsarRequestOptions): Promise<SamsarResult<ExtendImageListResponse>>;
|
|
1014
|
+
/**
|
|
1015
|
+
* Create and save a reusable receipt extraction template from a sample receipt image.
|
|
1016
|
+
* This endpoint is free and does not deduct credits.
|
|
1017
|
+
*/
|
|
1018
|
+
createReceiptTemplate(payload: CreateReceiptTemplateRequest, options?: SamsarRequestOptions): Promise<SamsarResult<CreateReceiptTemplateResponse>>;
|
|
1019
|
+
/**
|
|
1020
|
+
* Extract standardized receipt JSON by validating a receipt image against a saved template.
|
|
1021
|
+
* This endpoint charges 50 credits per request.
|
|
1022
|
+
*/
|
|
1023
|
+
queryReceiptTemplate(payload: QueryReceiptTemplateRequest, options?: SamsarRequestOptions): Promise<SamsarResult<QueryReceiptTemplateResponse>>;
|
|
1024
|
+
/**
|
|
1025
|
+
* Fetch the structured template JSON for a template id that belongs to the authenticated API key.
|
|
1026
|
+
*/
|
|
1027
|
+
getReceiptTemplateJson(templateId: string, options?: SamsarRequestOptions): Promise<SamsarResult<GetReceiptTemplateJsonResponse>>;
|
|
922
1028
|
/**
|
|
923
1029
|
* Create a roll-up banner from preprocessed (already enhanced) images.
|
|
924
1030
|
*/
|
package/dist/index.js
CHANGED
|
@@ -590,6 +590,82 @@ export class SamsarClient {
|
|
|
590
590
|
async extendImageList(payload, options) {
|
|
591
591
|
return this.post('image/add_image_set', payload, options);
|
|
592
592
|
}
|
|
593
|
+
/**
|
|
594
|
+
* Create and save a reusable receipt extraction template from a sample receipt image.
|
|
595
|
+
* This endpoint is free and does not deduct credits.
|
|
596
|
+
*/
|
|
597
|
+
async createReceiptTemplate(payload, options) {
|
|
598
|
+
const raw = payload;
|
|
599
|
+
const imageUrl = (typeof raw.image_url === 'string' && raw.image_url.trim()) ||
|
|
600
|
+
(typeof raw.imageUrl === 'string' && raw.imageUrl.trim()) ||
|
|
601
|
+
(typeof raw.receipt_url === 'string' && raw.receipt_url.trim()) ||
|
|
602
|
+
(typeof raw.receiptUrl === 'string' && raw.receiptUrl.trim()) ||
|
|
603
|
+
(typeof raw.template_url === 'string' && raw.template_url.trim()) ||
|
|
604
|
+
(typeof raw.templateUrl === 'string' && raw.templateUrl.trim()) ||
|
|
605
|
+
null;
|
|
606
|
+
if (!imageUrl) {
|
|
607
|
+
throw new Error('image_url (or receipt_url/template_url) is required');
|
|
608
|
+
}
|
|
609
|
+
const templateName = (typeof raw.template_name === 'string' && raw.template_name.trim()) ||
|
|
610
|
+
(typeof raw.templateName === 'string' && raw.templateName.trim()) ||
|
|
611
|
+
undefined;
|
|
612
|
+
const requestPayload = {
|
|
613
|
+
...payload,
|
|
614
|
+
image_url: imageUrl,
|
|
615
|
+
receipt_url: imageUrl,
|
|
616
|
+
template_url: imageUrl,
|
|
617
|
+
...(templateName ? { template_name: templateName } : {}),
|
|
618
|
+
};
|
|
619
|
+
return this.post('image/receipt_templates/create', requestPayload, options);
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Extract standardized receipt JSON by validating a receipt image against a saved template.
|
|
623
|
+
* This endpoint charges 50 credits per request.
|
|
624
|
+
*/
|
|
625
|
+
async queryReceiptTemplate(payload, options) {
|
|
626
|
+
const raw = payload;
|
|
627
|
+
const imageUrl = (typeof raw.image_url === 'string' && raw.image_url.trim()) ||
|
|
628
|
+
(typeof raw.imageUrl === 'string' && raw.imageUrl.trim()) ||
|
|
629
|
+
(typeof raw.receipt_url === 'string' && raw.receipt_url.trim()) ||
|
|
630
|
+
(typeof raw.receiptUrl === 'string' && raw.receiptUrl.trim()) ||
|
|
631
|
+
null;
|
|
632
|
+
if (!imageUrl) {
|
|
633
|
+
throw new Error('image_url (or receipt_url) is required');
|
|
634
|
+
}
|
|
635
|
+
const templateId = (typeof raw.template_id === 'string' && raw.template_id.trim()) ||
|
|
636
|
+
(typeof raw.templateId === 'string' && raw.templateId.trim()) ||
|
|
637
|
+
(typeof raw.receipt_template_id === 'string' && raw.receipt_template_id.trim()) ||
|
|
638
|
+
(typeof raw.receiptTemplateId === 'string' && raw.receiptTemplateId.trim()) ||
|
|
639
|
+
null;
|
|
640
|
+
if (!templateId) {
|
|
641
|
+
throw new Error('template_id is required');
|
|
642
|
+
}
|
|
643
|
+
const requestPayload = {
|
|
644
|
+
...payload,
|
|
645
|
+
image_url: imageUrl,
|
|
646
|
+
receipt_url: imageUrl,
|
|
647
|
+
template_id: templateId,
|
|
648
|
+
receipt_template_id: templateId,
|
|
649
|
+
};
|
|
650
|
+
return this.post('image/receipt_templates/query', requestPayload, options);
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* Fetch the structured template JSON for a template id that belongs to the authenticated API key.
|
|
654
|
+
*/
|
|
655
|
+
async getReceiptTemplateJson(templateId, options) {
|
|
656
|
+
const normalizedTemplateId = typeof templateId === 'string' ? templateId.trim() : '';
|
|
657
|
+
if (!normalizedTemplateId) {
|
|
658
|
+
throw new Error('templateId is required');
|
|
659
|
+
}
|
|
660
|
+
const query = {
|
|
661
|
+
...(options?.query ?? {}),
|
|
662
|
+
template_id: normalizedTemplateId,
|
|
663
|
+
};
|
|
664
|
+
return this.get('image/template_json', {
|
|
665
|
+
...(options ?? {}),
|
|
666
|
+
query,
|
|
667
|
+
});
|
|
668
|
+
}
|
|
593
669
|
/**
|
|
594
670
|
* Create a roll-up banner from preprocessed (already enhanced) images.
|
|
595
671
|
*/
|