@albatrossai/albatross-sdk 1.1.41 → 1.1.42
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/dist/index.cjs +68 -47
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +37 -49
- package/dist/index.d.ts +37 -49
- package/dist/index.js +68 -47
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -7,12 +7,8 @@ var zod = require('zod');
|
|
|
7
7
|
var MAX_RETRIES = 3;
|
|
8
8
|
var BASE_DELAY_MS = 1e3;
|
|
9
9
|
var REQUEST_TIMEOUT_MS = 3e4;
|
|
10
|
-
var isRetryableError = (status) =>
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
var getRetryDelay = (attempt) => {
|
|
14
|
-
return Math.pow(2, attempt) * BASE_DELAY_MS;
|
|
15
|
-
};
|
|
10
|
+
var isRetryableError = (status) => status >= 500 && status <= 599;
|
|
11
|
+
var getRetryDelay = (attempt) => 2 ** attempt * BASE_DELAY_MS;
|
|
16
12
|
var makeRequest = async ({
|
|
17
13
|
url,
|
|
18
14
|
method = "GET",
|
|
@@ -21,7 +17,7 @@ var makeRequest = async ({
|
|
|
21
17
|
}) => {
|
|
22
18
|
const body = data ? JSON.stringify(data) : void 0;
|
|
23
19
|
let lastError = null;
|
|
24
|
-
for (let attempt = 0; attempt <= MAX_RETRIES; attempt
|
|
20
|
+
for (let attempt = 0; attempt <= MAX_RETRIES; attempt += 1) {
|
|
25
21
|
const controller = new AbortController();
|
|
26
22
|
const timeoutId = setTimeout(
|
|
27
23
|
() => controller.abort(),
|
|
@@ -51,7 +47,8 @@ var makeRequest = async ({
|
|
|
51
47
|
continue;
|
|
52
48
|
}
|
|
53
49
|
throw new Error(
|
|
54
|
-
`Request failed after ${MAX_RETRIES} retries: ${response.status} ${response.statusText} ${errorText}
|
|
50
|
+
`Request failed after ${MAX_RETRIES} retries: ${response.status} ${response.statusText} ${errorText}`,
|
|
51
|
+
{ cause: error }
|
|
55
52
|
);
|
|
56
53
|
}
|
|
57
54
|
return response.json();
|
|
@@ -70,7 +67,8 @@ var makeRequest = async ({
|
|
|
70
67
|
continue;
|
|
71
68
|
}
|
|
72
69
|
throw new Error(
|
|
73
|
-
`Request timeout after ${REQUEST_TIMEOUT_MS}ms (${MAX_RETRIES} retries)
|
|
70
|
+
`Request timeout after ${REQUEST_TIMEOUT_MS}ms (${MAX_RETRIES} retries)`,
|
|
71
|
+
{ cause: error }
|
|
74
72
|
);
|
|
75
73
|
}
|
|
76
74
|
if (error.message.includes("fetch") || error.message.includes("network") || error.message.includes("Failed to fetch")) {
|
|
@@ -82,7 +80,8 @@ var makeRequest = async ({
|
|
|
82
80
|
continue;
|
|
83
81
|
}
|
|
84
82
|
throw new Error(
|
|
85
|
-
`Network error after ${MAX_RETRIES} retries: ${error.message}
|
|
83
|
+
`Network error after ${MAX_RETRIES} retries: ${error.message}`,
|
|
84
|
+
{ cause: error }
|
|
86
85
|
);
|
|
87
86
|
}
|
|
88
87
|
}
|
|
@@ -91,29 +90,29 @@ var makeRequest = async ({
|
|
|
91
90
|
}
|
|
92
91
|
throw lastError || new Error("Unexpected error in makeRequest");
|
|
93
92
|
};
|
|
94
|
-
|
|
93
|
+
var flattenNested = (obj) => {
|
|
95
94
|
const result = {};
|
|
96
|
-
|
|
95
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
97
96
|
if (typeof value === "object" && !Array.isArray(value) && value !== null) {
|
|
98
|
-
|
|
97
|
+
for (const [nestedKey, nestedValue] of Object.entries(value)) {
|
|
99
98
|
if (Array.isArray(nestedValue)) {
|
|
100
99
|
result[`${key}_${nestedKey}`] = nestedValue.join(",");
|
|
101
100
|
} else if (typeof nestedValue === "object" && nestedValue !== null) {
|
|
102
|
-
|
|
101
|
+
for (const [subKey, subValue] of Object.entries(nestedValue)) {
|
|
103
102
|
result[`${nestedKey}_${subKey}`] = subValue;
|
|
104
|
-
}
|
|
103
|
+
}
|
|
105
104
|
} else {
|
|
106
105
|
result[`${key}_${nestedKey}`] = nestedValue;
|
|
107
106
|
}
|
|
108
|
-
}
|
|
107
|
+
}
|
|
109
108
|
} else if (Array.isArray(value)) {
|
|
110
109
|
result[key] = value.join(",");
|
|
111
110
|
} else {
|
|
112
111
|
result[key] = value;
|
|
113
112
|
}
|
|
114
|
-
}
|
|
113
|
+
}
|
|
115
114
|
return result;
|
|
116
|
-
}
|
|
115
|
+
};
|
|
117
116
|
var preFormatRow = (rowHeaders, row) => {
|
|
118
117
|
const flattenedRow = flattenNested(row);
|
|
119
118
|
return rowHeaders.map((header) => {
|
|
@@ -131,7 +130,7 @@ var bodyToCSV = (data) => {
|
|
|
131
130
|
if (data.length === 0) {
|
|
132
131
|
throw new Error("data is empty");
|
|
133
132
|
}
|
|
134
|
-
const row0 = data
|
|
133
|
+
const [row0] = data;
|
|
135
134
|
if (!row0) {
|
|
136
135
|
throw new Error("data is empty");
|
|
137
136
|
}
|
|
@@ -318,7 +317,12 @@ var FeedbackParamsSchema = zod.z.object({
|
|
|
318
317
|
|
|
319
318
|
// src/client.ts
|
|
320
319
|
var hostDefault = "https://app.usealbatross.ai/api";
|
|
321
|
-
var Client = class {
|
|
320
|
+
var Client = class _Client {
|
|
321
|
+
token;
|
|
322
|
+
tenantId;
|
|
323
|
+
baseUrl;
|
|
324
|
+
makeRequest;
|
|
325
|
+
predictionPreProcessing;
|
|
322
326
|
/**
|
|
323
327
|
* create new albatross sdk client instance
|
|
324
328
|
*
|
|
@@ -356,11 +360,6 @@ var Client = class {
|
|
|
356
360
|
this.makeRequest = options.makeRequest || makeRequest;
|
|
357
361
|
this.predictionPreProcessing = options.predictionPreProcessing || ((data) => data);
|
|
358
362
|
}
|
|
359
|
-
token;
|
|
360
|
-
tenantId;
|
|
361
|
-
baseUrl;
|
|
362
|
-
makeRequest;
|
|
363
|
-
predictionPreProcessing;
|
|
364
363
|
/**
|
|
365
364
|
* generate http headers based on endpoint type
|
|
366
365
|
*
|
|
@@ -374,7 +373,7 @@ var Client = class {
|
|
|
374
373
|
getHeaders(endpointType) {
|
|
375
374
|
const headers = {
|
|
376
375
|
"content-type": "application/json",
|
|
377
|
-
Authorization:
|
|
376
|
+
Authorization: `Bearer ${this.token}`
|
|
378
377
|
};
|
|
379
378
|
if (endpointType === "robin") {
|
|
380
379
|
headers["X-Tenant-Id"] = this.tenantId;
|
|
@@ -393,7 +392,7 @@ var Client = class {
|
|
|
393
392
|
* @throws {Error} if image exceeds 50mb (52428800 bytes)
|
|
394
393
|
* @private
|
|
395
394
|
*/
|
|
396
|
-
validateImageSize(base64Image) {
|
|
395
|
+
static validateImageSize(base64Image) {
|
|
397
396
|
const MAX_SIZE = 52428800;
|
|
398
397
|
if (base64Image.length > MAX_SIZE) {
|
|
399
398
|
throw new Error(
|
|
@@ -436,7 +435,7 @@ var Client = class {
|
|
|
436
435
|
async contentSelection(request) {
|
|
437
436
|
ContentSelectionRequestSchema.parse(request);
|
|
438
437
|
if (request.options?.image) {
|
|
439
|
-
|
|
438
|
+
_Client.validateImageSize(request.options.image);
|
|
440
439
|
}
|
|
441
440
|
const url = `${this.baseUrl}/prediction/content-selection`;
|
|
442
441
|
return this.makeRequest({
|
|
@@ -615,7 +614,7 @@ var Client = class {
|
|
|
615
614
|
*/
|
|
616
615
|
async searchByImage(image, useCase, options) {
|
|
617
616
|
SearchByImageParamsSchema.parse({ image, useCase, options });
|
|
618
|
-
|
|
617
|
+
_Client.validateImageSize(image);
|
|
619
618
|
return this.contentSelection({
|
|
620
619
|
use_case: useCase,
|
|
621
620
|
search: options?.query,
|
|
@@ -691,7 +690,7 @@ var Client = class {
|
|
|
691
690
|
* ```
|
|
692
691
|
*/
|
|
693
692
|
async getVersion() {
|
|
694
|
-
const url = this.baseUrl
|
|
693
|
+
const url = `${this.baseUrl}/version`;
|
|
695
694
|
return this.makeRequest({ url, headers: this.getHeaders("catalog") });
|
|
696
695
|
}
|
|
697
696
|
/**
|
|
@@ -732,7 +731,7 @@ var Client = class {
|
|
|
732
731
|
CatalogAddPropsSchema.parse({ entity, data, mainUnit, formatData });
|
|
733
732
|
const formattedData = formatData === void 0 || formatData === true ? data.map(flattenNested) : data;
|
|
734
733
|
return this.makeRequest({
|
|
735
|
-
url: this.baseUrl
|
|
734
|
+
url: `${this.baseUrl}/catalog`,
|
|
736
735
|
method: "PUT",
|
|
737
736
|
data: { data: formattedData, entity, mainUnit },
|
|
738
737
|
headers: this.getHeaders("catalog")
|
|
@@ -775,7 +774,7 @@ var Client = class {
|
|
|
775
774
|
};
|
|
776
775
|
const queryParamsString = Object.entries(queryParams).filter((entry) => !!entry[1]).map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join("&");
|
|
777
776
|
const path = "/catalog/csv";
|
|
778
|
-
const url = this.baseUrl
|
|
777
|
+
const url = `${this.baseUrl}${path}?${queryParamsString}`;
|
|
779
778
|
const body = bodyToCSV(data);
|
|
780
779
|
const r = await fetch(url, { headers: this.getHeaders("catalog"), method: "PUT", body });
|
|
781
780
|
return r.json();
|
|
@@ -977,9 +976,13 @@ var FilterBuilder = class {
|
|
|
977
976
|
field,
|
|
978
977
|
condition: { type: "Match", value }
|
|
979
978
|
};
|
|
980
|
-
if (type === "must")
|
|
981
|
-
|
|
982
|
-
else
|
|
979
|
+
if (type === "must") {
|
|
980
|
+
this.must.push(condition);
|
|
981
|
+
} else if (type === "should") {
|
|
982
|
+
this.should.push(condition);
|
|
983
|
+
} else {
|
|
984
|
+
this.must_not.push(condition);
|
|
985
|
+
}
|
|
983
986
|
return this;
|
|
984
987
|
}
|
|
985
988
|
/**
|
|
@@ -1015,9 +1018,13 @@ var FilterBuilder = class {
|
|
|
1015
1018
|
field,
|
|
1016
1019
|
condition: { type: "Range", value }
|
|
1017
1020
|
};
|
|
1018
|
-
if (type === "must")
|
|
1019
|
-
|
|
1020
|
-
else
|
|
1021
|
+
if (type === "must") {
|
|
1022
|
+
this.must.push(condition);
|
|
1023
|
+
} else if (type === "should") {
|
|
1024
|
+
this.should.push(condition);
|
|
1025
|
+
} else {
|
|
1026
|
+
this.must_not.push(condition);
|
|
1027
|
+
}
|
|
1021
1028
|
return this;
|
|
1022
1029
|
}
|
|
1023
1030
|
/**
|
|
@@ -1053,9 +1060,13 @@ var FilterBuilder = class {
|
|
|
1053
1060
|
field,
|
|
1054
1061
|
condition: { type: "DatetimeRange", value }
|
|
1055
1062
|
};
|
|
1056
|
-
if (type === "must")
|
|
1057
|
-
|
|
1058
|
-
else
|
|
1063
|
+
if (type === "must") {
|
|
1064
|
+
this.must.push(condition);
|
|
1065
|
+
} else if (type === "should") {
|
|
1066
|
+
this.should.push(condition);
|
|
1067
|
+
} else {
|
|
1068
|
+
this.must_not.push(condition);
|
|
1069
|
+
}
|
|
1059
1070
|
return this;
|
|
1060
1071
|
}
|
|
1061
1072
|
/**
|
|
@@ -1086,9 +1097,13 @@ var FilterBuilder = class {
|
|
|
1086
1097
|
field,
|
|
1087
1098
|
condition: { type: "GeoRadius", value: { lat, lon, radius } }
|
|
1088
1099
|
};
|
|
1089
|
-
if (type === "must")
|
|
1090
|
-
|
|
1091
|
-
else
|
|
1100
|
+
if (type === "must") {
|
|
1101
|
+
this.must.push(condition);
|
|
1102
|
+
} else if (type === "should") {
|
|
1103
|
+
this.should.push(condition);
|
|
1104
|
+
} else {
|
|
1105
|
+
this.must_not.push(condition);
|
|
1106
|
+
}
|
|
1092
1107
|
return this;
|
|
1093
1108
|
}
|
|
1094
1109
|
/**
|
|
@@ -1115,9 +1130,15 @@ var FilterBuilder = class {
|
|
|
1115
1130
|
*/
|
|
1116
1131
|
build() {
|
|
1117
1132
|
const filter = {};
|
|
1118
|
-
if (this.must.length > 0)
|
|
1119
|
-
|
|
1120
|
-
|
|
1133
|
+
if (this.must.length > 0) {
|
|
1134
|
+
filter.must = this.must;
|
|
1135
|
+
}
|
|
1136
|
+
if (this.should.length > 0) {
|
|
1137
|
+
filter.should = this.should;
|
|
1138
|
+
}
|
|
1139
|
+
if (this.must_not.length > 0) {
|
|
1140
|
+
filter.must_not = this.must_not;
|
|
1141
|
+
}
|
|
1121
1142
|
return filter;
|
|
1122
1143
|
}
|
|
1123
1144
|
};
|