( isNonInteractiveSession: boolean, )
| 541 | } |
| 542 | |
| 543 | export async function getApiKeyFromApiKeyHelper( |
| 544 | isNonInteractiveSession: boolean, |
| 545 | ): Promise<string | null> { |
| 546 | if (!getConfiguredApiKeyHelper()) return null |
| 547 | const ttl = calculateApiKeyHelperTTL() |
| 548 | if (_apiKeyHelperCache) { |
| 549 | if (Date.now() - _apiKeyHelperCache.timestamp < ttl) { |
| 550 | return _apiKeyHelperCache.value |
| 551 | } |
| 552 | // Stale — return stale value now, refresh in the background. |
| 553 | // `??=` banned here by eslint no-nullish-assign-object-call (bun bug). |
| 554 | if (!_apiKeyHelperInflight) { |
| 555 | _apiKeyHelperInflight = { |
| 556 | promise: _runAndCache( |
| 557 | isNonInteractiveSession, |
| 558 | false, |
| 559 | _apiKeyHelperEpoch, |
| 560 | ), |
| 561 | startedAt: null, |
| 562 | } |
| 563 | } |
| 564 | return _apiKeyHelperCache.value |
| 565 | } |
| 566 | // Cold cache — deduplicate concurrent calls |
| 567 | if (_apiKeyHelperInflight) return _apiKeyHelperInflight.promise |
| 568 | _apiKeyHelperInflight = { |
| 569 | promise: _runAndCache(isNonInteractiveSession, true, _apiKeyHelperEpoch), |
| 570 | startedAt: Date.now(), |
| 571 | } |
| 572 | return _apiKeyHelperInflight.promise |
| 573 | } |
| 574 | |
| 575 | async function _runAndCache( |
| 576 | isNonInteractiveSession: boolean, |
no test coverage detected