mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-01 21:02:12 +03:00
* 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>
47 lines
1.4 KiB
TypeScript
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))
|
|
);
|
|
}
|