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

38.4 KB
Raw Blame
import { useCallback, useEffect, useRef, useState } from "react";
import { z } from "zod";
import { useForm } from "react-hook-form";
import { useQuery } from "@tanstack/react-query";
import { providersApi, type WorkBuddyModelOwner } from "@/lib/api/providers";
import { useTranslation } from "react-i18next";
import { ChevronRight, Plus, Trash2 } from "lucide-react";
import { toast } from "@/lib/toast";
import { Button } from "@/components/ui/button";
import { Form, FormLabel } from "@/components/ui/form";
import { HelpTip } from "@/components/ui/help-tip";
import { HoverTip } from "@/components/ui/hover-tip";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import JsonEditor from "@/components/JsonEditor";
import { useDarkMode } from "@/hooks/useDarkMode";
import { useCommittableRef } from "@/hooks/useLatestRef";
import { useModelMetadataFill } from "@/hooks/useModelMetadataFill";
import {
  fetchModelsForConfig,
  showFetchModelsError,
  type FetchedModel,
} from "@/lib/api/model-fetch";
import { cn } from "@/lib/utils";
import { useProvidersQuery } from "@/lib/query/queries";
import type { ProviderFormData } from "@/lib/schemas/provider";
import { piPresetModelSources } from "@/config/presetModelMetadata";
import {
  WORKBUDDY_ACCOUNT_AUTH_PROVIDER,
  WORKBUDDY_DEFAULT_CONFIG,
  WORKBUDDY_EFFORTS,
  setWorkBuddyCanDisableThinking,
  setWorkBuddyEffort,
  workbuddyOfficialPreset,
  workbuddyProviderPresets,
  type WorkBuddyModel,
  type WorkBuddyProviderPreset,
  type WorkBuddySettingsConfig,
} from "@/config/workbuddyProviderPresets";
import { resolveManagedAccountId } from "@/lib/authBinding";
import type { ManagedAuthAccount } from "@/lib/api";
import { authApi } from "@/lib/api";
import { WORKBUDDY_STATUS_KEY } from "@/components/providers/workbuddyOfficial";
import { WorkBuddyOfficialSection } from "./WorkBuddyOfficialSection";
import { BasicFormFields, REQUIRED_LABEL } from "./BasicFormFields";
import {
  FetchedModelPicker,
  type FetchedModelSelection,
} from "./FetchedModelPicker";
import { ProviderPresetSelector } from "./ProviderPresetSelector";
import {
  fillWorkBuddyModel,
  metadataFilledAnything,
} from "./modelMetadataFill";
import { ApiKeySection, EndpointField, ModelDropdown } from "./shared";
import type { ProviderFormProps } from "./ProviderForm";

const OFFICIAL_PRESET_ID = "official";
const MODEL_OWNERS_KEY = ["workbuddy", "modelOwners"] as const;

const PRESET_ENTRIES = [
  { id: OFFICIAL_PRESET_ID, preset: workbuddyOfficialPreset },
  ...workbuddyProviderPresets.map((preset, index) => ({
    id: String(index),
    preset,
  })),
];

const presetById = (id: string): WorkBuddyProviderPreset | undefined =>
  id === "custom"
    ? undefined
    : id === OFFICIAL_PRESET_ID
      ? workbuddyOfficialPreset
      : workbuddyProviderPresets[Number(id)];

/** 官方登录卡的 key 自动生成,不让用户填(它不写进 models.json)。 */
const officialKey = (taken: Record<string, unknown>) => {
  const base = workbuddyOfficialPreset.providerKey;
  if (!Object.prototype.hasOwnProperty.call(taken, base)) return base;
  for (let n = 2; ; n += 1) {
    const key = `${base}-${n}`;
    if (!Object.prototype.hasOwnProperty.call(taken, key)) return key;
  }
};

// WorkBuddy 的自定义模型这里只配 OpenAI Chat Completions。旧条目里的 `api` 字段
// 不在表单里展示,靠 passthrough 原样带回保存。
const CHAT_COMPLETIONS = "openai-completions";

