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

49.6 KB
Raw Blame
import type { TFunction } from "i18next";
import type { Provider } from "@/types";
import type { AppId } from "@/lib/api";
import type { AppMode } from "@/types/proxy";
import { resolveManagedAccountId } from "@/lib/authBinding";
import {
  boundWorkBuddyAccountId,
  isWorkBuddyOfficial,
  type WorkBuddyOfficialState,
} from "@/components/providers/workbuddyOfficial";
import {
  isOfficialAccount,
  providerNeedsRouting,
  supportsFailover,
  supportsOfficialProxyTakeover,
} from "@/utils/providerCapabilities";
import { appHas } from "@/config/appSpecs";

/**
 * 供应商卡片怎么画(v7):当前那张的模式色、状态文字或按钮、徽标。由列表按应用和正在查看的
 * 模式算好交给卡片,卡片本身不再判断模式。
 */
export type CardTone = "direct" | "route" | "stack";

export type ChipTone =
  | "outline"
  | "direct"
  | "route"
  | "stack"
  | "success"
  | "warning"
  | "danger";

export interface CardChip {
  key: string;
  label: string;
  tone: ChipTone;
  title?: string;
}

export interface CardMenuOption {
  key: string;
  label: string;
  detail?: string;
  onSelect: () => void;
}

/** 「更多」菜单里跟当前模式有关的一项。 */
export interface CardMenuItem {
  key: string;
  label: string;
  /** 不能点的原因:照常列出来,原因写在名字下面 */
  disabledReason?: string;
  onSelect: () => void;
  /** 展开成子菜单选一项(OpenClaw / OpenCode 的「设为默认」选默认模型) */
  submenu?: { title: string; options: CardMenuOption[] };
}

export interface CardButton {
  key: string;
  label: string;
  onClick: () => void;
  /** 不能点的原因:按钮照常画出来,原因挂在按钮的说明卡上 */
  disabledReason?: string;
}

export interface CardPresentation {
  /** 当前那张:模式色边框 + 淡底;共存式应用的默认那家(OpenClaw / OpenCode / Kimi 默认、Hermes 当前)也用 direct */
  tone?: CardTone;
  /** 主操作位换成状态文字(使用中 / 路由中 / 当前默认 / 已添加…) */
  status?: { label: string; dot: CardTone | "muted" };
  buttons: CardButton[];
  /** 「更多」菜单最前面、跟当前模式有关的操作(聚合页、OpenClaw / OpenCode 的「设为默认」) */
  menuItems?: CardMenuItem[];
  chips: CardChip[];
  /** 整卡淡一些(不能用于当前模式的官方订阅、Hermes 托管) */
  dim?: boolean;
  /** 故障转移队列里的上移 / 下移(键盘也能排序) */
  move?: { onUp?: () => void; onDown?: () => void };
  /** 在队列里且路由服务在跑时显示健康状态 */
  showHealth?: boolean;
  /** 编辑 / 删除不可用的原因(当前那家不能删、Hermes 托管只能在 Web UI 改…) */
  editDisabledReason?: string;
  deleteDisabledReason?: string;
}

export interface ProviderSection {
  key: string;
  title?: string;
  help?: { title: string; body: string };
  /** 标题后面一句常驻的灰字说明(DSH:Web 和 Desktop 共用这份列表) */
  note?: string;
  emptyText?: string;
  items: { provider: Provider; presentation: CardPresentation }[];
}

/**
 * Antigravity 只能直连的第三方卡:没填 Key,或用了自定义 CA 证书(代理连上游不带用户的
 * CA)。返回原因的文案 key;同后端 `agy_direct::can_route`。
 */
function agyDirectOnlyReason(app: AppId, provider: Provider): string | null {
  if (app !== "agy" || isOfficialAccount(app, provider)) return null;
  const settings = provider.settingsConfig as {
    env?: { GEMINI_API_KEY?: unknown };
    gateway?: { caCertPath?: unknown };
  };
  const text = (value: unknown) =>
    typeof value === "string" ? value.trim() : "";
  if (!text(settings?.env?.GEMINI_API_KEY))
    return "providerCard.reason.noRouteNoKey";
  if (text(settings?.gateway?.caCertPath))
    return "providerCard.reason.noRouteCaCert";
  return null;
}

/** Antigravity 卡的上游协议(同后端 `row_wire_protocol`):没写就是 genai。 */
export function agyWireProtocol(provider: Provider): "genai" | "openai" {
  const gateway = (provider.settingsConfig as { gateway?: unknown })?.gateway;
  const protocol =
    gateway && typeof gateway === "object"
      ? (gateway as { wireProtocol?: unknown }).wireProtocol
      : undefined;
  return protocol === "openai" ? "openai" : "genai";
}

/**
 * Antigravity 的故障转移队列只收和路由那家同协议的卡(同后端 `same_wire_protocol`):
 * agy 一个网关只讲一种协议,代理不转格式。不知道路由那家时不拦。
 */
function otherWireProtocol(
  app: AppId,
  route: Provider | undefined,
  provider: Provider,
): boolean {
  return (
    app === "agy" &&
    route !== undefined &&
    agyWireProtocol(route) !== agyWireProtocol(provider)
  );
}

/**
 * 走路由时不能用的卡:官方订阅(Codex 官方账号可以,靠客户端自己的登录),以及
 * Antigravity 只能直连的卡。
 */
export function blockedFromRouting(app: AppId, provider: Provider): boolean {
  return (
    (isOfficialAccount(app, provider) &&
      !supportsOfficialProxyTakeover(app, provider)) ||
    agyDirectOnlyReason(app, provider) !== null
  );
}

/** [`blockedFromRouting`] 的卡为什么不能路由。 */
function noRouteReason(app: AppId, provider: Provider, t: TFunction): string {
  const agy = agyDirectOnlyReason(app, provider);
  return agy ? t(agy) : t("providerCard.reason.noRoute");
}

// ─── 切换式应用(Claude Code / Codex / Gemini CLI / Grok Build)──────────────

