@zodic/shared 0.0.424 → 0.0.425
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/app/api/index.ts +45 -16
- package/package.json +1 -1
package/app/api/index.ts
CHANGED
|
@@ -182,25 +182,54 @@ export const Api = (env: BackendBindings) => ({
|
|
|
182
182
|
body['init_strength'] = initStrength;
|
|
183
183
|
}
|
|
184
184
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
185
|
+
const maxRetries = 3;
|
|
186
|
+
const baseDelayMs = 2000;
|
|
187
|
+
|
|
188
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
189
|
+
try {
|
|
190
|
+
const response = await fetch(endpoint, {
|
|
191
|
+
method: 'POST',
|
|
192
|
+
headers,
|
|
193
|
+
body: JSON.stringify(body),
|
|
194
|
+
});
|
|
191
195
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
throw new Error(`Leonardo API Error: ${response.status}`);
|
|
196
|
-
}
|
|
196
|
+
if (!response.ok) {
|
|
197
|
+
const error = await response.json().catch(() => ({}));
|
|
198
|
+
const isRetryable = response.status >= 500 || response.status === 429;
|
|
197
199
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
200
|
+
if (isRetryable && attempt < maxRetries) {
|
|
201
|
+
const delayMs = baseDelayMs * Math.pow(2, attempt - 1);
|
|
202
|
+
console.warn(
|
|
203
|
+
`Leonardo API ${response.status} (attempt ${attempt}/${maxRetries}), retrying in ${delayMs}ms`,
|
|
204
|
+
error
|
|
205
|
+
);
|
|
206
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
console.error('❌ Error from Leonardo API:', error);
|
|
211
|
+
throw new Error(`Leonardo API Error: ${response.status}`);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const data = (await response.json()) as LeonardoGenerateImageResponse;
|
|
215
|
+
return data;
|
|
216
|
+
} catch (err: any) {
|
|
217
|
+
const isNetworkError = !err.message?.startsWith('Leonardo API Error');
|
|
218
|
+
if (isNetworkError && attempt < maxRetries) {
|
|
219
|
+
const delayMs = baseDelayMs * Math.pow(2, attempt - 1);
|
|
220
|
+
console.warn(
|
|
221
|
+
`Leonardo API network error (attempt ${attempt}/${maxRetries}), retrying in ${delayMs}ms:`,
|
|
222
|
+
err.message
|
|
223
|
+
);
|
|
224
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
console.error('❌ Error calling Leonardo API:', err.message);
|
|
228
|
+
throw err;
|
|
229
|
+
}
|
|
203
230
|
}
|
|
231
|
+
|
|
232
|
+
throw new Error('Leonardo API: max retries exhausted');
|
|
204
233
|
},
|
|
205
234
|
|
|
206
235
|
generatePresignedUrl: async (
|