( api_key: string, cacheOnly = false, useCache = true, mode?: RateLimiterMode, )
| 243 | |
| 244 | /** @public used by auto_charge.ts (disabled, Autumn handles auto-recharge) */ |
| 245 | export async function getACUC( |
| 246 | api_key: string, |
| 247 | cacheOnly = false, |
| 248 | useCache = true, |
| 249 | mode?: RateLimiterMode, |
| 250 | ): Promise<AuthCreditUsageChunk | null> { |
| 251 | let isExtract = |
| 252 | mode === RateLimiterMode.Extract || |
| 253 | mode === RateLimiterMode.ExtractStatus || |
| 254 | mode === RateLimiterMode.ExtractAgentPreview; |
| 255 | |
| 256 | if (api_key === config.PREVIEW_TOKEN) { |
| 257 | const acuc = mockPreviewACUC(api_key, isExtract); |
| 258 | acuc.is_extract = isExtract; |
| 259 | return acuc; |
| 260 | } |
| 261 | |
| 262 | if (config.USE_DB_AUTHENTICATION !== true) { |
| 263 | const acuc = mockACUC(); |
| 264 | acuc.is_extract = isExtract; |
| 265 | return acuc; |
| 266 | } |
| 267 | |
| 268 | const cacheKeyACUC = `acuc_${api_key}_${isExtract ? "extract" : "scrape"}`; |
| 269 | |
| 270 | if (useCache) { |
| 271 | const cachedACUC = await getValue(cacheKeyACUC); |
| 272 | if (cachedACUC !== null) { |
| 273 | return JSON.parse(cachedACUC); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | if (!cacheOnly) { |
| 278 | let data: AuthCreditUsageChunkRow[] = []; |
| 279 | let retries = 0; |
| 280 | const maxRetries = 5; |
| 281 | while (retries < maxRetries) { |
| 282 | const database = Math.random() > 2 / 3 ? dbRr : db; |
| 283 | try { |
| 284 | data = await authCreditUsageChunk(database, api_key, isExtract); |
| 285 | break; |
| 286 | } catch (error) { |
| 287 | logger.warn( |
| 288 | `Failed to retrieve authentication and credit usage data after ${retries}, trying again...`, |
| 289 | { error }, |
| 290 | ); |
| 291 | retries++; |
| 292 | if (retries === maxRetries) { |
| 293 | throw new Error( |
| 294 | "Failed to retrieve authentication and credit usage data after 3 attempts: " + |
| 295 | JSON.stringify(error), |
| 296 | ); |
| 297 | } |
| 298 | |
| 299 | // Wait for a short time before retrying |
| 300 | await new Promise(resolve => setTimeout(resolve, 200)); |
| 301 | } |
| 302 | } |
no test coverage detected
searching dependent graphs…