/**
 * 故障转移队列里把一家上移 / 下移一格后,各家要写回的 sortIndex。队列顺序就是供应商的排序:
 * 按队列成员原来占着的位置重新分配。位置必须两两不同,否则换位后并列、顺序看起来没动——
 * 两家 sortIndex 相同,或新加的供应商没有 sortIndex、拿下标兜底时撞上别家的值,都会这样。
 * 移不动(不在队列 / 已到头)返回 null。
 */
export function queueMoveSortUpdates(
  items: { providerId: string; sortIndex?: number }[],
  providers: Record<string, Pick<Provider, "sortIndex">>,
  providerId: string,
  delta: -1 | 1,
): { id: string; sortIndex: number }[] | null {
  const index = items.findIndex((item) => item.providerId === providerId);
  const target = index + delta;
  if (index < 0 || target < 0 || target >= items.length) return null;
  const order = items.map((item) => item.providerId);
  order.splice(index, 1);
  order.splice(target, 0, providerId);
  const slots = items
    .map(
      (item, i) => item.sortIndex ?? providers[item.providerId]?.sortIndex ?? i,
    )
    .sort((a, b) => a - b);
  for (let i = 1; i < slots.length; i++) {
    if (slots[i] <= slots[i - 1]) slots[i] = slots[i - 1] + 1;
  }
  return order.map((id, i) => ({ id, sortIndex: slots[i] }));
}

export interface SwitchModeInput {
  app: AppId;
  t: TFunction;
  providers: Provider[];
  active: AppMode;
  view: AppMode;
  directId: string | null;
  routeId: string | null;
  failoverOn: boolean;
  /** 队列里的 id,按优先级 */
  queue: string[];
  /**
   * 正在路由的那家(取自没被搜索过滤的全部供应商)。Antigravity 的队列按它的协议拦卡,
   * 别的应用用不到
   */
  routeProvider?: Provider;
  /** 聚合名单(含默认那家):id → 发布的模型数 */
  stackMembers: Map<string, number>;
  /** 需要路由的原因(悬停「需要路由」时的说明) */
  routingReason: (provider: Provider) => string;
  serviceRunning: boolean;
  actions: {
    switchDirect: (provider: Provider) => void;
    needsRouteDialog: (provider: Provider) => void;
    exitAndUse: (provider: Provider) => void;
    routeTo: (provider: Provider) => void;
    queueAdd: (provider: Provider) => void;
    queueRemove: (provider: Provider) => void;
    queueMove: (provider: Provider, delta: -1 | 1) => void;
    stackAdd: (provider: Provider) => void;
    stackRemove: (provider: Provider) => void;
    stackSetDefault: (provider: Provider) => void;
  };
}

export function buildSwitchSections(input: SwitchModeInput): ProviderSection[] {
  const { t, active, directId, routeId } = input;
  // 后端 is_referenced:直连指针指着的、路由 / 聚合模式下正在路由的,都删不掉。
  // 不管当前看的是哪个视图都要标出来,否则确认后后端拒绝、确认框还卡在原地
  // 路由 / 聚合时直连那家不算「正在用」,路由页卡上也不标,单独说明为什么删不掉
  const deleteBlockedReason = (id: string) => {
    if (active !== "direct" && id === routeId)
      return t("providerCard.reason.inUseCannotDelete");
    if (id === directId)
      return active === "direct"
        ? t("providerCard.reason.inUseCannotDelete")
        : t("providerCard.reason.directDefaultCannotDelete");
    return undefined;
  };
  return buildSwitchSectionsByView(input).map((section) => ({
    ...section,
    items: section.items.map((item) => {
      const reason = deleteBlockedReason(item.provider.id);
      return reason
        ? {
            ...item,
            presentation: {
              ...item.presentation,
              deleteDisabledReason: reason,
            },
          }
        : item;
    }),
  }));
}

