@mynthio/sdk 0.0.44 → 0.0.45
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 +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +34 -9
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -855,7 +855,7 @@ Set `MYNTH_WEBHOOK_SECRET` in your environment, or pass `webhookSecret` explicit
|
|
|
855
855
|
|
|
856
856
|
`upload()`, `generate()`, `generateAsync()`, `rate()`, `rateAsync()`, `alt()`, `altAsync()`, `review()`, `reviewAsync()`, `removeBackground()`, `removeBackgroundAsync()`, `models.list()`, and the `video` equivalents (`video.generate()`, `video.generateAsync()`, `video.upload()`, `video.estimate()`) may throw `MynthAPIError` if the request fails. Polling can also throw task-specific errors:
|
|
857
857
|
|
|
858
|
-
While polling, transient failures (404, 5xx, dropped connections) are retried: a created task is owed an answer, so a cold cache or a brief outage does not lose you the result. Polling gives up after 20 consecutive failures (~100s) with `TaskAsyncFetchError` or `TaskAsyncTaskFetchError`,
|
|
858
|
+
While polling, transient failures (404, 5xx, dropped connections) are retried: a created task is owed an answer, so a cold cache or a brief outage does not lose you the result. A 429 slows polling down, doubling the interval up to 30 seconds, and only the timeout ends it. Polling gives up after 20 consecutive failures (~100s) with `TaskAsyncFetchError` or `TaskAsyncTaskFetchError`, immediately on a 401 or 403 with `TaskAsyncUnauthorizedError`, and immediately on any other 4xx with `TaskAsyncFetchError` or `TaskAsyncTaskFetchError`. Image waits time out after 30 minutes, video waits after an hour.
|
|
859
859
|
|
|
860
860
|
```ts
|
|
861
861
|
import {
|
package/dist/index.d.ts
CHANGED
|
@@ -224,10 +224,11 @@ export declare class TaskAsyncUnauthorizedError extends Error {
|
|
|
224
224
|
constructor(taskId: string);
|
|
225
225
|
}
|
|
226
226
|
/**
|
|
227
|
-
* Error thrown when fetching task status fails after multiple retries
|
|
227
|
+
* Error thrown when fetching task status fails after multiple retries, or at
|
|
228
|
+
* once on a response that a retry cannot change (a 400, say).
|
|
228
229
|
*/
|
|
229
230
|
export declare class TaskAsyncFetchError extends Error {
|
|
230
|
-
constructor(taskId: string, cause?: Error);
|
|
231
|
+
constructor(taskId: string, cause?: Error, status?: number);
|
|
231
232
|
}
|
|
232
233
|
/**
|
|
233
234
|
* Error thrown when fetching full task data fails.
|
|
@@ -292,7 +293,7 @@ export declare class TaskAsync<ResultT> {
|
|
|
292
293
|
* @param options.signal - Stops this wait when aborted (see {@link TaskAsyncWaitOptions})
|
|
293
294
|
* @throws {TaskAsyncTimeoutError} If polling exceeds the timeout
|
|
294
295
|
* @throws {TaskAsyncUnauthorizedError} If access is denied
|
|
295
|
-
* @throws {TaskAsyncFetchError} If fetching status fails repeatedly
|
|
296
|
+
* @throws {TaskAsyncFetchError} If fetching status fails repeatedly, or with a non-retryable 4xx
|
|
296
297
|
* @throws {TaskAsyncTaskFetchError} If fetching the completed task fails
|
|
297
298
|
* @throws {TaskAsyncTaskFailedError} If the task fails before completion
|
|
298
299
|
*/
|
package/dist/index.js
CHANGED
|
@@ -660,6 +660,7 @@ const FAST_POLLING_DURATION_MS = 12e3;
|
|
|
660
660
|
const FAST_POLLING_INTERVAL_MS = 2500;
|
|
661
661
|
const SLOW_POLLING_INTERVAL_MS = 5e3;
|
|
662
662
|
const MAX_RETRY_COUNT = 20;
|
|
663
|
+
const MAX_RATE_LIMITED_INTERVAL_MS = 3e4;
|
|
663
664
|
/**
|
|
664
665
|
* Error thrown when task polling exceeds the maximum timeout duration.
|
|
665
666
|
*/
|
|
@@ -679,11 +680,12 @@ var TaskAsyncUnauthorizedError = class extends Error {
|
|
|
679
680
|
}
|
|
680
681
|
};
|
|
681
682
|
/**
|
|
682
|
-
* Error thrown when fetching task status fails after multiple retries
|
|
683
|
+
* Error thrown when fetching task status fails after multiple retries, or at
|
|
684
|
+
* once on a response that a retry cannot change (a 400, say).
|
|
683
685
|
*/
|
|
684
686
|
var TaskAsyncFetchError = class extends Error {
|
|
685
|
-
constructor(taskId, cause) {
|
|
686
|
-
super(`Failed to fetch status for task ${taskId} after multiple retries`);
|
|
687
|
+
constructor(taskId, cause, status) {
|
|
688
|
+
super(status ? `Failed to fetch status for task ${taskId} (status ${status})` : `Failed to fetch status for task ${taskId} after multiple retries`);
|
|
687
689
|
this.name = "TaskAsyncFetchError";
|
|
688
690
|
this.cause = cause;
|
|
689
691
|
}
|
|
@@ -751,7 +753,7 @@ var TaskAsync = class {
|
|
|
751
753
|
* @param options.signal - Stops this wait when aborted (see {@link TaskAsyncWaitOptions})
|
|
752
754
|
* @throws {TaskAsyncTimeoutError} If polling exceeds the timeout
|
|
753
755
|
* @throws {TaskAsyncUnauthorizedError} If access is denied
|
|
754
|
-
* @throws {TaskAsyncFetchError} If fetching status fails repeatedly
|
|
756
|
+
* @throws {TaskAsyncFetchError} If fetching status fails repeatedly, or with a non-retryable 4xx
|
|
755
757
|
* @throws {TaskAsyncTaskFetchError} If fetching the completed task fails
|
|
756
758
|
* @throws {TaskAsyncTaskFailedError} If the task fails before completion
|
|
757
759
|
*/
|
|
@@ -797,19 +799,24 @@ var TaskAsync = class {
|
|
|
797
799
|
const { timeoutMs, fastDurationMs, fastIntervalMs, intervalMs } = this.polling;
|
|
798
800
|
const startTime = Date.now();
|
|
799
801
|
let retryCount = 0;
|
|
802
|
+
let rateLimitedCount = 0;
|
|
800
803
|
let useApiKeyFallback = false;
|
|
801
804
|
let lastError;
|
|
802
805
|
while (true) {
|
|
803
806
|
const elapsed = Date.now() - startTime;
|
|
804
807
|
if (elapsed >= timeoutMs) throw new TaskAsyncTimeoutError(this.id, timeoutMs);
|
|
805
808
|
const result = await this.fetchStatus(useApiKeyFallback, signal);
|
|
809
|
+
let rateLimited = false;
|
|
806
810
|
if (result.ok) {
|
|
807
811
|
if (result.status === "completed") {
|
|
808
812
|
const fetched = await this.fetchTask(signal);
|
|
809
813
|
if (fetched.ok) return this.resultFactory(fetched.data);
|
|
810
814
|
if (fetched.unauthorized) throw new TaskAsyncUnauthorizedError(this.id);
|
|
811
|
-
|
|
812
|
-
|
|
815
|
+
if (fetched.rateLimited) rateLimited = true;
|
|
816
|
+
else {
|
|
817
|
+
retryCount++;
|
|
818
|
+
if (!fetched.retryable || retryCount >= MAX_RETRY_COUNT) throw new TaskAsyncTaskFetchError(this.id, fetched.status, fetched.error);
|
|
819
|
+
}
|
|
813
820
|
} else if (result.status === "failed") throw new TaskAsyncTaskFailedError(this.id);
|
|
814
821
|
else retryCount = 0;
|
|
815
822
|
} else {
|
|
@@ -818,13 +825,17 @@ var TaskAsync = class {
|
|
|
818
825
|
continue;
|
|
819
826
|
}
|
|
820
827
|
if (result.unauthorized) throw new TaskAsyncUnauthorizedError(this.id);
|
|
821
|
-
if (result.retryable)
|
|
828
|
+
if (!result.retryable) throw new TaskAsyncFetchError(this.id, result.error, result.status);
|
|
829
|
+
if (result.rateLimited) rateLimited = true;
|
|
830
|
+
else {
|
|
822
831
|
retryCount++;
|
|
823
832
|
lastError = result.error;
|
|
824
833
|
if (retryCount >= MAX_RETRY_COUNT) throw new TaskAsyncFetchError(this.id, lastError);
|
|
825
834
|
}
|
|
826
835
|
}
|
|
827
|
-
|
|
836
|
+
rateLimitedCount = rateLimited ? rateLimitedCount + 1 : 0;
|
|
837
|
+
const baseInterval = elapsed < fastDurationMs ? fastIntervalMs : intervalMs;
|
|
838
|
+
const interval = Math.min(baseInterval * 2 ** rateLimitedCount, Math.max(baseInterval, MAX_RATE_LIMITED_INTERVAL_MS)) + Math.random() * 500;
|
|
828
839
|
const remainingTime = timeoutMs - elapsed;
|
|
829
840
|
const waitTime = Math.min(interval, remainingTime);
|
|
830
841
|
await this.sleep(waitTime, signal);
|
|
@@ -852,6 +863,12 @@ var TaskAsync = class {
|
|
|
852
863
|
retryable: true,
|
|
853
864
|
notFound: true
|
|
854
865
|
};
|
|
866
|
+
if (response.status === 429) return {
|
|
867
|
+
ok: false,
|
|
868
|
+
unauthorized: false,
|
|
869
|
+
retryable: true,
|
|
870
|
+
rateLimited: true
|
|
871
|
+
};
|
|
855
872
|
if (response.status >= 500) return {
|
|
856
873
|
ok: false,
|
|
857
874
|
unauthorized: false,
|
|
@@ -860,7 +877,8 @@ var TaskAsync = class {
|
|
|
860
877
|
return {
|
|
861
878
|
ok: false,
|
|
862
879
|
unauthorized: false,
|
|
863
|
-
retryable: false
|
|
880
|
+
retryable: false,
|
|
881
|
+
status: response.status
|
|
864
882
|
};
|
|
865
883
|
} catch (error) {
|
|
866
884
|
signal.throwIfAborted();
|
|
@@ -884,6 +902,13 @@ var TaskAsync = class {
|
|
|
884
902
|
unauthorized: true,
|
|
885
903
|
retryable: false
|
|
886
904
|
};
|
|
905
|
+
if (response.status === 429) return {
|
|
906
|
+
ok: false,
|
|
907
|
+
unauthorized: false,
|
|
908
|
+
retryable: true,
|
|
909
|
+
rateLimited: true,
|
|
910
|
+
status: response.status
|
|
911
|
+
};
|
|
887
912
|
if (response.status === 404 || response.status >= 500) return {
|
|
888
913
|
ok: false,
|
|
889
914
|
unauthorized: false,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mynthio/sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.45",
|
|
4
4
|
"description": "Official Mynth SDK for image generation and analysis",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -45,11 +45,12 @@
|
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@arethetypeswrong/core": "^0.18.5",
|
|
47
47
|
"@types/node": "^22.20.4",
|
|
48
|
+
"@vitest/coverage-v8": "^5.0.2",
|
|
48
49
|
"convex": "^1.46.0",
|
|
49
50
|
"publint": "^0.3.24",
|
|
50
51
|
"tsdown": "^0.23.0",
|
|
51
52
|
"typescript": "7.0.2",
|
|
52
|
-
"vitest": "^5.0.
|
|
53
|
+
"vitest": "^5.0.2"
|
|
53
54
|
},
|
|
54
55
|
"peerDependencies": {
|
|
55
56
|
"convex": "^1.27.3"
|
|
@@ -62,6 +63,7 @@
|
|
|
62
63
|
"scripts": {
|
|
63
64
|
"build": "tsdown",
|
|
64
65
|
"test": "vitest run",
|
|
66
|
+
"test:coverage": "vitest run --coverage",
|
|
65
67
|
"test:watch": "vitest",
|
|
66
68
|
"typecheck": "tsc"
|
|
67
69
|
}
|