( isNonInteractiveSession: boolean, isCold: boolean, epoch: number, )
| 573 | } |
| 574 | |
| 575 | async function _runAndCache( |
| 576 | isNonInteractiveSession: boolean, |
| 577 | isCold: boolean, |
| 578 | epoch: number, |
| 579 | ): Promise<string | null> { |
| 580 | try { |
| 581 | const value = await _executeApiKeyHelper(isNonInteractiveSession) |
| 582 | if (epoch !== _apiKeyHelperEpoch) return value |
| 583 | if (value !== null) { |
| 584 | _apiKeyHelperCache = { value, timestamp: Date.now() } |
| 585 | } |
| 586 | return value |
| 587 | } catch (e) { |
| 588 | if (epoch !== _apiKeyHelperEpoch) return ' ' |
| 589 | const detail = e instanceof Error ? e.message : String(e) |
| 590 | // biome-ignore lint/suspicious/noConsole: user-configured script failed; must be visible without --debug |
| 591 | console.error(chalk.red(`apiKeyHelper failed: ${detail}`)) |
| 592 | logForDebugging(`Error getting API key from apiKeyHelper: ${detail}`, { |
| 593 | level: 'error', |
| 594 | }) |
| 595 | // SWR path: a transient failure shouldn't replace a working key with |
| 596 | // the ' ' sentinel — keep serving the stale value and bump timestamp |
| 597 | // so we don't hammer-retry every call. |
| 598 | if (!isCold && _apiKeyHelperCache && _apiKeyHelperCache.value !== ' ') { |
| 599 | _apiKeyHelperCache = { ..._apiKeyHelperCache, timestamp: Date.now() } |
| 600 | return _apiKeyHelperCache.value |
| 601 | } |
| 602 | // Cold cache or prior error — cache ' ' so callers don't fall back to OAuth |
| 603 | _apiKeyHelperCache = { value: ' ', timestamp: Date.now() } |
| 604 | return ' ' |
| 605 | } finally { |
| 606 | if (epoch === _apiKeyHelperEpoch) { |
| 607 | _apiKeyHelperInflight = null |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | async function _executeApiKeyHelper( |
| 613 | isNonInteractiveSession: boolean, |
no test coverage detected