function buildSwitchSectionsByView(input: SwitchModeInput): ProviderSection[] {
  const {
    app,
    t,
    providers,
    active,
    view,
    directId,
    routeId,
    failoverOn,
    queue,
    routeProvider,
    stackMembers,
    actions,
  } = input;
  // 早期绑定托管账号的 Codex 官方卡没有 category,按身份认
  const official = (p: Provider) => isOfficialAccount(app, p);

  const chip = {
    official: (): CardChip => ({
      key: "official",
      label: t("providerCard.chip.official"),
      tone: "outline",
    }),
    needsRoute: (p: Provider): CardChip => ({
      key: "needsRoute",
      label: t("providerCard.chip.needsRoute"),
      tone: "outline",
      title: input.routingReason(p),
    }),
    // 只在直连页上标:不用点开「回到直连」的确认框,在列表里就看得出会落到哪家。
    // 路由页上没有跟直连那家有关的操作,不标
    directWhenDirect: (): CardChip => ({
      key: "direct",
      label: t("providerCard.chip.directWhenDirect"),
      tone: "direct",
      title: t("provider.directProviderHint"),
    }),
  };
  const base = (p: Provider) => {
    const chips: CardChip[] = [];
    if (official(p)) chips.push(chip.official());
    if (providerNeedsRouting(app, p)) chips.push(chip.needsRoute(p));
    return chips;
  };
  const officialOnly = (p: Provider) => (official(p) ? [chip.official()] : []);
  const blocked = (p: Provider, label: string, reason: string) => ({
    provider: p,
    presentation: {
      chips: officialOnly(p),
      dim: true,
      buttons: [
        {
          key: "blocked",
          label,
          onClick: () => undefined,
          disabledReason: reason,
        },
      ],
    } satisfies CardPresentation,
  });

  if (view === "direct") {
    return [
      {
        key: "all",
        items: providers.map((p) => {
          const chips = base(p);
          if (active === "direct") {
            if (p.id === directId) {
              return {
                provider: p,
                presentation: {
                  tone: "direct",
                  status: {
                    label: t("providerCard.status.inUse"),
                    dot: "direct",
                  },
                  chips,
                  buttons: [],
                },
              };
            }
            return {
              provider: p,
              presentation: {
                chips,
                buttons: [
                  {
                    key: "switch",
                    label: t("providerCard.action.switch"),
                    onClick: () =>
                      providerNeedsRouting(app, p)
                        ? actions.needsRouteDialog(p)
                        : actions.switchDirect(p),
                  },
                ],
              },
            };
          }
          // 实际在路由 / 聚合,正在查看直连
          if (p.id === directId) chips.push(chip.directWhenDirect());
          return {
            provider: p,
            presentation: {
              chips,
              buttons: [
                {
                  key: "exitAndUse",
                  label: t("providerCard.action.backToDirectAndUse"),
                  onClick: () => actions.exitAndUse(p),
                  disabledReason: providerNeedsRouting(app, p)
                    ? t("providerCard.reason.needsRoute", {
                        reason: input.routingReason(p),
                      })
                    : undefined,
                },
              ],
            },
          };
        }),
      },
    ];
  }

  if (view === "route") {
    if (active === "route" && !failoverOn) {
      return [
        {
          key: "all",
          items: providers.map((p) => {
            if (blockedFromRouting(app, p))
              return blocked(
                p,
                t("providerCard.action.routeHere"),
                noRouteReason(app, p, t),
              );
            const chips = officialOnly(p);
            if (p.id === routeId) {
              return {
                provider: p,
                presentation: {
                  tone: "route",
                  status: {
                    label: t("providerCard.status.routing"),
                    dot: "route",
                  },
                  chips,
                  buttons: [],
                },
              };
            }
            return {
              provider: p,
              presentation: {
                chips,
                buttons: [
                  {
                    key: "routeHere",
                    label: t("providerCard.action.routeHere"),
                    onClick: () => actions.routeTo(p),
                  },
                ],
              },
            };
          }),
        },
      ];
    }

    if (active === "route") {
      // 故障转移开着:队列 + 不在队列。P 序号和上下移按完整队列算,`providers`
      // 可能被搜索过滤过、只决定画哪些卡;「路由中」是后端记下的那家(转移成功后
      // 会换),不一定是队首
      const byId = new Map(providers.map((p) => [p.id, p]));
      const routing =
        routeId !== null && queue.includes(routeId) ? routeId : queue[0];
      const queueItems = queue.flatMap((id, index) => {
        const p = byId.get(id);
        if (!p) return [];
        const current = id === routing;
        return [
          {
            provider: p,
            presentation: {
              tone: current ? "route" : undefined,
              status: current
                ? {
                    label: t("providerCard.status.routing"),
                    dot: "route" as const,
                  }
                : undefined,
              chips: [
                {
                  key: "priority",
                  label: `P${index + 1}`,
                  tone: "outline" as const,
                },
                // 入队之后路由换了协议:还在队列里,但请求不会转给它
                ...(otherWireProtocol(app, routeProvider, p)
                  ? [
                      {
                        key: "otherProtocol",
                        label: t("providerCard.chip.otherProtocol"),
                        tone: "warning" as const,
                        title: t("providerCard.reason.otherProtocolSkipped"),
                      },
                    ]
                  : []),
              ],
              showHealth: input.serviceRunning,
              buttons: [
                {
                  key: "queueRemove",
                  label: t("providerCard.action.removeFromQueue"),
                  onClick: () => actions.queueRemove(p),
                },
              ],
              move: {
                onUp: index > 0 ? () => actions.queueMove(p, -1) : undefined,
                onDown:
                  index < queue.length - 1
                    ? () => actions.queueMove(p, 1)
                    : undefined,
              },
            } satisfies CardPresentation,
          },
        ];
      });
      const rest = providers
        .filter((p) => !queue.includes(p.id))
        .map((p) => {
          if (blockedFromRouting(app, p))
            return blocked(
              p,
              t("providerCard.action.addToQueue"),
              noRouteReason(app, p, t),
            );
          // Codex 官方账号卡能路由,但后端不让它进队列(靠客户端自己的登录,不能和别家轮换)
          if (!supportsFailover(app, p))
            return blocked(
              p,
              t("providerCard.action.addToQueue"),
              t("providerCard.reason.noFailover"),
            );
          if (otherWireProtocol(app, routeProvider, p))
            return blocked(
              p,
              t("providerCard.action.addToQueue"),
              t("providerCard.reason.noFailoverProtocol"),
            );
          return {
            provider: p,
            presentation: {
              chips: officialOnly(p),
              buttons: [
                {
                  key: "queueAdd",
                  label: t("providerCard.action.addToQueue"),
                  onClick: () => actions.queueAdd(p),
                },
              ],
            } satisfies CardPresentation,
          };
        });
      return [
        {
          key: "queue",
          title: t("providerCard.section.queue", { count: queueItems.length }),
          help: {
            title: t("providerCard.section.queueHelpTitle"),
            body: t("providerCard.section.queueHelp"),
          },
          emptyText: t("providerCard.section.queueEmpty"),
          items: queueItems,
        },
        {
          key: "rest",
          title: t("providerCard.section.notInQueue", { count: rest.length }),
          items: rest,
        },
      ];
    }

    // 实际在直连 / 聚合,正在查看路由:行上没有主操作,要生效只有通知条的「开始路由」,
    // 路由到哪家在确认框里选(预选上次那家,框里看得到,卡上不再标)。不能路由的官方订阅
    // 没有按钮可挂原因,原因挂在「官方」徽标上
    return [
      {
        key: "all",
        items: providers.map((p) => {
          if (blockedFromRouting(app, p))
            return {
              provider: p,
              presentation: {
                // Antigravity 只能直连的第三方卡没有「官方」徽标可挂原因,只置灰
                chips: isOfficialAccount(app, p)
                  ? [{ ...chip.official(), title: noRouteReason(app, p, t) }]
                  : officialOnly(p),
                dim: true,
                buttons: [],
              } satisfies CardPresentation,
            };
          return {
            provider: p,
            presentation: {
              chips: officialOnly(p),
              buttons: [],
            } satisfies CardPresentation,
          };
        }),
      },
    ];
  }

  // view === "stack"
  const on = active === "stack";
  const defaultId = (() => {
    const candidate = on ? routeId : (routeId ?? directId);
    const p = providers.find((x) => x.id === candidate);
    return p && !blockedFromRouting(app, p) ? p.id : null;
  })();
  const defaultProvider = providers.find((p) => p.id === defaultId);
  // 协议按默认那家比:搜索把它过滤掉时,正在聚合就用路由那家(同一个指针)
  const protocolRef =
    defaultProvider ??
    (on && routeProvider?.id === routeId ? routeProvider : undefined);
  // 行上的主操作只有名单的添加 / 移除;「设为默认」在「更多」菜单里:进了聚合当场生效,
  // 在直连时只记下选择(切换时的确认框里还能改)。正在路由时不能点:默认和路由目标是同一个
  // 指针,改了就是当场换路由
  const setDefaultItems = (p: Provider): CardMenuItem[] => [
    {
      key: "setDefault",
      label: t("providerCard.action.setDefault"),
      disabledReason:
        active === "route"
          ? t("providerCard.reason.defaultWhileRouting")
          : undefined,
      onSelect: () => actions.stackSetDefault(p),
    },
  ];
  const memberIds = providers
    .filter((p) => p.id !== defaultId && stackMembers.has(p.id))
    .map((p) => p.id);
  const modelsChip = (p: Provider): CardChip => {
    const count = stackMembers.get(p.id) ?? 0;
    return count > 0
      ? {
          key: "models",
          label: t("providerCard.chip.models", { count }),
          tone: "outline",
        }
      : {
          key: "models",
          label: t("providerCard.chip.noModels"),
          tone: "warning",
          title: t("provider.stackBadgeNoModels"),
        };
  };

  const defaultItems = defaultProvider
    ? [
        {
          provider: defaultProvider,
          presentation: {
            tone: on ? ("stack" as const) : undefined,
            status: on
              ? {
                  label: t("providerCard.status.currentDefault"),
                  dot: "stack" as const,
                }
              : {
                  label: t("providerCard.status.defaultAfterSwitch"),
                  dot: "muted" as const,
                },
            chips: [
              {
                key: "default",
                label: t("providerCard.chip.default"),
                tone: on ? ("outline" as const) : ("stack" as const),
              },
              ...officialOnly(defaultProvider),
            ],
            buttons: [],
          } satisfies CardPresentation,
        },
      ]
    : [];

  const memberItems = memberIds.map((id) => {
    const p = providers.find((x) => x.id === id)!;
    return {
      provider: p,
      presentation: {
        // Antigravity:和默认那家协议不同的成员留在名单里,但不发布模型(同后端
        // `stack::agy_unpublished_reason`),换回同协议的默认后恢复
        chips: [
          otherWireProtocol(app, protocolRef, p)
            ? {
                key: "otherProtocol",
                label: t("providerCard.chip.otherProtocol"),
                tone: "warning" as const,
                title: t("providerCard.reason.stackProtocolUnpublished"),
              }
            : modelsChip(p),
        ],
        menuItems: setDefaultItems(p),
        buttons: [
          {
            key: "remove",
            label: t("providerCard.action.remove"),
            onClick: () => actions.stackRemove(p),
          },
        ],
      } satisfies CardPresentation,
    };
  });

  const availItems = providers
    .filter((p) => p.id !== defaultId && !memberIds.includes(p.id))
    .map((p) => {
      if (blockedFromRouting(app, p)) {
        const agy = agyDirectOnlyReason(app, p);
        return blocked(
          p,
          t("providerCard.action.add"),
          t(agy ?? "providerCard.reason.noStack"),
        );
      }
      if (otherWireProtocol(app, protocolRef, p)) {
        return blocked(
          p,
          t("providerCard.action.add"),
          t("providerCard.reason.noStackProtocol"),
        );
      }
      if (official(p)) {
        return {
          provider: p,
          presentation: {
            chips: [chip.official()],
            menuItems: setDefaultItems(p),
            buttons: [
              {
                key: "add",
                label: t("providerCard.action.add"),
                onClick: () => undefined,
                disabledReason: t("providerCard.reason.officialStack"),
              },
            ],
          } satisfies CardPresentation,
        };
      }
      return {
        provider: p,
        presentation: {
          chips: providerNeedsRouting(app, p) ? [chip.needsRoute(p)] : [],
          // 还没添加的也能直接设为默认:后端会把默认那家一起加进名单
          menuItems: setDefaultItems(p),
          buttons: [
            {
              key: "add",
              label: t("providerCard.action.add"),
              onClick: () => actions.stackAdd(p),
            },
          ],
        } satisfies CardPresentation,
      };
    });

  return [
    {
      key: "default",
      title: on
        ? t("providerCard.section.stackDefault")
        : t("providerCard.section.stackDefaultAfterSwitch"),
      help: {
        title: t("providerCard.section.stackDefault"),
        body: t(
          app === "claude-desktop"
            ? "providerCard.section.stackDefaultHelpDesktop"
            : app === "agy"
              ? "providerCard.section.stackDefaultHelpAgy"
              : "providerCard.section.stackDefaultHelp",
        ),
      },
      emptyText: t("providerCard.section.stackNoDefault"),
      items: defaultItems,
    },
    {
      key: "members",
      title: t("providerCard.section.added", { count: memberItems.length }),
      emptyText: t("providerCard.section.stackEmpty"),
      items: memberItems,
    },
    {
      key: "available",
      title: t("providerCard.section.available", { count: availItems.length }),
      items: availItems,
    },
  ];
}

