( isNonInteractiveSession: boolean, )
| 467 | } |
| 468 | |
| 469 | export async function getApiKeyFromApiKeyHelper( |
| 470 | isNonInteractiveSession: boolean, |
| 471 | ): Promise<string | null> { |
| 472 | if (!getConfiguredApiKeyHelper()) return null |
| 473 | const ttl = calculateApiKeyHelperTTL() |
| 474 | if (_apiKeyHelperCache) { |
| 475 | if (Date.now() - _apiKeyHelperCache.timestamp < ttl) { |
| 476 | return _apiKeyHelperCache.value |
| 477 | } |
| 478 | // Stale — return stale value now, refresh in the background. |
| 479 | // `??=` banned here by eslint no-nullish-assign-object-call (bun bug). |
| 480 | if (!_apiKeyHelperInflight) { |
| 481 | _apiKeyHelperInflight = { |
| 482 | promise: _runAndCache( |
| 483 | isNonInteractiveSession, |
| 484 | false, |
| 485 | _apiKeyHelperEpoch, |
| 486 | ), |
| 487 | startedAt: null, |
| 488 | } |
| 489 | } |
| 490 | return _apiKeyHelperCache.value |
| 491 | } |
| 492 | // Cold cache — deduplicate concurrent calls |
| 493 | if (_apiKeyHelperInflight) return _apiKeyHelperInflight.promise |
| 494 | _apiKeyHelperInflight = { |
| 495 | promise: _runAndCache(isNonInteractiveSession, true, _apiKeyHelperEpoch), |
| 496 | startedAt: Date.now(), |
| 497 | } |
| 498 | return _apiKeyHelperInflight.promise |
| 499 | } |
| 500 | |
| 501 | async function _runAndCache( |
| 502 | isNonInteractiveSession: boolean, |
no test coverage detected