@zodic/shared 0.0.424 → 0.0.426
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 +49 -18
- package/package.json +1 -1
- package/types/scopes/cloudflare.ts +2 -2
- package/utils/faceSwapHelpers.ts +2 -1
package/app/api/index.ts
CHANGED
|
@@ -143,9 +143,10 @@ export const Api = (env: BackendBindings) => ({
|
|
|
143
143
|
? negPrompt
|
|
144
144
|
: 'blurry, boring, dark, low quality, lowres, grainy, distorted, overexposed, oversaturated, flat, rough, noisy, abstract, monochrome, dull, cracked, chipped, uneven, asymmetrical, deformed, pixelated, cartoonish, out of focus, messy, dull lighting, rough texture, unbalanced, low contrast, poorly drawn, faded colors, chaotic, unnatural metals, overly sharp colors, text, writing, letters, symbols, logos, watermark, calligraphy, caption, bad anatomy, malformed hands, extra fingers, missing fingers, poorly drawn face, distorted eyes, crossed eyes, out-of-proportion eyes, deformed limbs, floating limbs, disconnected limbs, mutated hands, elongated neck, unrealistic body proportions, disfigured, extra limbs, unnatural skin texture, melted features, unnatural fingers, poorly rendered hands, blurry face';
|
|
145
145
|
|
|
146
|
+
const leonardoApiKey = await env.LEONARDO_API_KEY.get();
|
|
146
147
|
const endpoint = 'https://cloud.leonardo.ai/api/rest/v1/generations';
|
|
147
148
|
const headers = {
|
|
148
|
-
Authorization: `Bearer ${
|
|
149
|
+
Authorization: `Bearer ${leonardoApiKey}`,
|
|
149
150
|
'Content-Type': 'application/json',
|
|
150
151
|
};
|
|
151
152
|
|
|
@@ -182,25 +183,54 @@ export const Api = (env: BackendBindings) => ({
|
|
|
182
183
|
body['init_strength'] = initStrength;
|
|
183
184
|
}
|
|
184
185
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
method: 'POST',
|
|
188
|
-
headers,
|
|
189
|
-
body: JSON.stringify(body),
|
|
190
|
-
});
|
|
186
|
+
const maxRetries = 3;
|
|
187
|
+
const baseDelayMs = 2000;
|
|
191
188
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
189
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
190
|
+
try {
|
|
191
|
+
const response = await fetch(endpoint, {
|
|
192
|
+
method: 'POST',
|
|
193
|
+
headers,
|
|
194
|
+
body: JSON.stringify(body),
|
|
195
|
+
});
|
|
197
196
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
197
|
+
if (!response.ok) {
|
|
198
|
+
const error = await response.json().catch(() => ({}));
|
|
199
|
+
const isRetryable = response.status >= 500 || response.status === 429;
|
|
200
|
+
|
|
201
|
+
if (isRetryable && attempt < maxRetries) {
|
|
202
|
+
const delayMs = baseDelayMs * Math.pow(2, attempt - 1);
|
|
203
|
+
console.warn(
|
|
204
|
+
`Leonardo API ${response.status} (attempt ${attempt}/${maxRetries}), retrying in ${delayMs}ms`,
|
|
205
|
+
error
|
|
206
|
+
);
|
|
207
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
console.error('❌ Error from Leonardo API:', error);
|
|
212
|
+
throw new Error(`Leonardo API Error: ${response.status}`);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const data = (await response.json()) as LeonardoGenerateImageResponse;
|
|
216
|
+
return data;
|
|
217
|
+
} catch (err: any) {
|
|
218
|
+
const isNetworkError = !err.message?.startsWith('Leonardo API Error');
|
|
219
|
+
if (isNetworkError && attempt < maxRetries) {
|
|
220
|
+
const delayMs = baseDelayMs * Math.pow(2, attempt - 1);
|
|
221
|
+
console.warn(
|
|
222
|
+
`Leonardo API network error (attempt ${attempt}/${maxRetries}), retrying in ${delayMs}ms:`,
|
|
223
|
+
err.message
|
|
224
|
+
);
|
|
225
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
console.error('❌ Error calling Leonardo API:', err.message);
|
|
229
|
+
throw err;
|
|
230
|
+
}
|
|
203
231
|
}
|
|
232
|
+
|
|
233
|
+
throw new Error('Leonardo API: max retries exhausted');
|
|
204
234
|
},
|
|
205
235
|
|
|
206
236
|
generatePresignedUrl: async (
|
|
@@ -211,9 +241,10 @@ export const Api = (env: BackendBindings) => ({
|
|
|
211
241
|
key: string;
|
|
212
242
|
url: string;
|
|
213
243
|
}> => {
|
|
244
|
+
const leonardoApiKey = await env.LEONARDO_API_KEY.get();
|
|
214
245
|
const endpoint = 'https://cloud.leonardo.ai/api/rest/v1/init-image';
|
|
215
246
|
const headers = {
|
|
216
|
-
Authorization: `Bearer ${
|
|
247
|
+
Authorization: `Bearer ${leonardoApiKey}`,
|
|
217
248
|
'Content-Type': 'application/json',
|
|
218
249
|
};
|
|
219
250
|
|
package/package.json
CHANGED
|
@@ -23,7 +23,7 @@ export type CentralBindings = {
|
|
|
23
23
|
CONCEPTS_BUCKET: R2Bucket;
|
|
24
24
|
PHOTOS_BUCKET: R2Bucket;
|
|
25
25
|
WHEEL_BUCKET: R2Bucket;
|
|
26
|
-
LEONARDO_API_KEY:
|
|
26
|
+
LEONARDO_API_KEY: SECRETS_STORE_SECRET;
|
|
27
27
|
KV_ASTRO: KVNamespace;
|
|
28
28
|
KV_CONCEPT_FAILURES: KVNamespace;
|
|
29
29
|
KV_API_USAGE: KVNamespace;
|
|
@@ -52,7 +52,7 @@ export type CentralBindings = {
|
|
|
52
52
|
REDIRECT_PREFIX: string;
|
|
53
53
|
ASTROLOGY_WESTERN_USER_ID: string;
|
|
54
54
|
ASTROLOGY_WESTERN_API_KEY: string;
|
|
55
|
-
PIAPI_API_KEY:
|
|
55
|
+
PIAPI_API_KEY: SECRETS_STORE_SECRET;
|
|
56
56
|
DEEPSEEK_API_KEY: SECRETS_STORE_SECRET;
|
|
57
57
|
ENV: Environment;
|
|
58
58
|
|
package/utils/faceSwapHelpers.ts
CHANGED
|
@@ -78,9 +78,10 @@ export const sendRequest = async (
|
|
|
78
78
|
): Promise<ApidogModel> => {
|
|
79
79
|
const maxRetries = 3;
|
|
80
80
|
const retryDelay = 1000; // 1 second delay between retries
|
|
81
|
+
const piapiApiKey = await env.PIAPI_API_KEY.get();
|
|
81
82
|
const endpoint = 'https://api.piapi.ai/api/v1/task';
|
|
82
83
|
const headers = {
|
|
83
|
-
'x-api-key':
|
|
84
|
+
'x-api-key': piapiApiKey,
|
|
84
85
|
'Content-Type': 'application/json',
|
|
85
86
|
};
|
|
86
87
|
|