// ─── 共存式应用(OpenCode / OpenClaw / Hermes / Pi / MiniMax Code / Kimi)─────

export interface AdditiveInput {
  app: AppId;
  t: TFunction;
  providers: Provider[];
  isInConfig: (provider: Provider) => boolean;
  /** OpenCode:当前启用的 OMO / OMO Slim */
  currentOmoId?: string | null;
  currentOmoSlimId?: string | null;
  /** OpenClaw / OpenCode:默认模型属于哪家、它的模型名 */
  defaultModel?: { providerId: string; model: string } | null;
  /** OpenClaw / OpenCode:每家可选的模型 */
  defaultModelOptions?: (provider: Provider) => { id: string; name?: string }[];
  /** Hermes:model.provider 指向的那家 */
  hermesCurrentId?: string | null;
  isHermesManaged?: (provider: Provider) => boolean;
  /** Pi / Oh My Pi:读不到当前配置时一律不能改 */
  piStateUnavailable?: boolean;
  /** Kimi:原生 default_model 别名所属的供应商。 */
  kimiCurrentId?: string | null;
  /** WorkBuddy 官方登录卡:已收录账号、当前登录、是否在运行 */
  workbuddyOfficial?: WorkBuddyOfficialState;
  /** CodeBuddy:官方登录卡绑定账号的状态(授权中心收录的账号 + CodeBuddy 现在登着的号) */
  codebuddyAccounts?: CodebuddyAccountsState;
  /** Qoder:settings.json 的 `model.name` 所属的卡(`auto` 时是当前登录账号的官方卡)。 */
  qoderCurrentId?: string | null;
  /** Qoder 官方账号卡:收录的账号、Qoder 现在登着的号 */
  qoderAccounts?: QoderAccountsState;
  /** Copilot CLI:settings.json 的 `model`(`<卡片 id>/<模型>`)所属的自带模型卡。 */
  copilotDefaultId?: string | null;
  /** 「已添加」里列表外另画的卡(DSH 的 DeepSeek 账号卡),计入标题数字 */
  extraAdded?: number;
  actions: {
    add: (provider: Provider) => void;
    remove: (provider: Provider) => void;
    disableOmo: (provider: Provider) => void;
    setDefault: (provider: Provider, modelId?: string) => void;
    /** WorkBuddy 官方登录卡的「添加」:把 WorkBuddy 的登录切到卡片绑定的账号 */
    /** CodeBuddy 官方登录卡的「添加」:把 CodeBuddy 的登录换成卡片绑定的账号 */
    switchAccount?: (provider: Provider) => void;
    /** Copilot CLI 官方卡的主按钮 */
    openTerminal?: (provider: Provider) => void;
  };
}

