( isNonInteractiveSession: boolean, isCold: boolean, epoch: number, )
| 499 | } |
| 500 | |
| 501 | async function _runAndCache( |
| 502 | isNonInteractiveSession: boolean, |
| 503 | isCold: boolean, |
| 504 | epoch: number, |
| 505 | ): Promise<string | null> { |
| 506 | try { |
| 507 | const value = await _executeApiKeyHelper(isNonInteractiveSession) |
| 508 | if (epoch !== _apiKeyHelperEpoch) return value |
| 509 | if (value !== null) { |
| 510 | _apiKeyHelperCache = { value, timestamp: Date.now() } |
| 511 | } |
| 512 | return value |
| 513 | } catch (e) { |
| 514 | if (epoch !== _apiKeyHelperEpoch) return ' ' |
| 515 | const detail = e instanceof Error ? e.message : String(e) |
| 516 | // biome-ignore lint/suspicious/noConsole: user-configured script failed; must be visible without --debug |
| 517 | console.error(chalk.red(`apiKeyHelper failed: ${detail}`)) |
| 518 | logForDebugging(`Error getting API key from apiKeyHelper: ${detail}`, { |
| 519 | level: 'error', |
| 520 | }) |
| 521 | // SWR path: a transient failure shouldn't replace a working key with |
| 522 | // the ' ' sentinel — keep serving the stale value and bump timestamp |
| 523 | // so we don't hammer-retry every call. |
| 524 | if (!isCold && _apiKeyHelperCache && _apiKeyHelperCache.value !== ' ') { |
| 525 | _apiKeyHelperCache = { ..._apiKeyHelperCache, timestamp: Date.now() } |
| 526 | return _apiKeyHelperCache.value |
| 527 | } |
| 528 | // Cold cache or prior error — cache ' ' so callers don't fall back to OAuth |
| 529 | _apiKeyHelperCache = { value: ' ', timestamp: Date.now() } |
| 530 | return ' ' |
| 531 | } finally { |
| 532 | if (epoch === _apiKeyHelperEpoch) { |
| 533 | _apiKeyHelperInflight = null |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | async function _executeApiKeyHelper( |
| 539 | isNonInteractiveSession: boolean, |
no test coverage detected