Files
aigc-home/calliope-web/src/lib/endpointKind.ts
T

45 lines
1.2 KiB
TypeScript

import { t } from "$lib/i18n.svelte";
export type EndpointKind = "local" | "lan" | "cloud";
/** 本机 / 局域网 / 云端 by host — drives the blue (local, LAN) vs purple (cloud) tags (§2.1). */
export function classifyEndpoint(rawUrl: string): EndpointKind {
try {
const u = new URL(rawUrl.trim());
const h = u.hostname.toLowerCase();
if (h === "localhost" || h === "::1" || h.startsWith("127."))
return "local";
const m = h.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
if (m) {
const a = Number(m[1]);
const b = Number(m[2]);
if (
a === 10 ||
(a === 172 && b >= 16 && b <= 31) ||
(a === 192 && b === 168)
)
return "lan";
if (a === 169 && b === 254) return "lan";
return "cloud";
}
if (
!h.includes(".") ||
h.endsWith(".local") ||
h.endsWith(".lan") ||
h.endsWith(".internal")
)
return "lan";
return "cloud";
} catch {
return "cloud";
}
}
export function endpointTypeLabel(kind: EndpointKind): string {
return kind === "local"
? t("models.typeLocal")
: kind === "lan"
? t("models.typeLan")
: t("models.typeCloud");
}