export interface QoderAccountsState {
  status: "loading" | "error" | "ready";
  /** Qoder 现在登着的号 */
  currentAccountId: string | null;
  accountIds: string[];
  /** Qoder 里登着、还没收录的号:切号会覆盖它,先收录 */
  uncollectedLogin: string | null;
  /** 正在添加(切号)的卡 */
  switchingId?: string | null;
}

export interface CodebuddyAccountsState {
  status: "loading" | "error" | "ready";
  /** CodeBuddy 现在登着的号 */
  currentAccountId: string | null;
  accounts: { id: string; requiresReauth: boolean }[];
  /** 正在添加(切号)的卡 */
  switchingId?: string | null;
}

/**
 * CodeBuddy 官方登录卡:和模型卡一样分在「已添加 / 可添加」,但不进 models.json。
 * CodeBuddy 同时只能登录一个号,所以「已添加」最多一张:绑定的号就是 CodeBuddy 现在登着的号。
 * 「添加」把 CodeBuddy 的登录换成卡片绑定的账号(原来那张自动回到「可添加」);
 * 已添加那张的「移除」不可用(不替 CodeBuddy 登出),说明添加另一张即可切换。
 */
function codebuddyOfficialItem(
  t: TFunction,
  p: Provider,
  state: CodebuddyAccountsState | undefined,
  switchAccount: ((provider: Provider) => void) | undefined,
): { added: boolean; item: ProviderSection["items"][number] } {
  const chips: CardChip[] = [
    {
      key: "official",
      label: t("providerCard.chip.official"),
      tone: "outline",
    },
  ];
  const accountId = resolveManagedAccountId(p.meta, "codebuddy_oauth");
  const account = state?.accounts.find((item) => item.id === accountId);
  const inUse = Boolean(
    account &&
      state?.status === "ready" &&
      state.currentAccountId === account.id,
  );
  if (inUse) {
    return {
      added: true,
      item: {
        provider: p,
        presentation: {
          chips,
          buttons: [
            {
              key: "remove",
              label: t("providerCard.action.remove"),
              onClick: () => {},
              disabledReason: t("codebuddy.official.removeHint", {
                defaultValue:
                  "CodeBuddy 同时只能登录一个账号,添加另一张官方卡即可切换",
              }),
            },
          ],
        },
      },
    };
  }
  let disabledReason: string | undefined;
  if (!accountId) {
    disabledReason = t("codebuddy.official.unbound", {
      defaultValue: "还没有选择账号,请编辑卡片选择一个",
    });
  } else if (!state || state.status === "loading") {
    disabledReason = t("codebuddy.official.statusLoading", {
      defaultValue: "正在读取账号…",
    });
  } else if (state.status === "error") {
    disabledReason = t("authCenter.codebuddy.statusLoadFailed", {
      defaultValue: "无法读取 CodeBuddy 账号,请重试。",
    });
  } else if (!account) {
    disabledReason = t("codebuddy.official.accountDeleted", {
      defaultValue: "账号已删除,请重新选择",
    });
    chips.push({
      key: "accountDeleted",
      label: t("codebuddy.official.accountDeletedChip", {
        defaultValue: "账号已删除",
      }),
      tone: "warning",
    });
  } else if (account.requiresReauth) {
    disabledReason = t("codebuddy.official.expired", {
      defaultValue: "这个账号的登录已过期,请在授权中心重新登录后再添加",
    });
  } else if (state.switchingId) {
    disabledReason = t("codebuddy.official.adding", {
      defaultValue: "正在添加…",
    });
  }
  return {
    added: false,
    item: {
      provider: p,
      presentation: {
        chips,
        buttons: [
          {
            key: "add",
            label: t("providerCard.action.add"),
            onClick: () => switchAccount?.(p),
            disabledReason,
          },
        ],
      },
    },
  };
}

