Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

19.5 KB
Raw Blame
import { useCallback } from "react";
import { useQueryClient } from "@tanstack/react-query";
import { toast } from "@/lib/toast";
import { useTranslation } from "react-i18next";
import {
  piApi,
  providersApi,
  settingsApi,
  openclawApi,
  type AppId,
} from "@/lib/api";
import type {
  Provider,
  UsageScript,
  OpenClawProviderConfig,
  OpenClawDefaultModel,
} from "@/types";
import { getOpencodeDeclaredModels } from "@/components/providers/forms/helpers/opencodeFormUtils";
import {
  savedOpenClawModelIds,
  type OpenClawSuggestedDefaults,
} from "@/config/openclawProviderPresets";
import type { ProviderEditorSave } from "@/lib/api/providers";
import { injectCodingPlanUsageScript } from "@/config/codingPlanProviders";
import {
  useAddProviderMutation,
  useUpdateProviderMutation,
  useDeleteProviderMutation,
  useSwitchProviderMutation,
} from "@/lib/query";
import { usageKeys } from "@/lib/query/usage";
import { routeSwitchNotification } from "@/utils/routeSwitchNotification";
import { extractErrorMessage } from "@/utils/errorUtils";
import { openclawKeys } from "@/hooks/useOpenClaw";
import { qoderApi } from "@/lib/api/qoder";
import { supportsOfficialProxyTakeover } from "@/utils/providerCapabilities";
import { getRoutingReason } from "@/utils/routingReason";
import { logFrontendInfo } from "@/lib/frontendLogger";
import { codexHistorySplitToastExtras } from "@/utils/codexHistorySplitHint";
import { appHas } from "@/config/appSpecs";

/**
 * Hook for managing provider actions (add, update, delete, switch)
 * Extracts business logic from App.tsx
 */
