refactor(account): share token freshness helper (#20591)

This commit is contained in:
Kit Langton
2026-04-01 22:57:45 -04:00
committed by GitHub
parent 5daf2fa7f0
commit 916afb5220
2 changed files with 13 additions and 6 deletions

View File

@@ -120,6 +120,10 @@ class TokenRefreshRequest extends Schema.Class<TokenRefreshRequest>("TokenRefres
const clientId = "opencode-cli"
const eagerRefreshThreshold = Duration.minutes(5)
const eagerRefreshThresholdMs = Duration.toMillis(eagerRefreshThreshold)
const isTokenFresh = (tokenExpiry: number | null, now: number) =>
tokenExpiry != null && tokenExpiry > now + eagerRefreshThresholdMs
const mapAccountServiceError =
(message = "Account service operation failed") =>
@@ -219,7 +223,7 @@ export namespace Account {
const account = maybeAccount.value
const now = yield* Clock.currentTimeMillis
if (account.token_expiry && account.token_expiry > now + Duration.toMillis(eagerRefreshThreshold)) {
if (isTokenFresh(account.token_expiry, now)) {
return account.access_token
}
@@ -229,7 +233,7 @@ export namespace Account {
const resolveToken = Effect.fnUntraced(function* (row: AccountRow) {
const now = yield* Clock.currentTimeMillis
if (row.token_expiry && row.token_expiry > now + Duration.toMillis(eagerRefreshThreshold)) {
if (isTokenFresh(row.token_expiry, now)) {
return row.access_token
}