Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

21.9 KB
Raw Blame
"use client";

/**
 * useModelImportHandlers — Issue #3501 Phase 1k
 *
 * Owns import-progress state and handlers that were previously inline in
 * ProviderDetailPageClient:
 *  - importingModels, showImportModal, importProgress, togglingAutoSync
 *  - handleImportModels, handleCompatibleImportWithProgress, handleToggleAutoSync
 *  - canImportModels (derived), isAutoSyncEnabled (derived)
 *
 * Cycle-safe: imports only from leaf modules and React.
 * No import from ProviderDetailPageClient.
 */

import React, { useState } from "react";
import { settleBoundedSyncRequests } from "@/lib/providerModels/immediateSyncBatch";
import { extractApiErrorMessage } from "@/shared/http/apiErrorMessage";
import { providerText, type ProviderMessageTranslator } from "../providerPageHelpers";
import { classifyModelImport, resolveNoNewModelsPhase } from "./modelImportWarning";

interface NotifyStore {
  success: (message: string, title?: string) => number;
  error: (message: string, title?: string) => number;
  warning: (message: string, title?: string) => number;
  info: (message: string, title?: string) => number;
}

// ──── types ──────────────────────────────────────────────────────────────────

export interface ImportProgress {
  current: number;
  total: number;
  phase: "idle" | "fetching" | "importing" | "done" | "warning" | "error";
  status: string;
  logs: string[];
  error: string;
  importedCount: number;
}

export interface UseModelImportHandlersParams {
  providerId: string;
  models: Array<{ id: string; name?: string }>;
  modelMeta: { customModels: Array<{ id: string }>; modelCompatOverrides?: unknown[] };
  modelAliases: Record<string, string>;
  connections: Array<{
    id?: string;
    isActive?: boolean;
    providerSpecificData?: Record<string, unknown>;
  }>;
  isFreeNoAuth: boolean;
  handleSetAlias: (modelId: string, alias: string, providerAlias: string) => Promise<void>;
  fetchAliases: () => Promise<void>;
  fetchProviderModelMeta: () => Promise<void>;
  fetchConnections: () => Promise<void>;
  notify: NotifyStore;
  t: ProviderMessageTranslator;
  providerStorageAlias: string;
}

export interface UseModelImportHandlersReturn {
  importingModels: boolean;
  showImportModal: boolean;
  importProgress: ImportProgress;
  togglingAutoSync: boolean;
  togglingAutoFetchModels: boolean;
  canImportModels: boolean;
  isAutoSyncEnabled: boolean;
  isAutoFetchModelsEnabled: boolean;
  setShowImportModal: (v: boolean) => void;
  setImportProgress: React.Dispatch<React.SetStateAction<ImportProgress>>;
  handleImportModels: () => Promise<void>;
  handleCompatibleImportWithProgress: (connectionId: string) => Promise<void>;
  handleToggleAutoSync: () => Promise<void>;
  handleToggleAutoFetchModels: () => Promise<void>;
}

/** Only successfully enabled connections need an immediate upstream discovery. */
function newlyEnabledAutoSyncIds(
  connections: UseModelImportHandlersParams["connections"],
  results: PromiseSettledResult<Response>[]
): string[] {
  return connections.flatMap((connection, index) => {
    const result = results[index];
    if (connection.providerSpecificData?.autoSync === true || !connection.id) return [];
    return result?.status === "fulfilled" && result.value.ok ? [connection.id] : [];
  });
}

// ──── hook ───────────────────────────────────────────────────────────────────

