@albatrossai/albatross-sdk 1.1.40 → 1.1.42-alpha

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 CHANGED
@@ -1,18 +1,14 @@
1
1
  'use strict';
2
2
 
3
- var sync = require('csv-stringify/sync');
4
3
  var zod = require('zod');
4
+ var sync = require('csv-stringify/sync');
5
5
 
6
6
  // src/request.ts
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
- return status >= 500 && status <= 599;
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,62 +90,6 @@ var makeRequest = async ({
91
90
  }
92
91
  throw lastError || new Error("Unexpected error in makeRequest");
93
92
  };
94
- function flattenNested(obj) {
95
- const result = {};
96
- Object.entries(obj).forEach(([key, value]) => {
97
- if (typeof value === "object" && !Array.isArray(value) && value !== null) {
98
- Object.entries(value).forEach(([nestedKey, nestedValue]) => {
99
- if (Array.isArray(nestedValue)) {
100
- result[`${key}_${nestedKey}`] = nestedValue.join(",");
101
- } else if (typeof nestedValue === "object" && nestedValue !== null) {
102
- Object.entries(nestedValue).forEach(([subKey, subValue]) => {
103
- result[`${nestedKey}_${subKey}`] = subValue;
104
- });
105
- } else {
106
- result[`${key}_${nestedKey}`] = nestedValue;
107
- }
108
- });
109
- } else if (Array.isArray(value)) {
110
- result[key] = value.join(",");
111
- } else {
112
- result[key] = value;
113
- }
114
- });
115
- return result;
116
- }
117
- var preFormatRow = (rowHeaders, row) => {
118
- const flattenedRow = flattenNested(row);
119
- return rowHeaders.map((header) => {
120
- const value = flattenedRow[header];
121
- if (value === null || value === void 0) {
122
- return "";
123
- }
124
- if (typeof value === "object") {
125
- return JSON.stringify(value);
126
- }
127
- return String(value);
128
- });
129
- };
130
- var bodyToCSV = (data) => {
131
- if (data.length === 0) {
132
- throw new Error("data is empty");
133
- }
134
- const row0 = data[0];
135
- if (!row0) {
136
- throw new Error("data is empty");
137
- }
138
- const rowHeaders = Object.keys(flattenNested(row0));
139
- const csvData = [
140
- rowHeaders,
141
- ...data.map((row) => preFormatRow(rowHeaders, row))
142
- ];
143
- return sync.stringify(csvData, {
144
- delimiter: ",",
145
- quote: true,
146
- quoted_string: true,
147
- header: false
148
- }).trim();
149
- };
150
93
  var EventSchema = zod.z.object({
151
94
  eventType: zod.z.string().min(1, "event type required"),
152
95
  payload: zod.z.record(zod.z.string(), zod.z.any()).optional(),
@@ -315,10 +258,71 @@ var FeedbackParamsSchema = zod.z.object({
315
258
  predictionUuid: zod.z.string().min(1, "prediction uuid required"),
316
259
  value: zod.z.record(zod.z.string(), zod.z.any())
317
260
  });
261
+ var flattenNested = (obj) => {
262
+ const result = {};
263
+ for (const [key, value] of Object.entries(obj)) {
264
+ if (typeof value === "object" && !Array.isArray(value) && value !== null) {
265
+ for (const [nestedKey, nestedValue] of Object.entries(value)) {
266
+ if (Array.isArray(nestedValue)) {
267
+ result[`${key}_${nestedKey}`] = nestedValue.join(",");
268
+ } else if (typeof nestedValue === "object" && nestedValue !== null) {
269
+ for (const [subKey, subValue] of Object.entries(nestedValue)) {
270
+ result[`${nestedKey}_${subKey}`] = subValue;
271
+ }
272
+ } else {
273
+ result[`${key}_${nestedKey}`] = nestedValue;
274
+ }
275
+ }
276
+ } else if (Array.isArray(value)) {
277
+ result[key] = value.join(",");
278
+ } else {
279
+ result[key] = value;
280
+ }
281
+ }
282
+ return result;
283
+ };
284
+ var preFormatRow = (rowHeaders, row) => {
285
+ const flattenedRow = flattenNested(row);
286
+ return rowHeaders.map((header) => {
287
+ const value = flattenedRow[header];
288
+ if (value === null || value === void 0) {
289
+ return "";
290
+ }
291
+ if (typeof value === "object") {
292
+ return JSON.stringify(value);
293
+ }
294
+ return String(value);
295
+ });
296
+ };
297
+ var bodyToCSV = (data) => {
298
+ if (data.length === 0) {
299
+ throw new Error("data is empty");
300
+ }
301
+ const [row0] = data;
302
+ if (!row0) {
303
+ throw new Error("data is empty");
304
+ }
305
+ const rowHeaders = Object.keys(flattenNested(row0));
306
+ const csvData = [
307
+ rowHeaders,
308
+ ...data.map((row) => preFormatRow(rowHeaders, row))
309
+ ];
310
+ return sync.stringify(csvData, {
311
+ delimiter: ",",
312
+ quote: true,
313
+ quoted_string: true,
314
+ header: false
315
+ }).trim();
316
+ };
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: "Bearer " + this.token
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
- this.validateImageSize(request.options.image);
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
- this.validateImageSize(image);
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 + "/version";
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 + "/catalog",
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 + path + "?" + queryParamsString;
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") this.must.push(condition);
981
- else if (type === "should") this.should.push(condition);
982
- else this.must_not.push(condition);
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") this.must.push(condition);
1019
- else if (type === "should") this.should.push(condition);
1020
- else this.must_not.push(condition);
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") this.must.push(condition);
1057
- else if (type === "should") this.should.push(condition);
1058
- else this.must_not.push(condition);
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") this.must.push(condition);
1090
- else if (type === "should") this.should.push(condition);
1091
- else this.must_not.push(condition);
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) filter.must = this.must;
1119
- if (this.should.length > 0) filter.should = this.should;
1120
- if (this.must_not.length > 0) filter.must_not = this.must_not;
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
  };