const configSchema = z
  .object({
    vendor: z.string().optional(),
    url: z.string(),
    apiKey: z.string(),
    models: z.array(
      z
        .object({
          id: z.string(),
          name: z.string().optional(),
          maxInputTokens: z.number().optional(),
          maxOutputTokens: z.number().optional(),
          supportsToolCall: z.boolean().optional(),
          supportsImages: z.boolean().optional(),
          supportsReasoning: z.boolean().optional(),
          // 表单按它画强度按钮,JSON 里写坏了就不进表单(编辑中途的半截结构会让表单崩)。
          reasoning: z
            .object({
              supportedEfforts: z.array(z.enum(WORKBUDDY_EFFORTS)),
              defaultEffort: z.enum(WORKBUDDY_EFFORTS),
              canDisableThinking: z.boolean(),
            })
            .passthrough()
            .optional(),
        })
        .passthrough(),
    ),
  })
  .passthrough();

const KEY_PATTERN = /^[a-z0-9]+(-[a-z0-9]+)*$/;
const normalizeKey = (value: string) =>
  value.toLowerCase().replace(/[^a-z0-9-]/g, "");

// 能力项和 Kimi 的同名开关共用文案。
const CAPABILITIES = [
  ["supportsToolCall", "kimi.capabilities.tool_use"],
  ["supportsImages", "kimi.capabilities.image_in"],
  ["supportsReasoning", "kimi.capabilities.thinking"],
] as const;

const parseTokens = (value: string): number | undefined => {
  const parsed = Number.parseInt(value, 10);
  return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
};