/** Qoder 每家可选的模型(settings.json 里这家的 `models`),给「设为默认」的子菜单。 */
function qoderModelOptions(p: Provider): { id: string; name?: string }[] {
  const models = (p.settingsConfig as { models?: unknown })?.models;
  if (!Array.isArray(models)) return [];
  return models.flatMap((model) => {
    const id = (model as { model?: unknown })?.model;
    const name = (model as { displayName?: unknown })?.displayName;
    return typeof id === "string" && id.trim()
      ? [{ id, name: typeof name === "string" ? name : undefined }]
      : [];
  });
}

function setDefaultMenuItem(
  t: TFunction,
  p: Provider,
  models: { id: string; name?: string }[],
  setDefault: (provider: Provider, modelId?: string) => void,
): CardMenuItem {
  return {
    key: "setDefault",
    label: t("providerCard.action.setDefault"),
    onSelect: () => setDefault(p, models[0]?.id),
    submenu:
      models.length > 1
        ? {
            title: t("openclaw.selectDefaultModel"),
            options: models.map((model) => ({
              key: model.id,
              label: model.name?.trim() || model.id,
              detail:
                model.name?.trim() && model.name.trim() !== model.id
                  ? model.id
                  : undefined,
              onSelect: () => setDefault(p, model.id),
            })),
          }
        : undefined,
  };
}

/**
 * Qoder 的卡:模型卡和 Kimi 一样「添加」写进当前版本的 settings.json、「移除」拿掉条目留着卡;
 * 官方账号卡和 CodeBuddy 一样每个收录的号一张,Qoder 登着的那个号在「已添加」,「添加」别的号就是切号。
 * 「设为默认」改 `model.name`(官方卡是 `auto`),收在「更多」菜单里;默认那家不能移除、不能删。
 */
function qoderItem(
  input: AdditiveInput,
  p: Provider,
): { added: boolean; item: ProviderSection["items"][number] } {
  const { t, actions } = input;
  const isDefault = p.id === input.qoderCurrentId;
  const status = isDefault
    ? { label: t("providerCard.status.default"), dot: "direct" as const }
    : undefined;
  const tone = isDefault ? ("direct" as const) : undefined;
  if (p.category === "official") {
    const chips: CardChip[] = [
      {
        key: "official",
        label: t("providerCard.chip.official"),
        tone: "outline",
      },
    ];
    const state = input.qoderAccounts;
    const accountId = resolveManagedAccountId(p.meta, "qoder_oauth");
    const ready = state?.status === "ready";
    const known = Boolean(accountId && state?.accountIds.includes(accountId));
    const deleteDisabledReason = t("qoder.accountManagedByQoder");
    if (ready && known && state.currentAccountId === accountId) {
      return {
        added: true,
        item: {
          provider: p,
          presentation: {
            tone,
            status,
            chips,
            buttons: [
              {
                key: "remove",
                label: t("providerCard.action.remove"),
                onClick: () => {},
                disabledReason: t("qoder.official.removeHint"),
              },
            ],
            menuItems: isDefault
              ? undefined
              : [
                  {
                    key: "setDefault",
                    label: t("providerCard.action.setDefault"),
                    onSelect: () => actions.setDefault(p),
                  },
                ],
            deleteDisabledReason,
          },
        },
      };
    }
    const disabledReason =
      !state || state.status === "loading"
        ? t("qoder.official.statusLoading")
        : state.status === "error"
          ? t("qoder.statusFailed")
          : !known
            ? t("qoder.official.accountMissing")
            : state.uncollectedLogin
              ? t("qoder.official.collectFirst", {
                  name: state.uncollectedLogin,
                })
              : state.switchingId
                ? t("qoder.official.adding")
                : undefined;
    return {
      added: false,
      item: {
        provider: p,
        presentation: {
          chips,
          buttons: [
            {
              key: "add",
              label: t("providerCard.action.add"),
              onClick: () => actions.switchAccount?.(p),
              disabledReason,
            },
          ],
          deleteDisabledReason,
        },
      },
    };
  }
  const live = input.isInConfig(p);
  const buttons: CardButton[] = [];
  const menuItems: CardMenuItem[] = [];
  if (!live) {
    buttons.push({
      key: "add",
      label: t("providerCard.action.add"),
      onClick: () => actions.add(p),
    });
  } else {
    if (!isDefault) {
      const models = qoderModelOptions(p);
      if (models.length > 0)
        menuItems.push(setDefaultMenuItem(t, p, models, actions.setDefault));
    }
    buttons.push({
      key: "remove",
      label: t("providerCard.action.remove"),
      disabledReason: isDefault
        ? t("providerCard.reason.defaultCannotRemove")
        : undefined,
      onClick: () => actions.remove(p),
    });
  }
  return {
    added: live,
    item: {
      provider: p,
      presentation: {
        tone: live ? tone : undefined,
        status: live ? status : undefined,
        chips: [],
        buttons,
        menuItems: menuItems.length ? menuItems : undefined,
        deleteDisabledReason:
          live && isDefault
            ? t("providerCard.reason.inUseCannotDelete")
            : undefined,
      },
    },
  };
}

/**
 * WorkBuddy 官方登录卡:和模型卡一样分进「已添加 / 可添加」,但同一时间只有一张在「已添加」——
 * 绑定的账号就是 WorkBuddy 现在登着的号。「添加」= 把 WorkBuddy 的登录切到这张卡的账号(要先退出
 * WorkBuddy),原来那张自动回到「可添加」。WorkBuddy 只能登一个号,在用的那张不能单独「移除」
 * (不登出、不改凭据),按钮禁用并说明添加另一张即可切换。官方卡从不写进 models.json。
 */