export function useProviderActions(
  activeApp: AppId,
  isProxyTakeover?: boolean,
) {
  const { t } = useTranslation();
  const queryClient = useQueryClient();

  const addProviderMutation = useAddProviderMutation(activeApp);
  const updateProviderMutation = useUpdateProviderMutation(activeApp);
  const deleteProviderMutation = useDeleteProviderMutation(activeApp);
  const switchProviderMutation = useSwitchProviderMutation(activeApp);

  // Claude 插件同步逻辑
  const syncClaudePlugin = useCallback(
    async (provider: Provider) => {
      if (activeApp !== "claude") return;

      try {
        const settings = await settingsApi.get();
        if (!settings?.enableClaudePluginIntegration) {
          return;
        }

        const isOfficial = provider.category === "official";
        await settingsApi.applyClaudePluginConfig({ official: isOfficial });

        // 静默执行,不显示成功通知
      } catch (error) {
        const detail =
          extractErrorMessage(error) ||
          t("notifications.syncClaudePluginFailed", {
            defaultValue: "同步 Claude 插件失败",
          });
        toast.error(detail, { duration: 4200 });
      }
    },
    [activeApp, t],
  );

  // 添加供应商
  const addProvider = useCallback(
    async (
      provider: Omit<Provider, "id"> & {
        providerKey?: string;
        suggestedDefaults?: OpenClawSuggestedDefaults;
        addToLive?: boolean;
        ensureClaudeDesktopOfficialSeed?: boolean;
        ensureGrokBuildOfficialSeed?: boolean;
        editorSave?: ProviderEditorSave;
      },
    ) => {
      const enhanced = injectCodingPlanUsageScript(activeApp, provider);
      await addProviderMutation.mutateAsync(enhanced);

      // OpenClaw: register models to allowlist after adding provider
      if (activeApp === "openclaw" && provider.suggestedDefaults) {
        const { model, modelCatalog } = provider.suggestedDefaults;
        let modelsRegistered = false;

        try {
          // 1. Merge model catalog (allowlist)
          if (modelCatalog && Object.keys(modelCatalog).length > 0) {
            const existingCatalog = (await openclawApi.getModelCatalog()) || {};
            // 新补的空条目不覆盖已有条目(别名、手加的字段);预设条目照旧覆盖
            const mergedCatalog = { ...existingCatalog };
            for (const [ref, entry] of Object.entries(modelCatalog)) {
              if (Object.keys(entry).length > 0 || !(ref in mergedCatalog)) {
                mergedCatalog[ref] = entry;
              }
            }
            await openclawApi.setModelCatalog(mergedCatalog);
            await queryClient.invalidateQueries({
              queryKey: openclawKeys.health,
            });
            modelsRegistered = true;
          }

          // 2. Set default model (only if not already set)
          if (model) {
            const existingDefault = await openclawApi.getDefaultModel();
            if (!existingDefault?.primary) {
              await openclawApi.setDefaultModel(model);
              await queryClient.invalidateQueries({
                queryKey: openclawKeys.health,
              });
            }
          }

          // Show success toast if models were registered
          if (modelsRegistered) {
            toast.success(
              t("notifications.openclawModelsRegistered", {
                defaultValue: "模型已注册到 /model 列表",
              }),
              { closeButton: true },
            );
          }
        } catch (error) {
          // Log warning but don't block main flow - provider config is already saved
          console.warn(
            "[OpenClaw] Failed to register models to allowlist:",
            error,
          );
        }
      }
    },
    [addProviderMutation, activeApp, queryClient, t],
  );

  // 更新供应商
  const updateProvider = useCallback(
    async (
      provider: Provider,
      originalId?: string,
      editorSave?: ProviderEditorSave,
    ) => {
      await updateProviderMutation.mutateAsync({
        provider,
        originalId,
        editorSave,
      });

      // OpenClaw: 编辑时新加的模型也登记到 /model 列表。只写进配置的供应商才登记,
      // 目录未设置时不新建,已有条目(别名、手加的字段)原样保留
      if (activeApp === "openclaw") {
        try {
          const live = await openclawApi.getLiveProvider(provider.id);
          const catalog = live && (await openclawApi.getModelCatalog());
          const added = catalog
            ? savedOpenClawModelIds(JSON.stringify(live))
                .map((id) => `${provider.id}/${id}`)
                .filter((ref) => !(ref in catalog))
            : [];
          if (catalog && added.length > 0) {
            await openclawApi.setModelCatalog({
              ...catalog,
              ...Object.fromEntries(added.map((ref) => [ref, {}])),
            });
            await queryClient.invalidateQueries({ queryKey: openclawKeys.all });
          }
        } catch (error) {
          console.warn(
            "[OpenClaw] Failed to register edited models to allowlist:",
            error,
          );
        }
      }

      // 更新托盘菜单(失败不影响主操作)
      try {
        await providersApi.updateTrayMenu();
      } catch (trayError) {
        console.error(
          "Failed to update tray menu after updating provider",
          trayError,
        );
      }
    },
    [updateProviderMutation, activeApp, queryClient],
  );

  // 切换供应商
  const switchProvider = useCallback(
    async (provider: Provider, options?: { acknowledgedRouting?: boolean }) => {
      // 必须开启当前应用的 takeover(只看全局进程会漏判别的应用已接管的情况)。
      // 确认框 F 里选了「仍然直连切换」的不再重复提醒。
      const routingReady =
        isProxyTakeover === true || options?.acknowledgedRouting === true;
      const proxyRequiredReason = routingReady
        ? null
        : getRoutingReason(activeApp, provider, t);

      if (proxyRequiredReason) {
        logFrontendInfo(
          `[SWITCH] ${activeApp} 直连切到 ${provider.id},提示需要路由:${proxyRequiredReason}`,
        );
        toast.warning(
          t("notifications.proxyRequiredForSwitch", {
            reason: proxyRequiredReason,
            defaultValue:
              "此供应商{{reason}},需要代理服务才能正常使用,请先启动代理",
          }),
        );
      }

      // Codex official account cards can reuse the active native ChatGPT login
      // through local routing. Other apps' official providers remain blocked.
      const officialSupportsTakeover = supportsOfficialProxyTakeover(
        activeApp,
        provider,
      );
      if (
        isProxyTakeover &&
        provider.category === "official" &&
        !officialSupportsTakeover
      ) {
        logFrontendInfo(
          `[SWITCH] ${activeApp} 拒绝切到 ${provider.id}:路由模式下不能切到官方供应商`,
        );
        toast.error(
          t("notifications.officialBlockedByProxy", {
            defaultValue:
              "代理接管模式下不能切换到官方供应商,使用代理访问官方 API 可能导致账号被封禁",
          }),
          { duration: 6000 },
        );
        return;
      }

      try {
        const result = await switchProviderMutation.mutateAsync(provider.id);
        await syncClaudePlugin(provider);

        // Surface switch warnings by code — a generic "backfill failed"
        // message for an auth-cleanup warning would point the user at the
        // wrong problem entirely.
        if (result?.warnings?.length) {
          const coded = [
            {
              code: "codex_auth_cleanup_failed",
              message: t("notifications.codexAuthCleanupFailed", {
                defaultValue:
                  "切换成功,但未能删除 auth.json,官方登录凭据仍留在磁盘上;如需彻底移除请手动删除 Codex 配置目录中的 auth.json",
              }),
            },
            {
              code: "omo_variant_cleanup_failed",
              message: t("notifications.omoVariantCleanupFailed", {
                defaultValue:
                  "切换成功,但未能删除另一个 OMO 变体(OMO / OMO Slim)的配置文件,OpenCode 可能同时加载两者;请在 OpenCode 配置目录中手动删除它",
              }),
            },
          ];
          for (const { code, message } of coded) {
            if (result.warnings.some((warning) => warning.startsWith(code))) {
              toast.warning(message, { duration: 6000 });
            }
          }
          const hasOtherWarnings = result.warnings.some(
            (warning) => !coded.some(({ code }) => warning.startsWith(code)),
          );
          if (hasOtherWarnings) {
            toast.warning(
              t("notifications.backfillWarning", {
                defaultValue:
                  "切换成功,但旧供应商配置回填失败,您手动修改的配置可能未保存",
              }),
              { duration: 5000 },
            );
          }
        }

        // 会话分组的提醒并进「切换成功」那条,不另弹
        const splitExtras = result?.codexHistorySplit
          ? codexHistorySplitToastExtras(result.codexHistorySplit, t)
          : undefined;

        // 若已弹过 proxyRequired 警告则不再弹 success
        if (!proxyRequiredReason) {
          let messageKey = "notifications.switchSuccess";
          let defaultMessage = "切换成功!";
          if (activeApp === "codex") {
            messageKey = "notifications.codexRestartRequired";
            defaultMessage = "切换成功,请重启客户端以生效";
          } else if (activeApp === "gemini") {
            messageKey = "notifications.geminiRestartRequired";
            defaultMessage = "切换成功,请重启 Gemini CLI 以生效";
          } else if (activeApp === "grokbuild") {
            messageKey = "notifications.grokBuildRestartRequired";
            defaultMessage = "切换成功,请重启 Grok Build 以生效";
          } else if (activeApp === "claude-desktop") {
            // 直连、路由换一家都改了 Desktop 的配置文件,它只在启动时读
            messageKey = "notifications.claudeDesktopRestartRequired";
            defaultMessage = "切换成功,重启 Claude Desktop 后生效";
          } else if (activeApp === "agy") {
            // CLI 每次启动都读 config.json;桌面版只在启动时读
            messageKey = "notifications.agyRestartRequired";
            defaultMessage =
              "切换成功。新开的 agy 立即生效,Antigravity 桌面版重启后生效";
          } else if (
            // 共存式应用走到这里是「添加」;Hermes、Kimi 用同一个动作设默认,仍算「切换」
            appHas(activeApp, "additive") &&
            activeApp !== "hermes" &&
            activeApp !== "kimi"
          ) {
            messageKey = "notifications.addToConfigSuccess";
            defaultMessage = "已添加到配置";
          }
          const message = result?.route
            ? routeSwitchNotification(result.route, activeApp, provider.name, t)
            : t(messageKey, { defaultValue: defaultMessage });
          toast.success(message, {
            closeButton: true,
            ...splitExtras,
          });
        } else if (splitExtras?.description) {
          // 没弹「切换成功」时单独提醒一句,仍然只是一行
          toast.info(splitExtras.description as string, {
            closeButton: true,
            duration: splitExtras.duration,
          });
        }
      } catch {
        // 错误提示由 mutation 处理
      }
    },
    [switchProviderMutation, syncClaudePlugin, activeApp, isProxyTakeover, t],
  );

  // 删除供应商
  const deleteProvider = useCallback(
    async (id: string) => {
      await deleteProviderMutation.mutateAsync(id);
    },
    [deleteProviderMutation],
  );

  // 保存用量脚本
  const saveUsageScript = useCallback(
    async (provider: Provider, script: UsageScript) => {
      try {
        const updatedProvider: Provider = {
          ...provider,
          meta: {
            ...provider.meta,
            usage_script: script,
          },
        };

        if (appHas(activeApp, "piFamily")) {
          await piApi.updateProviderUsageScript(provider.id, script, activeApp);
        } else {
          await providersApi.update(updatedProvider, activeApp);
        }
        await queryClient.invalidateQueries({
          queryKey: ["providers", activeApp],
        });
        // 🔧 保存用量脚本后,也应该失效该 provider 的用量查询缓存
        // 这样主页列表会使用新配置重新查询,而不是使用测试时的缓存
        await queryClient.invalidateQueries({
          queryKey: usageKeys.script(provider.id, activeApp),
        });
        await queryClient.invalidateQueries({
          queryKey: ["subscription", "quota", activeApp],
        });
        toast.success(
          t("provider.usageSaved", {
            defaultValue: "用量查询配置已保存",
          }),
          { closeButton: true },
        );
      } catch (error) {
        const detail =
          extractErrorMessage(error) ||
          t("provider.usageSaveFailed", {
            defaultValue: "用量查询配置保存失败",
          });
        toast.error(detail);
      }
    },
    [activeApp, queryClient, t],
  );

  // OpenCode:只写 opencode.json 顶层 model,small_model 不动
  const setOpenCodeDefaultModel = useCallback(
    async (provider: Provider, modelId?: string) => {
      const modelIds = getOpencodeDeclaredModels(
        provider.id,
        provider.settingsConfig,
      ).map((model) => model.id);
      if (modelIds.length === 0) {
        toast.error(
          t("notifications.openclawNoModels", {
            defaultValue: "该供应商没有配置模型",
          }),
        );
        return;
      }
      const selected = modelId ?? modelIds[0];
      if (!modelIds.includes(selected)) {
        toast.error(
          t("notifications.openclawModelNotFound", {
            defaultValue: "所选模型已不存在,请刷新后重试",
          }),
        );
        return;
      }

      try {
        await providersApi.setOpenCodeDefaultModel(
          `${provider.id}/${selected}`,
        );
        await queryClient.invalidateQueries({
          queryKey: ["opencodeDefaultModel"],
        });
        toast.success(
          t("notifications.openclawDefaultModelSet", {
            defaultValue: "已设为默认模型",
          }),
          { closeButton: true },
        );
      } catch (error) {
        toast.error(
          extractErrorMessage(error) ||
            t("notifications.openclawDefaultModelSetFailed", {
              defaultValue: "设置默认模型失败",
            }),
        );
      }
    },
    [queryClient, t],
  );

  // Set provider as default model (OpenClaw / OpenCode / Qoder)
  const setAsDefaultModel = useCallback(
    async (provider: Provider, modelId?: string) => {
      if (activeApp === "opencode") {
        await setOpenCodeDefaultModel(provider, modelId);
        return;
      }
      if (activeApp === "qoder") {
        // 自定义卡把 model.name 指到它的模型(没选就用它自己的默认模型),官方卡改成 auto
        try {
          await qoderApi.setDefaultModel(provider.id, modelId);
          toast.success(
            t("notifications.openclawDefaultModelSet", {
              defaultValue: "已设为默认模型",
            }),
            { closeButton: true },
          );
        } catch (error) {
          toast.error(
            extractErrorMessage(error) ||
              t("notifications.openclawDefaultModelSetFailed", {
                defaultValue: "设置默认模型失败",
              }),
          );
        } finally {
          await queryClient.invalidateQueries({
            queryKey: ["providers", "qoder"],
          });
          await providersApi.updateTrayMenu().catch(() => {});
        }
        return;
      }
      const config = provider.settingsConfig as OpenClawProviderConfig;
      if (!config.models || config.models.length === 0) {
        toast.error(
          t("notifications.openclawNoModels", {
            defaultValue: "该供应商没有配置模型",
          }),
        );
        return;
      }

      const selectedModel = modelId
        ? config.models.find((model) => model.id === modelId)
        : config.models[0];
      if (!selectedModel) {
        toast.error(
          t("notifications.openclawModelNotFound", {
            defaultValue: "所选模型已不存在,请刷新后重试",
          }),
        );
        return;
      }

      try {
        const primary = `${provider.id}/${selectedModel.id}`;
        const existingDefault = await openclawApi.getDefaultModel();
        const model: OpenClawDefaultModel = {
          ...(existingDefault ?? {}),
          primary,
        };
        if (existingDefault?.fallbacks) {
          model.fallbacks = existingDefault.fallbacks.filter(
            (fallback) => fallback !== primary,
          );
        }

        await openclawApi.setDefaultModel(model);
        await queryClient.invalidateQueries({
          queryKey: openclawKeys.defaultModel,
        });
        await queryClient.invalidateQueries({
          queryKey: openclawKeys.health,
        });
        toast.success(
          t("notifications.openclawDefaultModelSet", {
            defaultValue: "已设为默认模型",
          }),
          { closeButton: true },
        );
      } catch (error) {
        const detail =
          extractErrorMessage(error) ||
          t("notifications.openclawDefaultModelSetFailed", {
            defaultValue: "设置默认模型失败",
          });
        toast.error(detail);
      }
    },
    [activeApp, queryClient, setOpenCodeDefaultModel, t],
  );

  return {
    addProvider,
    updateProvider,
    switchProvider,
    deleteProvider,
    saveUsageScript,
    setAsDefaultModel,
  };
}