export function useModelImportHandlers({
  providerId,
  models,
  modelMeta,
  modelAliases,
  connections,
  isFreeNoAuth,
  handleSetAlias,
  fetchAliases,
  fetchProviderModelMeta,
  fetchConnections,
  notify,
  t,
  providerStorageAlias,
}: UseModelImportHandlersParams): UseModelImportHandlersReturn {
  const [importingModels, setImportingModels] = useState(false);
  const [showImportModal, setShowImportModal] = useState(false);
  const [importProgress, setImportProgress] = useState<ImportProgress>({
    current: 0,
    total: 0,
    phase: "idle",
    status: "",
    logs: [],
    error: "",
    importedCount: 0,
  });
  const [togglingAutoSync, setTogglingAutoSync] = useState(false);
  const [togglingAutoFetchModels, setTogglingAutoFetchModels] = useState(false);

  // Derived
  const canImportModels = isFreeNoAuth || connections.some((conn) => conn.isActive !== false);
  const activeConnections = connections.filter((conn) => conn.isActive !== false);
  // Mixed-state semantics (design §6): the master toggle reads OFF if any active
  // connection has autoSync off; toggling from a mixed state turns all active ON.
  // No tri-state UI — the master toggle is a pure binary all-on switch.
  const isAutoSyncEnabled =
    activeConnections.length > 0 &&
    activeConnections.every((conn) => !!conn.providerSpecificData?.autoSync);
  // Discovery persists its response in the synced-model cache, so opt in on every
  // active connection before treating the provider-level control as enabled.
  const isAutoFetchModelsEnabled =
    activeConnections.length > 0 &&
    activeConnections.every((conn) => conn.providerSpecificData?.autoFetchModels === true);

  const handleImportModels = async () => {
    if (importingModels) return;
    const activeConnection = connections.find((conn) => conn.isActive !== false);
    if (!activeConnection && !isFreeNoAuth) return;
    const importTargetId = activeConnection?.id ?? providerId;

    setImportingModels(true);
    setShowImportModal(true);
    setImportProgress({
      current: 0,
      total: 0,
      phase: "fetching",
      status: t("fetchingModels"),
      logs: [],
      error: "",
      importedCount: 0,
    });

    try {
      const res = await fetch(`/api/providers/${importTargetId}/models?refresh=true`);
      const data = await res.json();
      if (!res.ok) {
        setImportProgress((prev) => ({
          ...prev,
          phase: "error",
          status: t("failedFetchModels"),
          error: data.error || t("failedImportModels"),
        }));
        return;
      }
      const fetchedModels = data.models || [];
      // Discovery persists its result even when no new models need importing.
      // Refresh the active listing so removals take effect without a page reload.
      await fetchProviderModelMeta();
      const existingIds = new Set([
        ...(modelMeta.customModels || []).map((m: any) => m.id),
        ...models.map((m: any) => m.id),
      ]);
      const classification = classifyModelImport({
        modelsData: data,
        fetchedModels,
        isKnownModel: (id) => existingIds.has(id),
      });
      const importWarning = classification.warning;
      // B-03 (#15159): when discovery fell back to a local/cache catalog the
      // headline must not read as an authoritative "nothing to import". The
      // server's warning becomes the status, with the success-flavoured line
      // demoted to a log entry — the operator sees the real cause first instead
      // of scrolling for it. Only keys that already exist in all 66 locales are
      // used; no new translation is introduced.
      if (classification.outcome === "no-models") {
        setImportProgress((prev) => ({
          ...prev,
          phase: "done",
          status: classification.degraded && importWarning ? importWarning : t("noModelsFound"),
          logs: [
            ...(classification.degraded && importWarning ? [t("noModelsFound")] : []),
            t("noModelsReturnedFromEndpoint"),
          ],
        }));
        return;
      }

      const newModels = classification.newModels;

      if (classification.outcome === "nothing-new") {
        // #15069: a degraded (local-catalog fallback) result is a terminal "warning", not a
        // success — the live provider API was never actually consulted.
        const noNewModelsPhase = resolveNoNewModelsPhase(
          classification.degraded ? importWarning : null
        );
        setImportProgress((prev) => ({
          ...prev,
          phase: noNewModelsPhase,
          status:
            classification.degraded && importWarning
              ? importWarning
              : t("allModelsAlreadyImported") || "All models already imported",
          logs: [
            ...(classification.degraded && importWarning ? [t("allModelsAlreadyImported")] : []),
            ...(importWarning && !(classification.degraded && importWarning)
              ? [importWarning]
              : []),
            t("noNewModelsToImport") || "No new models to import",
          ],
          importedCount: 0,
          total: 0,
          current: 0,
        }));
        return;
      }

      setImportProgress((prev) => ({
        ...prev,
        phase: "importing",
        total: newModels.length,
        current: 0,
        status: t("importingModelsProgress", { current: 0, total: newModels.length }),
        logs: [
          ...(importWarning ? [importWarning] : []),
          t("foundModelsStartingImport", { count: newModels.length }),
          ...(newModels.length < fetchedModels.length
            ? [
                t("skippingExistingModels", { count: fetchedModels.length - newModels.length }) ||
                  `Skipping ${fetchedModels.length - newModels.length} existing models`,
              ]
            : []),
        ],
      }));

      let importedCount = 0;
      const failures: string[] = [];
      for (let i = 0; i < newModels.length; i++) {
        const model = newModels[i];
        const rawId = model.id || model.name || model.model;
        // Same coercion classifyModelImport uses for the "already imported" check.
        const modelId = typeof rawId === "string" ? rawId : String(rawId ?? "");
        if (!modelId) continue;
        const parts = modelId.split("/");
        const baseAlias = parts[parts.length - 1];

        setImportProgress((prev) => ({
          ...prev,
          current: i + 1,
          status: t("importingModelsProgress", { current: i + 1, total: newModels.length }),
          logs: [...prev.logs, t("importingModelById", { modelId })],
        }));

        const createRes = await fetch("/api/provider-models", {
          method: "POST",
          headers: { "Content-Type": "application/json" },
          body: JSON.stringify({
            provider: providerId,
            modelId,
            modelName: model.name || modelId,
            source: "imported",
            ...(typeof model.apiFormat === "string" ? { apiFormat: model.apiFormat } : {}),
            ...(Array.isArray(model.supportedEndpoints)
              ? { supportedEndpoints: model.supportedEndpoints }
              : {}),
            ...(typeof model.endpointCapabilitySource === "string"
              ? { endpointCapabilitySource: model.endpointCapabilitySource }
              : {}),
            ...(typeof model.dimensions === "number" && model.dimensions > 0
              ? { dimensions: model.dimensions }
              : {}),
            ...(Array.isArray(model.supportedInputTypes)
              ? { supportedInputTypes: model.supportedInputTypes }
              : {}),
            ...(typeof model.modelType === "string" ? { modelType: model.modelType } : {}),
            ...(typeof model.inputTokenLimit === "number" && model.inputTokenLimit > 0
              ? { max_input_tokens: model.inputTokenLimit }
              : {}),
            ...(typeof model.targetFormat === "string" ? { targetFormat: model.targetFormat } : {}),
          }),
        });
        // A rejected row was not stored: do not alias it or count it as imported,
        // otherwise the dialog reports success while nothing reached the catalog.
        if (!createRes.ok) {
          const reason = extractApiErrorMessage(
            await createRes.json().catch(() => null),
            `HTTP ${createRes.status}`
          );
          failures.push(reason);
          setImportProgress((prev) => ({
            ...prev,
            logs: [...prev.logs, `✗ ${modelId}: ${reason}`],
          }));
          continue;
        }
        if (!modelAliases[baseAlias]) {
          await handleSetAlias(modelId, baseAlias, providerStorageAlias);
        }
        importedCount += 1;
      }

      await fetchAliases();

      if (importedCount === 0 && failures.length > 0) {
        setImportProgress((prev) => ({
          ...prev,
          phase: "error",
          current: newModels.length,
          status: t("failedImportModels"),
          error: failures[0],
          importedCount: 0,
        }));
        return;
      }

      setImportProgress((prev) => ({
        ...prev,
        phase: "done",
        current: newModels.length,
        status:
          importedCount > 0
            ? t("importSuccessCount", { count: importedCount })
            : t("noNewModelsAddedExisting"),
        logs: [
          ...prev.logs,
          importedCount > 0
            ? t("importDoneCount", { count: importedCount })
            : t("noNewModelsAdded"),
          ...(failures.length > 0 ? [t("bulkFailedCount", { count: failures.length })] : []),
        ],
        importedCount,
      }));

      if (importedCount > 0 && failures.length > 0) {
        // A reload would wipe the failure lines before they can be read.
        await fetchProviderModelMeta();
      } else if (importedCount > 0) {
        setTimeout(() => {
          window.location.reload();
        }, 2000);
      }
    } catch (error) {
      console.log("Error importing models:", error);
      setImportProgress((prev) => ({
        ...prev,
        phase: "error",
        status: t("importFailed"),
        error: error instanceof Error ? error.message : t("unexpectedErrorOccurred"),
      }));
    } finally {
      setImportingModels(false);
    }
  };

  const handleCompatibleImportWithProgress = async (
    connectionId: string,
    mode: "import" | "sync" = "import"
  ) => {
    setShowImportModal(true);
    setImportProgress({
      current: 0,
      total: 0,
      phase: "fetching",
      status: t("fetchingModels"),
      logs: [],
      error: "",
      importedCount: 0,
    });

    try {
      // mode "import" merges/appends; "sync" replaces the available list (used when
      // re-syncing after toggling "import only free models").
      const syncUrl =
        mode === "sync"
          ? `/api/providers/${connectionId}/sync-models`
          : `/api/providers/${connectionId}/sync-models?mode=import`;
      const response = await fetch(syncUrl, {
        method: "POST",
        signal: AbortSignal.timeout(60_000),
      });
      const data = await response.json();
      if (!response.ok) {
        throw new Error(data.error || t("failedImportModels"));
      }
      await fetchProviderModelMeta();
      await fetchAliases();

      if (data.freeFilterEmpty) {
        setImportProgress((prev) => ({
          ...prev,
          phase: "done",
          status: t("noFreeModelsFound"),
          logs: [t("noFreeModelsFound")],
          total: 0,
          current: 0,
          importedCount: 0,
        }));
        return;
      }

      const importedModels = Array.isArray(data.importedModels) ? data.importedModels : [];
      const importedCount =
        typeof data.importedCount === "number" ? data.importedCount : importedModels.length;
      const changedCount =
        typeof data.importedChanges?.total === "number"
          ? data.importedChanges.total
          : importedCount;
      const totalChangedCount =
        changedCount +
        (typeof data.customModelChanges?.total === "number" ? data.customModelChanges.total : 0);

      if (importedModels.length === 0) {
        setImportProgress((prev) => ({
          ...prev,
          phase: "done",
          status:
            importedCount > 0
              ? t("importSuccessCount", { count: importedCount })
              : t("noNewModelsAdded"),
          logs: [
            importedCount > 0
              ? t("importDoneCount", { count: importedCount })
              : t("noNewModelsAdded"),
          ],
          importedCount,
        }));
        if (totalChangedCount > 0) {
          setTimeout(() => {
            window.location.reload();
          }, 2000);
        }
        return;
      }

      setImportProgress((prev) => ({
        ...prev,
        phase: "done",
        total: importedModels.length,
        current: importedModels.length,
        status:
          importedCount > 0
            ? t("importSuccessCount", { count: importedCount })
            : t("noNewModelsAdded"),
        logs: [
          t("foundModelsStartingImport", { count: importedModels.length }),
          ...importedModels.map((model: any) =>
            t("importingModelById", { modelId: model.id || model.name || model.model })
          ),
          importedCount > 0
            ? t("importDoneCount", { count: importedCount })
            : t("noNewModelsAdded"),
        ],
        importedCount,
      }));

      if (totalChangedCount > 0) {
        setTimeout(() => {
          window.location.reload();
        }, 2000);
      }
    } catch (error) {
      console.log("Error importing models:", error);
      setImportProgress((prev) => ({
        ...prev,
        phase: "error",
        status: t("importFailed"),
        error: error instanceof Error ? error.message : t("unexpectedErrorOccurred"),
      }));
    }
  };

  const handleToggleAutoSync = async () => {
    if (togglingAutoSync) return;
    if (activeConnections.length === 0) return;
    setTogglingAutoSync(true);
    try {
      const newValue = !isAutoSyncEnabled;
      const activeWithId = activeConnections.filter((conn) => conn.id);
      if (activeWithId.length === 0) return;
      const results = await Promise.allSettled(
        activeWithId.map((conn) =>
          fetch(`/api/providers/${conn.id}`, {
            method: "PUT",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({
              providerSpecificData: {
                ...(conn.providerSpecificData || {}),
                autoSync: newValue,
              },
            }),
          })
        )
      );
      // Only newly enabled connections need an immediate sync; existing
      // enabled accounts remain on their scheduler cadence.
      const syncIds = newValue ? newlyEnabledAutoSyncIds(activeWithId, results) : [];
      // A provider may have many accounts sharing one upstream/IP. Fan out
      // the setting writes as before, but cap *live* discovery calls at two
      // to avoid a burst that can trigger shared upstream rate limits.
      const syncResults = await settleBoundedSyncRequests(syncIds, async (id) => {
        const response = await fetch(`/api/providers/${id}/sync-models`, {
          method: "POST",
          signal: AbortSignal.timeout(60_000),
        });
        if (!response.ok) throw new Error(`HTTP ${response.status}`);
      });
      await fetchConnections();
      if (syncIds.length > 0) await fetchProviderModelMeta();
      const syncFailed = syncResults.some((r) => r.status === "rejected");
      const fulfilled = results.filter((r) => r.status === "fulfilled" && r.value.ok).length;
      if (fulfilled === results.length && !syncFailed) {
        notify[newValue ? "success" : "info"](
          newValue ? t("autoSyncEnabled") : t("autoSyncDisabled")
        );
      } else if (fulfilled === 0) {
        notify.error(t("autoSyncToggleFailed"));
      } else {
        notify.warning(
          t(
            syncFailed && fulfilled === results.length
              ? "autoSyncImmediatePartialFailure"
              : "autoSyncPartialFailure"
          )
        );
      }
    } catch (error) {
      console.error("Error toggling auto-sync:", error);
      notify.error(t("autoSyncToggleFailed"));
    } finally {
      setTogglingAutoSync(false);
    }
  };

  const handleToggleAutoFetchModels = async () => {
    if (togglingAutoFetchModels) return;
    const activeWithId = activeConnections.filter((conn) => conn.id);
    if (activeWithId.length === 0) return;

    setTogglingAutoFetchModels(true);
    try {
      const newValue = !isAutoFetchModelsEnabled;
      const results = await Promise.allSettled(
        activeWithId.map((conn) =>
          fetch(`/api/providers/${conn.id}`, {
            method: "PUT",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({
              providerSpecificData: {
                ...(conn.providerSpecificData || {}),
                autoFetchModels: newValue,
              },
            }),
          })
        )
      );
      await fetchConnections();
      const fulfilled = results.filter((result) => {
        return result.status === "fulfilled" && result.value.ok;
      }).length;
      if (fulfilled === results.length) {
        notify[newValue ? "success" : "info"](
          newValue
            ? providerText(t, "autoFetchModelsEnabled", "Upstream model auto-fetch enabled")
            : providerText(t, "autoFetchModelsDisabled", "Upstream model auto-fetch disabled")
        );
      } else if (fulfilled === 0) {
        notify.error(
          providerText(
            t,
            "autoFetchModelsToggleFailed",
            "Failed to toggle upstream model auto-fetch"
          )
        );
      } else {
        notify.warning(
          providerText(
            t,
            "autoFetchModelsPartialFailure",
            "Some connections updated, but upstream model auto-fetch was not changed everywhere"
          )
        );
      }
    } catch (error) {
      console.error("Error toggling upstream model auto-fetch:", error);
      notify.error(
        providerText(t, "autoFetchModelsToggleFailed", "Failed to toggle upstream model auto-fetch")
      );
    } finally {
      setTogglingAutoFetchModels(false);
    }
  };

  return {
    importingModels,
    showImportModal,
    importProgress,
    togglingAutoSync,
    togglingAutoFetchModels,
    canImportModels,
    isAutoSyncEnabled,
    isAutoFetchModelsEnabled,
    setShowImportModal,
    setImportProgress,
    handleImportModels,
    handleCompatibleImportWithProgress,
    handleToggleAutoSync,
    handleToggleAutoFetchModels,
  };
}