function workbuddyOfficialCard(
  input: AdditiveInput,
  p: Provider,
): { added: boolean; presentation: CardPresentation } {
  const { t } = input;
  const state = input.workbuddyOfficial;
  const bound = boundWorkBuddyAccountId(p);
  const missing =
    !bound || (state?.accountIds != null && !state.accountIds.has(bound));
  const current = !missing && bound === state?.currentAccountId;
  const chips: CardChip[] = [
    {
      key: "official",
      label: t("providerCard.chip.official"),
      tone: "outline",
    },
  ];
  if (missing) {
    chips.push({
      key: "accountMissing",
      label: bound
        ? t("workbuddy.official.accountDeletedChip")
        : t("workbuddy.official.accountUnsetChip"),
      tone: "warning",
    });
  }
  if (current) {
    return {
      added: true,
      presentation: {
        chips,
        buttons: [
          {
            key: "remove",
            label: t("providerCard.action.remove"),
            onClick: () => undefined,
            disabledReason: t("workbuddy.official.onlyOne"),
          },
        ],
      },
    };
  }
  return {
    added: false,
    presentation: {
      chips,
      buttons: [
        {
          key: "add",
          label: t("providerCard.action.add"),
          onClick: () => input.actions.switchAccount?.(p),
          disabledReason: missing
            ? bound
              ? t("workbuddy.official.accountDeleted")
              : t("workbuddy.official.accountUnset")
            : state?.running
              ? t("workbuddy.official.quitFirst")
              : state?.switching
                ? t("workbuddy.official.switching")
                : undefined,
        },
      ],
    },
  };
}

