| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error";
|
| import { getServiceModels, saveServiceModels, type ServiceModel } from "@/lib/db/serviceModels";
|
| import { updateVersionManagerTool } from "@/lib/db/versionManager";
|
|
|
| const SYNC_INTERVAL_MS = 5 * 60 * 1000;
|
| const FETCH_TIMEOUT_MS = 10_000;
|
|
|
| const activeTimers = new Map<string, ReturnType<typeof setInterval>>();
|
|
|
| |
| |
| |
|
|
| export async function syncServiceModels(
|
| tool: string,
|
| baseUrl: string,
|
| apiKey: string
|
| ): Promise<number> {
|
| try {
|
| const res = await fetch(`${baseUrl}/v1/models`, {
|
| headers: { Authorization: `Bearer ${apiKey}` },
|
| signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
|
| });
|
|
|
| if (!res.ok) {
|
| console.warn(`[ModelSync:${tool}] /v1/models returned HTTP ${res.status}`);
|
| return -1;
|
| }
|
|
|
| const json = await res.json();
|
| const data: unknown = Array.isArray(json?.data) ? json.data : Array.isArray(json) ? json : [];
|
| const models = (data as unknown[])
|
| .filter(
|
| (m): m is ServiceModel =>
|
| typeof m === "object" &&
|
| m !== null &&
|
| typeof (m as Record<string, unknown>).id === "string"
|
| )
|
| .map((m) => ({
|
| ...m,
|
|
|
|
|
| id: m.id.startsWith(`${tool}/`) ? m.id : `${tool}/${m.id}`,
|
| }));
|
|
|
| saveServiceModels(tool, models);
|
| await updateVersionManagerTool(tool, { lastSyncAt: new Date().toISOString() });
|
|
|
| console.log(`[ModelSync:${tool}] synced ${models.length} model(s)`);
|
| return models.length;
|
| } catch (err) {
|
| const msg = sanitizeErrorMessage(err instanceof Error ? err.message : String(err));
|
| console.warn(`[ModelSync:${tool}] fetch failed: ${msg}`);
|
| return -1;
|
| }
|
| }
|
|
|
| |
| |
| |
|
|
| export function scheduleServiceModelSync(
|
| tool: string,
|
| baseUrl: string,
|
| apiKey: string,
|
| intervalMs = SYNC_INTERVAL_MS
|
| ): void {
|
| if (activeTimers.has(tool)) return;
|
|
|
|
|
| syncServiceModels(tool, baseUrl, apiKey).catch(() => {});
|
|
|
| const timer = setInterval(() => {
|
| syncServiceModels(tool, baseUrl, apiKey).catch(() => {});
|
| }, intervalMs);
|
| timer.unref?.();
|
|
|
| activeTimers.set(tool, timer);
|
| console.log(`[ModelSync:${tool}] scheduler started (interval ${intervalMs / 1000}s)`);
|
| }
|
|
|
| |
| |
|
|
| export function stopServiceModelSync(tool: string): void {
|
| const timer = activeTimers.get(tool);
|
| if (!timer) return;
|
| clearInterval(timer);
|
| activeTimers.delete(tool);
|
| console.log(`[ModelSync:${tool}] scheduler stopped`);
|
| }
|
|
|
|
|
| export { getServiceModels };
|
|
|