( isNonInteractiveSession: boolean, isCold: boolean, epoch: number, )
| 549 | } |
| 550 | |
| 551 | async function _runAndCache( |
| 552 | isNonInteractiveSession: boolean, |
| 553 | isCold: boolean, |
| 554 | epoch: number, |
| 555 | ): Promise<string | null> { |
| 556 | try { |
| 557 | const value = await _executeApiKeyHelper(isNonInteractiveSession) |
| 558 | if (epoch !== _apiKeyHelperEpoch) return value |
| 559 | if (value !== null) { |
| 560 | _apiKeyHelperCache = { value, timestamp: Date.now() } |
| 561 | } |
| 562 | return value |
| 563 | } catch (e) { |
| 564 | if (epoch !== _apiKeyHelperEpoch) return ' ' |
| 565 | const detail = e instanceof Error ? e.message : String(e) |
| 566 | // biome-ignore lint/suspicious/noConsole: user-configured script failed; must be visible without --debug |
| 567 | console.error(chalk.red(`apiKeyHelper failed: ${detail}`)) |
| 568 | logForDebugging(`Error getting API key from apiKeyHelper: ${detail}`, { |
| 569 | level: 'error', |
| 570 | }) |
| 571 | // SWR path: a transient failure shouldn't replace a working key with |
| 572 | // the ' ' sentinel — keep serving the stale value and bump timestamp |
| 573 | // so we don't hammer-retry every call. |
| 574 | if (!isCold && _apiKeyHelperCache && _apiKeyHelperCache.value !== ' ') { |
| 575 | _apiKeyHelperCache = { ..._apiKeyHelperCache, timestamp: Date.now() } |
| 576 | return _apiKeyHelperCache.value |
| 577 | } |
| 578 | // Cold cache or prior error — cache ' ' so callers don't fall back to OAuth |
| 579 | _apiKeyHelperCache = { value: ' ', timestamp: Date.now() } |
| 580 | return ' ' |
| 581 | } finally { |
| 582 | if (epoch === _apiKeyHelperEpoch) { |
| 583 | _apiKeyHelperInflight = null |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | async function _executeApiKeyHelper( |
| 589 | isNonInteractiveSession: boolean, |
no test coverage detected