mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-06 07:12:12 +03:00
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>
This commit is contained in:
46
src/shared/utils/providerConnectionStatus.ts
Normal file
46
src/shared/utils/providerConnectionStatus.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
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))
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user