MCPcopy Create free account
hub / github.com/freecodexyz/free-code / _runAndCache

Function _runAndCache

src/utils/auth.ts:502–537  ·  view source on GitHub ↗
(
  isNonInteractiveSession: boolean,
  isCold: boolean,
  epoch: number,
)

Source from the content-addressed store, hash-verified

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

Callers 1

Calls 3

_executeApiKeyHelperFunction · 0.85
logForDebuggingFunction · 0.85
errorMethod · 0.45

Tested by

no test coverage detected