Files
OmniRoute/src/shared/utils/providerConnectionStatus.ts
nguyenha935 aaddfcd545 fix(providers): unify connection and routing flows (#7629)
* fix(providers): unify connection and routing flows

* docs(changelog): add provider flow consistency entry

* test(providers): move section-visibility cases to own file (test file-size cap)

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

* refactor(api): extract fetchLiveNoAuthModels + toLiveModel helpers (cognitive-complexity gate on buildNoAuthModelsResponse)

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: nguyenha935 <208228297+nguyenha935@users.noreply.github.com>
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
2026-07-18 21:20:09 -03:00

47 lines
1.4 KiB
TypeScript

export interface ProviderConnectionStatusLike {
isActive?: boolean | null;
testStatus?: string | null;
rateLimitedUntil?: string | number | Date | null;
}
const CONNECTED_STATUSES = new Set(["active", "success", "unknown"]);
const ERROR_STATUSES = new Set(["error", "expired", "unavailable"]);
export function getEffectiveProviderConnectionStatus(
connection: ProviderConnectionStatusLike,
now = Date.now()
): string {
const status = connection.testStatus || "unknown";
if (status !== "unavailable") return status;
const rawCooldownUntil = connection.rateLimitedUntil;
const cooldownUntil =
rawCooldownUntil instanceof Date
? rawCooldownUntil.getTime()
: typeof rawCooldownUntil === "string" || typeof rawCooldownUntil === "number"
? new Date(rawCooldownUntil).getTime()
: Number.NaN;
const isCoolingDown = Number.isFinite(cooldownUntil) && cooldownUntil > now;
return isCoolingDown ? status : "active";
}
export function isProviderConnectionConnected(
connection: ProviderConnectionStatusLike,
now = Date.now()
): boolean {
return (
connection.isActive !== false &&
CONNECTED_STATUSES.has(getEffectiveProviderConnectionStatus(connection, now))
);
}
export function isProviderConnectionErrored(
connection: ProviderConnectionStatusLike,
now = Date.now()
): boolean {
return (
connection.isActive !== false &&
ERROR_STATUSES.has(getEffectiveProviderConnectionStatus(connection, now))
);
}