export function buildAdditiveSections(input: AdditiveInput): ProviderSection[] {
  const { app, t, providers, actions } = input;
  const added: ProviderSection["items"] = [];
  const available: ProviderSection["items"] = [];
  // Copilot CLI 的官方卡:仍是启动式(只有「打开终端」),并进「已添加」排在最前
  const official: ProviderSection["items"] = [];
  const piBlocked =
    appHas(app, "piFamily") && input.piStateUnavailable
      ? t(`${app}.current.stateUnavailableHint`)
      : undefined;

  for (const p of providers) {
    const chips: CardChip[] = [];
    if (app === "codebuddy" && p.category === "official") {
      const { added: isAdded, item } = codebuddyOfficialItem(
        t,
        p,
        input.codebuddyAccounts,
        actions.switchAccount,
      );
      (isAdded ? added : available).push(item);
      continue;
    }
    if (app === "qoder") {
      const { added: isAdded, item } = qoderItem(input, p);
      (isAdded ? added : available).push(item);
      continue;
    }
    if (app === "workbuddy" && isWorkBuddyOfficial(p)) {
      const card = workbuddyOfficialCard(input, p);
      (card.added ? added : available).push({
        provider: p,
        presentation: card.presentation,
      });
      continue;
    }
    if (app === "copilot") {
      if (isOfficialAccount(app, p)) {
        // 官方卡用 Copilot CLI 自己的 GitHub 登录,不写任何文件:没有添加 / 移除,
        // 主操作就是「打开终端」,哪张都能删
        official.push({
          provider: p,
          presentation: {
            chips: [
              {
                key: "official",
                label: t("providerCard.chip.official"),
                tone: "outline",
              },
            ],
            buttons: [
              {
                key: "openTerminal",
                label: t("provider.openTerminalWithProvider"),
                onClick: () => actions.openTerminal?.(p),
              },
            ],
          },
        });
        continue;
      }
      // 自带模型卡:「添加」写进 Copilot 的 providers.json,「移除」只删这一家的条目。
      // 默认那家(settings.json 的 model 指向它)不能移除、不能删;Copilot 里换默认模型用 /model
      const live = input.isInConfig(p);
      const isDefault = live && p.id === input.copilotDefaultId;
      (live ? added : available).push({
        provider: p,
        presentation: {
          tone: isDefault ? "direct" : undefined,
          status: isDefault
            ? { label: t("providerCard.status.default"), dot: "direct" }
            : undefined,
          chips,
          buttons: [
            live
              ? {
                  key: "remove",
                  label: t("providerCard.action.remove"),
                  disabledReason: isDefault
                    ? t("copilot.defaultCannotRemove")
                    : undefined,
                  onClick: () => actions.remove(p),
                }
              : {
                  key: "add",
                  label: t("providerCard.action.add"),
                  onClick: () => actions.add(p),
                },
          ],
          deleteDisabledReason: isDefault
            ? t("copilot.defaultCannotRemove")
            : undefined,
        },
      });
      continue;
    }
    if (app === "kimi") {
      const official = isOfficialAccount(app, p);
      const live = input.isInConfig(p);
      // 官方卡可以编辑模型、可以移出 config.toml(登录留着,能再添加);仍在 Kimi 里的
      // 官方卡不能直接删,删号在授权中心(连登录一起删)
      const deleteProtectedReason =
        official && live ? t("kimi.oauth.managedHint") : undefined;
      if (official) {
        chips.push({
          key: "official",
          label: t("providerCard.chip.official"),
          tone: "outline",
        });
      }
      const current = live && p.id === input.kimiCurrentId;
      // 「添加」只写进 config.toml(模型出现在 Kimi 的 /model 列表),「移除」从 config.toml
      // 拿掉、卡片留着;改 default_model 的「设为默认」和 OpenCode 一样收在「更多」菜单里
      const buttons: CardButton[] = [];
      const menuItems: CardMenuItem[] = [];
      if (!live) {
        buttons.push({
          key: "add",
          label: t("providerCard.action.add"),
          onClick: () => actions.add(p),
        });
      } else {
        if (!current) {
          menuItems.push({
            key: "setDefault",
            label: t("providerCard.action.setDefault"),
            onSelect: () => actions.setDefault(p),
          });
        }
        buttons.push({
          key: "remove",
          label: t("providerCard.action.remove"),
          disabledReason: current
            ? t("providerCard.reason.defaultCannotRemove")
            : undefined,
          onClick: () => actions.remove(p),
        });
      }
      (live ? added : available).push({
        provider: p,
        presentation: {
          tone: current ? "direct" : undefined,
          status: current
            ? { label: t("providerCard.status.default"), dot: "direct" }
            : undefined,
          chips,
          buttons,
          menuItems: menuItems.length ? menuItems : undefined,
          deleteDisabledReason:
            deleteProtectedReason ??
            (current ? t("providerCard.reason.inUseCannotDelete") : undefined),
        },
      });
      continue;
    }
    const isOmo = p.category === "omo";
    const isOmoSlim = p.category === "omo-slim";
    if (isOmo) chips.push({ key: "omo", label: "OMO", tone: "outline" });
    if (isOmoSlim) chips.push({ key: "slim", label: "Slim", tone: "outline" });

    // OMO / Slim:插件配置,同一时间只启用一个
    if (isOmo || isOmoSlim) {
      const enabled = isOmo
        ? p.id === input.currentOmoId
        : p.id === input.currentOmoSlimId;
      if (enabled) {
        // 分组标题已经写了「已添加」,按钮是「停用」,卡上不再重复写状态
        added.push({
          provider: p,
          presentation: {
            chips,
            buttons: [
              {
                key: "disable",
                label: t("providerCard.action.disable"),
                onClick: () => actions.disableOmo(p),
              },
            ],
          },
        });
      } else {
        available.push({
          provider: p,
          presentation: {
            chips,
            buttons: [
              {
                key: "enable",
                label: t("providerCard.action.enable"),
                onClick: () => actions.add(p),
              },
            ],
          },
        });
      }
      continue;
    }

    const managed = app === "hermes" && (input.isHermesManaged?.(p) ?? false);
    if (managed) {
      chips.push({
        key: "managed",
        label: t("providerCard.chip.hermesManaged"),
        tone: "outline",
        title: t("provider.managedByHermesHint"),
      });
    }
    const readOnlyReason = managed
      ? t("provider.managedByHermesHint")
      : undefined;

    if (!input.isInConfig(p)) {
      available.push({
        provider: p,
        presentation: {
          chips,
          dim: managed,
          editDisabledReason: readOnlyReason,
          deleteDisabledReason: readOnlyReason ?? piBlocked,
          buttons: [
            {
              key: "add",
              label: appHas(app, "piFamily")
                ? t("providerCard.action.enable")
                : t("providerCard.action.add"),
              onClick: () => actions.add(p),
              disabledReason: piBlocked,
            },
          ],
        },
      });
      continue;
    }

    // 已添加:白底、不写「● 已添加」(分组标题和「移除」已经说清楚);蓝底只给默认那一家
    const buttons: CardButton[] = [];
    const menuItems: CardMenuItem[] = [];
    let status: CardPresentation["status"];
    let isDefault = false;
    let removeReason: string | undefined = piBlocked;

    // OpenCode 的顶层 model 一直允许移走所指的那家,这里不改;OpenClaw 不能
    if (app === "openclaw" || app === "opencode") {
      isDefault = input.defaultModel?.providerId === p.id;
      if (isDefault) {
        status = { label: t("providerCard.status.default"), dot: "direct" };
        chips.push({
          key: "defaultModel",
          label: t("providerCard.chip.defaultModel", {
            model: input.defaultModel?.model ?? "",
          }),
          tone: "outline",
        });
        if (app === "openclaw") {
          removeReason = t("providerCard.reason.defaultCannotRemove");
        }
      } else {
        const models = input.defaultModelOptions?.(p) ?? [];
        // 「设为默认」放「更多」菜单,不占卡片上的主操作位;多个模型时展开子菜单选。
        // OpenCode 内置供应商可以不列模型(运行时才有),给不出选项就不显示
        if (app === "openclaw" || models.length > 0) {
          menuItems.push(setDefaultMenuItem(t, p, models, actions.setDefault));
        }
      }
    }

    // Hermes 跟 OpenClaw / OpenCode 一样是累加式:model.provider 只是默认那家,
    // 「设为默认」收在「更多」菜单里,主操作位只留「移除」
    if (app === "hermes") {
      if (p.id === input.hermesCurrentId) {
        isDefault = true;
        status = { label: t("providerCard.status.default"), dot: "direct" };
        removeReason = t("providerCard.reason.defaultCannotRemove");
      } else {
        menuItems.push({
          key: "setDefault",
          label: t("providerCard.action.setDefault"),
          onSelect: () => actions.setDefault(p),
          disabledReason: readOnlyReason,
        });
      }
    }

    buttons.push({
      key: "remove",
      label: t("providerCard.action.remove"),
      onClick: () => actions.remove(p),
      disabledReason: removeReason ?? readOnlyReason,
    });

    added.push({
      provider: p,
      presentation: {
        tone: isDefault ? "direct" : undefined,
        status,
        chips,
        dim: managed,
        editDisabledReason: readOnlyReason,
        deleteDisabledReason: readOnlyReason ?? piBlocked,
        buttons,
        menuItems: menuItems.length ? menuItems : undefined,
      },
    });
  }

  return [
    {
      key: "added",
      title: t("providerCard.section.added", {
        count: official.length + added.length + (input.extraAdded ?? 0),
      }),
      help: {
        title: t("providerCard.section.addedHelpTitle"),
        // DSH 的供应商一起写进 Web 和 Desktop 两份配置,在这里说明
        body:
          app === "dsh"
            ? t("dsh.sharedProviders")
            : t("providerCard.section.addedHelp", {
                app: t(`apps.${app}`),
              }),
      },
      note: app === "dsh" ? t("dsh.sharedProvidersNote") : undefined,
      emptyText: t("providerCard.section.addedEmpty"),
      items: [...official, ...added],
    },
    {
      key: "available",
      title: t("providerCard.section.available", { count: available.length }),
      items: available,
    },
  ];
}