/** Chat Completions 填的是完整地址;拉模型列表和查预设参数都按基础地址来。 */
const baseUrlOf = (config: WorkBuddySettingsConfig) =>
  (config.url ?? "").trim().replace(/\/chat\/completions\/*$/, "");

export function WorkBuddyProviderForm({
  providerId,
  initialData,
  onSubmit,
  onCancel,
  submitLabel,
  showButtons = true,
  onSubmittingChange,
  onSubmitReadyChange,
}: ProviderFormProps) {
  const { t } = useTranslation();
  const isDarkMode = useDarkMode();
  // 官方登录卡存的是空对象,表单里的模型字段按默认值补齐(保存时不用它)
  const [config, setConfig] = useState<WorkBuddySettingsConfig>(() =>
    initialData?.category === "official"
      ? WORKBUDDY_DEFAULT_CONFIG
      : ((initialData?.settingsConfig as WorkBuddySettingsConfig | undefined) ??
        WORKBUDDY_DEFAULT_CONFIG),
  );
  const [jsonText, setJsonText] = useState(JSON.stringify(config, null, 2));
  const [jsonValid, setJsonValid] = useState(true);
  const [presetId, setPresetId] = useState("custom");
  const preset = presetById(presetId);
  const category = initialData?.category ?? preset?.category ?? "custom";
  // 官方登录卡:只绑定授权中心的一个 WorkBuddy 账号,没有接口地址、Key、模型
  const isOfficial = category === "official";
  const [accountId, setAccountId] = useState<string | null>(
    () =>
      resolveManagedAccountId(
        initialData?.meta,
        WORKBUDDY_ACCOUNT_AUTH_PROVIDER,
      )?.trim() || null,
  );
  // 名称默认跟着选中账号的昵称;用户改过名称就不再覆盖
  const autoName = useRef<string | null>(null);
  const [busy, setBusy] = useState(false);
  const [error, setError] = useState("");
  // The key is stored on every models.json entry this provider owns, so it is
  // fixed once the provider exists.
  const isEdit = Boolean(initialData);
  const [providerKey, setProviderKey] = useState(providerId ?? "");
  const { data: existing } = useProvidersQuery("workbuddy");
  const keyTaken =
    !isEdit &&
    Object.prototype.hasOwnProperty.call(
      existing?.providers ?? {},
      providerKey,
    );
  const keyInvalid =
    !isEdit && providerKey !== "" && !KEY_PATTERN.test(providerKey);

  const form = useForm<ProviderFormData>({
    defaultValues: {
      name: initialData?.name ?? "",
      notes: initialData?.notes ?? "",
      websiteUrl: initialData?.websiteUrl ?? "",
      icon: initialData?.icon ?? "",
      iconColor: initialData?.iconColor ?? "",
      settingsConfig: jsonText,
    },
  });
  const name = form.watch("name");
  const models = config.models ?? [];
  const modelIds = models.map((model) => model.id.trim());
  // WorkBuddy 里模型 id 既是唯一键又是发给上游的模型名,一个 id 只能属于一张卡:
  // 别的卡(或用户自己在 WorkBuddy 里加的)已经占用的 id,这里跳过或标出来。
  const { data: modelOwners } = useQuery({
    queryKey: MODEL_OWNERS_KEY,
    queryFn: providersApi.getWorkBuddyModelOwners,
    enabled: !isOfficial,
    staleTime: 0,
  });
  const ownerLabel = (owner: string | null) =>
    owner === null
      ? t("workbuddy.form.modelOwnerUser")
      : (existing?.providers?.[owner]?.name ?? owner);
  // 编辑时自己的条目不算占用;用户自己加的(owner 为 null)总算占用。
  const takenBy = (id: string): WorkBuddyModelOwner | undefined =>
    modelOwners?.find((entry) => entry.id === id && entry.owner !== providerId);
  const [skippedModels, setSkippedModels] = useState<string[]>([]);
  const modelsValid =
    models.length > 0 &&
    modelIds.every((id) => id && !takenBy(id)) &&
    new Set(modelIds).size === modelIds.length;
  const urlValid = /^https?:\/\/\S+$/.test(config.url?.trim() ?? "");
  // 和账号区共用缓存:绑定的号已被删掉时不能保存,要重新选
  const { data: workbuddyAccounts } = useQuery({
    queryKey: WORKBUDDY_STATUS_KEY,
    queryFn: () => authApi.authGetStatus(WORKBUDDY_ACCOUNT_AUTH_PROVIDER),
    enabled: isOfficial,
    staleTime: 30_000,
  });
  const accountAvailable =
    accountId !== null &&
    (workbuddyAccounts?.accounts.some((account) => account.id === accountId) ??
      false);
  const ready = isOfficial
    ? Boolean(name.trim() && accountAvailable)
    : Boolean(
        jsonValid &&
          name.trim() &&
          (isEdit || (providerKey && !keyInvalid && !keyTaken)) &&
          urlValid &&
          modelsValid,
      );
  const chooseAccount = (account: ManagedAuthAccount) => {
    setAccountId(account.id);
    const current = form.getValues("name").trim();
    if (!current || current === autoName.current) {
      form.setValue("name", account.login, { shouldDirty: true });
      autoName.current = account.login;
    }
  };
  useEffect(() => {
    onSubmitReadyChange?.(ready);
  }, [ready, onSubmitReadyChange]);

  const update = (next: WorkBuddySettingsConfig) => {
    setConfig(next);
    setJsonText(JSON.stringify(next, null, 2));
    setJsonValid(true);
  };
  // 补全参数可能晚到,异步回调经 ref 读最新配置再提交。
  const [configRef, commitConfig] = useCommittableRef(config, update);
  const commitModels = (next: WorkBuddyModel[]) =>
    commitConfig({ ...configRef.current, models: next });
  const replaceModel = (
    index: number,
    update: (model: WorkBuddyModel) => WorkBuddyModel,
  ) =>
    commitModels(
      configRef.current.models.map((model, i) =>
        i === index ? update(model) : model,
      ),
    );
  const updateModel = (index: number, patch: Partial<WorkBuddyModel>) =>
    replaceModel(index, (model) => ({ ...model, ...patch }));
  // 模型是数组,展开状态按下标记;删除一行时把后面的下标往前挪。
  const [expandedModels, setExpandedModels] = useState<Set<number>>(
    () => new Set(),
  );
  const toggleModelExpand = (index: number) =>
    setExpandedModels((current) => {
      const next = new Set(current);
      if (!next.delete(index)) next.add(index);
      return next;
    });
  const removeModel = (index: number) => {
    commitModels(configRef.current.models.filter((_, i) => i !== index));
    setExpandedModels(
      (current) =>
        new Set(
          [...current]
            .filter((i) => i !== index)
            .map((i) => (i > index ? i - 1 : i)),
        ),
    );
  };

  // ── 从接口拉模型列表 ──
  const baseUrl = baseUrlOf(config);
  const apiKey = config.apiKey?.trim() ?? "";
  const [fetchedModels, setFetchedModels] = useState<FetchedModel[]>([]);
  const [isFetchingModels, setIsFetchingModels] = useState(false);
  const modelFetchGeneration = useRef(0);
  useEffect(() => {
    setFetchedModels((prev) => (prev.length === 0 ? prev : []));
    setIsFetchingModels(false);
    return () => {
      // Ignore responses for a previous endpoint/key or an unmounted form.
      modelFetchGeneration.current += 1;
    };
  }, [baseUrl, apiKey]);

  const handleFetchModels = useCallback(() => {
    if (!baseUrl || !apiKey) {
      showFetchModelsError(null, t, {
        hasApiKey: Boolean(apiKey),
        hasBaseUrl: Boolean(baseUrl),
      });
      return;
    }
    const generation = ++modelFetchGeneration.current;
    setFetchedModels([]);
    setIsFetchingModels(true);
    // WorkBuddy 能用第三方生图模型(#10129),不滤图片 / 视频模型。
    fetchModelsForConfig(baseUrl, apiKey, undefined, undefined, undefined, {
      apiFormat: CHAT_COMPLETIONS,
      includeNonChatModels: true,
    })
      .then((result) => {
        if (generation !== modelFetchGeneration.current) return;
        const unique = [
          ...new Map(result.map((model) => [model.id, model])).values(),
        ];
        setFetchedModels(unique);
        // 拉到模型时直接打开选择弹窗,不再提示成功。
        if (unique.length === 0) {
          toast.info(t("providerForm.fetchModelsEmpty"));
        }
      })
      .catch((err) => {
        if (generation !== modelFetchGeneration.current) return;
        console.warn("[ModelFetch] Failed:", err);
        showFetchModelsError(err, t);
      })
      .finally(() => {
        if (generation === modelFetchGeneration.current) {
          setIsFetchingModels(false);
        }
      });
  }, [apiKey, baseUrl, t]);

  const fillModelMetadata = useModelMetadataFill({
    baseUrl,
    presets: piPresetModelSources,
    prefetch: fetchedModels.length > 0,
  });
  const fillModelMetadataFor = (id: string, silent?: boolean) =>
    fillModelMetadata(
      id,
      (metadata) => {
        const current = configRef.current.models;
        const index = current.findIndex((model) => model.id === id);
        if (index < 0) return false;
        const filled = fillWorkBuddyModel(current[index], metadata, baseUrl);
        if (!metadataFilledAnything(current[index], filled)) return false;
        commitModels(current.map((model, i) => (i === index ? filled : model)));
        return true;
      },
      { silent },
    );

  const applyFetchedModels = ({ add, remove }: FetchedModelSelection) => {
    const kept = configRef.current.models.filter(
      (model) => !remove.includes(model.id.trim()),
    );
    const keptIds = new Set(kept.map((model) => model.id.trim()));
    const additions = add
      .filter((id) => !keptIds.has(id))
      .map((id) => ({ id, supportsToolCall: true }));
    // 删了行,下标对不上了,收起全部。
    if (kept.length !== configRef.current.models.length)
      setExpandedModels(new Set());
    commitModels([...kept, ...additions]);
    // 弹窗里已经显示过这些参数,补完不再提示。
    for (const { id } of additions) fillModelMetadataFor(id, true);
  };

  const choosePreset = (id: string) => {
    setPresetId(id);
    const selected = presetById(id);
    const official = selected?.category === "official";
    const presetConfig = selected?.settingsConfig ?? WORKBUDDY_DEFAULT_CONFIG;
    const skipped = presetConfig.models.filter((model) => takenBy(model.id));
    setSkippedModels(
      skipped.map(
        (model) => `${model.id} (${ownerLabel(takenBy(model.id)!.owner)})`,
      ),
    );
    const next = skipped.length
      ? {
          ...presetConfig,
          models: presetConfig.models.filter(
            (model) => !skipped.includes(model),
          ),
        }
      : presetConfig;
    update(next);
    setProviderKey(official ? "" : (selected?.providerKey ?? ""));
    setAccountId(null);
    autoName.current = null;
    form.reset({
      // 官方登录卡的名称在选账号时填成昵称
      name: official ? "" : (selected?.name ?? ""),
      notes: "",
      websiteUrl: selected?.websiteUrl ?? "",
      icon: selected?.icon ?? "",
      iconColor: selected?.iconColor ?? "",
      settingsConfig: JSON.stringify(next),
    });
  };

  return (
    <Form {...form}>
      <form
        id="provider-form"
        className="space-y-6"
        onSubmit={form.handleSubmit(async (identity) => {
          if (!ready || busy) return;
          setBusy(true);
          onSubmittingChange?.(true);
          setError("");
          try {
            if (isOfficial) {
              await onSubmit({
                ...identity,
                name: identity.name.trim(),
                meta: {
                  ...initialData?.meta,
                  authBinding: {
                    source: "managed_account",
                    authProvider: WORKBUDDY_ACCOUNT_AUTH_PROVIDER,
                    accountId: accountId ?? undefined,
                  },
                },
                providerKey: isEdit
                  ? providerId
                  : officialKey(existing?.providers ?? {}),
                presetCategory: "official",
                settingsConfig: "{}",
              });
              return;
            }
            const cleaned: WorkBuddySettingsConfig = {
              ...config,
              url: config.url.trim(),
              apiKey: config.apiKey.trim(),
              models: models.map((model) => ({
                ...model,
                id: model.id.trim(),
              })),
            };
            if (!cleaned.vendor?.trim()) delete cleaned.vendor;
            await onSubmit({
              ...identity,
              name: identity.name.trim(),
              meta: initialData?.meta,
              providerKey: isEdit ? providerId : providerKey,
              presetCategory: category,
              settingsConfig: JSON.stringify(cleaned),
            });
          } catch (error) {
            setError(String(error));
          } finally {
            setBusy(false);
            onSubmittingChange?.(false);
          }
        })}
      >
        {!initialData && (
          <ProviderPresetSelector
            selectedPresetId={presetId}
            presetEntries={PRESET_ENTRIES}
            presetCategoryLabels={{
              custom: t("providerPreset.custom"),
              cn_official: t("providerForm.categoryCnOfficial"),
              aggregator: t("providerForm.categoryAggregation"),
              third_party: t("providerForm.categoryThirdParty"),
            }}
            onPresetChange={choosePreset}
            category={category}
          />
        )}
        {error && (
          <p role="alert" className="text-sm text-destructive">
            {error}
          </p>
        )}
        <fieldset
          disabled={busy || !jsonValid}
          className="min-w-0 space-y-6 border-0 p-0 disabled:opacity-50"
        >
          <BasicFormFields
            form={form}
            beforeNameSlot={
              isOfficial ? undefined : (
                <div className="space-y-2">
                  <Label
                    htmlFor="workbuddy-provider-key"
                    className={REQUIRED_LABEL}
                  >
                    {t("opencode.providerKey")}
                  </Label>
                  <Input
                    id="workbuddy-provider-key"
                    value={providerKey}
                    onChange={(event) =>
                      setProviderKey(normalizeKey(event.target.value))
                    }
                    disabled={isEdit}
                    placeholder={t("opencode.providerKeyPlaceholder")}
                    autoComplete="off"
                    aria-required
                    aria-invalid={keyTaken || keyInvalid || undefined}
                    className={
                      keyTaken || keyInvalid ? "border-destructive" : ""
                    }
                  />
                  <p
                    className={
                      keyTaken || keyInvalid
                        ? "text-xs text-destructive"
                        : "text-xs text-fg-2"
                    }
                  >
                    {keyTaken
                      ? t("opencode.providerKeyDuplicate")
                      : keyInvalid
                        ? t("opencode.providerKeyInvalid")
                        : isEdit
                          ? t("workbuddy.form.providerKeyLockedHint")
                          : t("workbuddy.form.providerKeyHint")}
                  </p>
                </div>
              )
            }
          />

          {isOfficial ? (
            <WorkBuddyOfficialSection
              value={accountId}
              onChange={chooseAccount}
            />
          ) : (
            <>
              <ApiKeySection
                id="workbuddy-api-key"
                value={config.apiKey}
                onChange={(apiKey) => update({ ...config, apiKey })}
                category={category}
                shouldShowLink={Boolean(preset?.apiKeyUrl)}
                websiteUrl={preset?.apiKeyUrl ?? ""}
                isPartner={preset?.isPartner}
                partnerPromotionKey={preset?.partnerPromotionKey}
                hint={t("workbuddy.form.apiKeyHint")}
              />

              <EndpointField
                id="workbuddy-url"
                label={t("workbuddy.form.url")}
                value={config.url}
                onChange={(url) => update({ ...config, url })}
                placeholder="https://api.example.com/v1/chat/completions"
                hint={t("workbuddy.form.urlHint")}
                showManageButton={false}
              />

              <div className="space-y-2">
                <Label htmlFor="workbuddy-vendor">
                  {t("workbuddy.form.vendor")}
                </Label>
                <Input
                  id="workbuddy-vendor"
                  value={config.vendor ?? ""}
                  onChange={(event) =>
                    update({ ...config, vendor: event.target.value })
                  }
                  placeholder="Custom"
                  autoComplete="off"
                />
              </div>

              <div className="space-y-3 border-l border-border pl-3">
                <div className="flex items-center justify-between">
                  <div className="flex items-center gap-1">
                    <FormLabel>{t("opencode.models")}</FormLabel>
                    <HelpTip title={t("opencode.models")}>
                      {t("workbuddy.form.modelsHint")}
                    </HelpTip>
                  </div>
                  <div className="flex gap-1">
                    <FetchedModelPicker
                      models={fetchedModels}
                      configuredModelIds={modelIds}
                      onApply={applyFetchedModels}
                      onFetch={handleFetchModels}
                      isFetching={isFetchingModels}
                      metadataSource={{
                        baseUrl,
                        presets: piPresetModelSources,
                      }}
                    />
                    <Button
                      type="button"
                      variant="outline"
                      size="sm"
                      className="h-7 gap-1"
                      onClick={() =>
                        commitModels([
                          ...configRef.current.models,
                          {
                            id: "",
                            supportsToolCall: true,
                            supportsImages: false,
                          },
                        ])
                      }
                    >
                      <Plus className="h-3.5 w-3.5" />
                      {t("opencode.addModel")}
                    </Button>
                  </div>
                </div>
                {skippedModels.length > 0 && (
                  <p role="status" className="text-xs text-fg-2">
                    {t("workbuddy.form.presetModelsSkipped", {
                      models: skippedModels.join(", "),
                    })}
                  </p>
                )}
                {models.length === 0 ? (
                  <p role="status" className="py-2 text-sm text-fg-2">
                    {t("opencode.noModels")}
                  </p>
                ) : (
                  <div className="space-y-2">
                    <div className="mb-1 flex items-center gap-2 px-1 text-xs text-fg-2">
                      <span className="w-9" />
                      <span className={cn("flex-1", REQUIRED_LABEL)}>
                        {t("opencode.modelId")}
                      </span>
                      <span className="flex-1">{t("opencode.modelName")}</span>
                      <span className="w-9" />
                    </div>
                    {models.map((model, index) => {
                      const taken = takenBy(model.id.trim());
                      return (
                        <div key={index} className="space-y-2">
                          <div className="flex items-center gap-2">
                            <Button
                              type="button"
                              variant="ghost"
                              size="icon"
                              onClick={() => toggleModelExpand(index)}
                              aria-label={t("opencode.toggleModelDetails")}
                              aria-expanded={expandedModels.has(index)}
                              className="h-9 w-9 shrink-0"
                            >
                              <ChevronRight
                                className={cn(
                                  "h-4 w-4 transition-transform motion-reduce:transition-none",
                                  expandedModels.has(index) && "rotate-90",
                                )}
                              />
                            </Button>
                            <div className="flex min-w-0 flex-1 gap-1">
                              <Input
                                id={`workbuddy-model-id-${index}`}
                                value={model.id}
                                onChange={(event) =>
                                  updateModel(index, { id: event.target.value })
                                }
                                placeholder="model-id"
                                aria-label={t("opencode.modelId")}
                                aria-required
                                aria-invalid={taken ? true : undefined}
                                aria-describedby={
                                  taken
                                    ? `workbuddy-model-taken-${index}`
                                    : undefined
                                }
                                autoComplete="off"
                                className={cn(
                                  "min-w-0 flex-1",
                                  taken && "border-destructive",
                                )}
                              />
                              {fetchedModels.length > 0 && (
                                <ModelDropdown
                                  models={fetchedModels}
                                  onSelect={(id) => {
                                    updateModel(index, { id });
                                    fillModelMetadataFor(id);
                                  }}
                                />
                              )}
                            </div>
                            <Input
                              id={`workbuddy-model-name-${index}`}
                              value={model.name ?? ""}
                              onChange={(event) =>
                                updateModel(index, {
                                  name: event.target.value || undefined,
                                })
                              }
                              // 留空时写入用厂商标签,WorkBuddy 菜单显示「厂商:模型 ID」
                              placeholder={
                                config.vendor?.trim() || t("opencode.modelName")
                              }
                              aria-label={t("opencode.modelName")}
                              autoComplete="off"
                              className="min-w-0 flex-1"
                            />
                            <HoverTip content={t("common.delete")}>
                              <Button
                                type="button"
                                variant="ghost"
                                size="icon"
                                aria-label={t("common.delete")}
                                className="h-9 w-9 shrink-0 text-fg-2 hover:text-destructive"
                                onClick={() => removeModel(index)}
                              >
                                <Trash2 className="h-4 w-4" />
                              </Button>
                            </HoverTip>
                          </div>
                          {taken && (
                            <p
                              id={`workbuddy-model-taken-${index}`}
                              className="ml-11 text-xs text-destructive"
                            >
                              {t("workbuddy.form.modelIdTaken", {
                                owner: ownerLabel(taken.owner),
                              })}
                            </p>
                          )}
                          {expandedModels.has(index) && (
                            <div className="ml-9 grid gap-3 border-l-2 border-muted pl-4 sm:grid-cols-2">
                              <div className="flex min-h-9 flex-wrap items-center gap-x-8 gap-y-2 sm:col-span-2">
                                {CAPABILITIES.map(([capability, label]) => (
                                  <div
                                    key={capability}
                                    className="flex items-center gap-2.5"
                                  >
                                    <Label
                                      htmlFor={`workbuddy-model-${capability}-${index}`}
                                      className="cursor-pointer"
                                    >
                                      {t(label)}
                                    </Label>
                                    <Switch
                                      id={`workbuddy-model-${capability}-${index}`}
                                      checked={model[capability] === true}
                                      onCheckedChange={(checked) =>
                                        updateModel(index, {
                                          [capability]: checked,
                                        })
                                      }
                                    />
                                  </div>
                                ))}
                              </div>
                              {model.supportsReasoning === true && (
                                <div className="space-y-2 sm:col-span-2">
                                  <div className="flex items-center gap-1">
                                    <Label
                                      id={`workbuddy-model-efforts-${index}`}
                                    >
                                      {t("workbuddy.form.thinkingEfforts")}
                                    </Label>
                                    <HelpTip
                                      title={t(
                                        "workbuddy.form.thinkingEfforts",
                                      )}
                                    >
                                      {t("workbuddy.form.thinkingEffortsHint")}
                                    </HelpTip>
                                  </div>
                                  <div
                                    role="group"
                                    aria-labelledby={`workbuddy-model-efforts-${index}`}
                                    className="flex flex-wrap items-center gap-1.5"
                                  >
                                    {WORKBUDDY_EFFORTS.map((effort) => {
                                      const selected =
                                        Array.isArray(
                                          model.reasoning?.supportedEfforts,
                                        ) &&
                                        model.reasoning.supportedEfforts.includes(
                                          effort,
                                        );
                                      return (
                                        <Button
                                          key={effort}
                                          type="button"
                                          size="sm"
                                          variant={
                                            selected ? "secondary" : "outline"
                                          }
                                          aria-pressed={selected}
                                          className="h-7"
                                          onClick={() =>
                                            replaceModel(index, (current) =>
                                              setWorkBuddyEffort(
                                                current,
                                                effort,
                                                !selected,
                                              ),
                                            )
                                          }
                                        >
                                          {t(
                                            `pi.form.thinkingLevels.${effort}`,
                                          )}
                                        </Button>
                                      );
                                    })}
                                  </div>
                                  {model.reasoning && (
                                    <div className="flex items-center gap-2.5">
                                      <Label
                                        htmlFor={`workbuddy-model-can-disable-${index}`}
                                        className="cursor-pointer"
                                      >
                                        {t("workbuddy.form.canDisableThinking")}
                                      </Label>
                                      <Switch
                                        id={`workbuddy-model-can-disable-${index}`}
                                        checked={
                                          model.reasoning.canDisableThinking ===
                                          true
                                        }
                                        onCheckedChange={(checked) =>
                                          replaceModel(index, (current) =>
                                            setWorkBuddyCanDisableThinking(
                                              current,
                                              checked,
                                            ),
                                          )
                                        }
                                      />
                                    </div>
                                  )}
                                </div>
                              )}
                              <div className="space-y-2">
                                <Label
                                  htmlFor={`workbuddy-model-input-${index}`}
                                >
                                  {t("workbuddy.form.maxInputTokens")}
                                </Label>
                                <Input
                                  id={`workbuddy-model-input-${index}`}
                                  type="number"
                                  min={1}
                                  step={1}
                                  value={model.maxInputTokens ?? ""}
                                  onChange={(event) =>
                                    updateModel(index, {
                                      maxInputTokens: parseTokens(
                                        event.target.value,
                                      ),
                                    })
                                  }
                                  placeholder="128000"
                                />
                              </div>
                              <div className="space-y-2">
                                <Label
                                  htmlFor={`workbuddy-model-output-${index}`}
                                >
                                  {t("workbuddy.form.maxOutputTokens")}
                                </Label>
                                <Input
                                  id={`workbuddy-model-output-${index}`}
                                  type="number"
                                  min={1}
                                  step={1}
                                  value={model.maxOutputTokens ?? ""}
                                  onChange={(event) =>
                                    updateModel(index, {
                                      maxOutputTokens: parseTokens(
                                        event.target.value,
                                      ),
                                    })
                                  }
                                  placeholder="16384"
                                />
                              </div>
                            </div>
                          )}
                        </div>
                      );
                    })}
                  </div>
                )}
              </div>
            </>
          )}
        </fieldset>

        {!isOfficial && (
          <div className="space-y-2">
            <Label htmlFor="workbuddy-settings-config">
              {t("provider.configJson")}
            </Label>
            <JsonEditor
              id="workbuddy-settings-config"
              ariaLabel={t("provider.configJson")}
              value={jsonText}
              darkMode={isDarkMode}
              language="json"
              showValidation
              height={Math.max(1, jsonText.split("\n").length) * 20 + 20}
              onChange={(text) => {
                setJsonText(text);
                try {
                  setConfig(configSchema.parse(JSON.parse(text)));
                  setJsonValid(true);
                } catch {
                  setJsonValid(false);
                }
              }}
            />
          </div>
        )}
        {showButtons && (
          <div className="flex justify-end gap-2">
            <Button
              type="button"
              variant="outline"
              onClick={onCancel}
              disabled={busy}
            >
              {t("common.cancel")}
            </Button>
            <Button type="submit" disabled={busy || !ready}>
              {submitLabel}
            </Button>
          </div>
        )}
      </form>
    </Form>
  );
}