Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

84.4 KB
Raw Blame
import { hardwareUnavailableReason } from '../../utils/systemCapabilities';
import { useMemo, useState } from 'react';
import { Link } from 'react-router';
import { AlertTriangle, Braces, Cpu, Link2, Plug, SlidersHorizontal } from 'lucide-react';
import toast from '../ui/Toast';
import * as api from '../../services/api';
import { sortProviderModels, filterHardwareCompatibleProviderModels, filterGenerationModels, isEmbeddingModel, isProviderHardwareCompatible, isProviderModelHardwareCompatible, mergeModelLists, configuredDefaultIn, localBackendForProvider, mergeObservedContextWindows, withRuntimeContextWindow, modelOptionLabel, isProcessProvider, isLocalEndpoint, effectiveModelContextWindow, isRunnerAllowedCommand, effortLevelsForProvider, isOllamaBackedProvider, gatewayForProvider, isClaudeCommandProvider, generationControlsFor, isCodexProvider } from '../../utils/providers';
import Banner from '../ui/Banner';
import {
  formatDurationMs,
  formatContextLength,
  formatCount,
  parseTimeoutMs,
  TIMEOUT_INPUT_MIN_MS,
  TIMEOUT_INPUT_MAX_MS,
  TIMEOUT_INPUT_STEP_MS,
} from '../../utils/formatters';
import EffortSelect from '../cos/EffortSelect';
import Drawer from '../Drawer';
import useDrawerTab from '../../hooks/useDrawerTab';
import { FormField } from '../ui/FormField';
import { GatewayKeyHint } from './ProviderNotices';
import ProviderModelAccess from './ProviderModelAccess';
import { CONFIGURED_MODEL_KEYS, configuredModelsOf, normalizeModelAccess, providerModelCatalog, scopeModelsByAccess } from '../../utils/providerModelAccess';

// The provider editor's Drawer tabs. `connection` is the default, so a bare
// /ai/edit/:providerId deep link opens on the identity/transport fields; the
// others are reachable as /ai/edit/:providerId?providerTab=<id>.
const PROVIDER_FORM_TABS = [
  { id: 'connection', label: 'Connection', icon: Plug },
  { id: 'models', label: 'Models', icon: Cpu },
  { id: 'generation', label: 'Generation', icon: SlidersHorizontal },
  { id: 'environment', label: 'Environment', icon: Braces },
];
const PROVIDER_FORM_TAB_IDS = PROVIDER_FORM_TABS.map(t => t.id);

// Numeric bounds for the editor's number inputs. Declared once so an input's own
// `min`/`max` and the submit-time check that stands in for it (the drawer
// unmounts inactive tabs, so the browser can't validate a field the user isn't
// looking at) can never drift apart. Mirrors the provider schema in
// `server/lib/aiToolkit/validation.js`; `timeout` is absent because it has a
// shared parser (`parseTimeoutMs`) that already owns its bounds.
const PROVIDER_FIELD_RANGES = {
  tuiPromptDelayMs: { min: 250, max: 60000 },
  contextWindow: { min: 512, max: 2097152 },
  numCtx: { min: 512, max: 1048576 },
  temperature: { min: 0, max: 2 },
  topP: { min: 0, max: 1 },
};

/** A space-separated argv input as the array the provider schema takes. */
const argList = (text) => (text ? text.split(' ').filter(Boolean) : []);
const modelListFromText = (text) => text.split(',').map(model => model.trim()).filter(Boolean);

const rangeMessage = (label, { min, max }, unit = '') =>
  `${label} must be between ${formatCount(min)} and ${formatCount(max)}${unit ? ` ${unit}` : ''}`;

// The four optional capability-tier overrides, in picker order (#8149).
const TIER_FIELDS = [
  { key: 'lightModel', label: 'Light (fast)', dot: 'bg-port-success', placeholder: 'haiku' },
  { key: 'mediumModel', label: 'Medium (balanced)', dot: 'bg-port-warning', placeholder: 'sonnet' },
  { key: 'heavyModel', label: 'Heavy (powerful)', dot: 'bg-port-error', placeholder: 'opus' },
  { key: 'ultraModel', label: 'Ultra (frontier)', dot: 'bg-port-error', placeholder: 'Fable or Astra model ID' },
];

export default function ProviderForm({ provider, daemonReadiness = null, onClose, onSave, onEditProvider, allProviders = [], localModels = { ollama: [], lmstudio: [], ctxById: {}, hardwareCompatibilityByBackend: {} }, runnerAllowedCommands = null }) {
  const [modelsText, setModelsText] = useState(() => providerModelCatalog(provider).join(', '));
  const [formData, setFormData] = useState({
    name: provider?.name || '',
    type: provider?.type || 'cli',
    command: provider?.command || '',
    args: provider?.args?.join(' ') || '',
    endpoint: provider?.endpoint || '',
    apiKey: '',
    allowCustomEndpoint: provider?.allowCustomEndpoint === true,
    ignoreUserConfig: provider?.ignoreUserConfig === true,
    codexAuthMode: provider?.codexAuthMode || '',
    lowPriorityOnUsageLimit: provider?.lowPriorityOnUsageLimit === true,
    // The FULL advertised catalog, never the model-access-scoped `models` the
    // payload carries. This textarea is saved verbatim, so seeding it from the
    // scoped list would let an ordinary Save persist the narrowed catalog over
    // the real one — recoverable only by a refresh, and silently wrong until then.
    models: providerModelCatalog(provider),
    // Absent on a record with no policy, which is the shape the server reads as
    // "unconstrained". `null` (not `undefined`) when cleared, so the PATCH
    // spread-merge sees a clear rather than "unchanged".
    modelAccess: provider?.modelAccess || null,
    hardwareRequirements: provider?.hardwareRequirements,
    modelHardwareRequirements: provider?.modelHardwareRequirements,
    defaultModel: provider?.defaultModel || '',
    effort: provider?.effort || '',
    lightModel: provider?.lightModel || '',
    mediumModel: provider?.mediumModel || '',
    heavyModel: provider?.heavyModel || '',
    ultraModel: provider?.ultraModel || '',
    fallbackProvider: provider?.fallbackProvider || '',
    fallbackModel: provider?.fallbackModel || '',
    numCtx: provider?.numCtx ?? '',
    // All three seed from the record ONLY. Seeding a value the provider does not
    // have would let an unrelated Save pin it — the editor must be able to leave
    // "unset" alone, since unset is what lets each backend keep its own default.
    temperature: provider?.temperature ?? '',
    topP: provider?.topP ?? '',
    thinking: provider?.thinking === true ? 'true' : provider?.thinking === false ? 'false' : '',
    contextWindow: provider?.contextWindow ?? '',
    timeout: provider?.timeout || 300000,
    enabled: provider?.enabled !== false,
    textTransportEnabled: provider?.textTransportEnabled === true
      && provider?.textTransportReadRiskAcknowledged === true,
    textTransportReadRiskAcknowledged: provider?.textTransportReadRiskAcknowledged === true,
    envVars: provider?.envVars || {},
    secretEnvVars: provider?.secretEnvVars || [],
    headlessArgs: provider?.headlessArgs?.join(' ') || '',
    tuiPromptDelayMs: provider?.tuiPromptDelayMs || 2500,
    credentialBootstrapSetupCommand: provider?.credentialBootstrap?.setupCommand || '',
    credentialBootstrapCommand: provider?.credentialBootstrap?.command || '',
    credentialBootstrapEnvCommand: provider?.credentialBootstrap?.envCommand?.join('\n') || '',
    credentialBootstrapArgs: provider?.credentialBootstrap?.args?.join(' ') || '',
    credentialBootstrapHarnessId: provider?.credentialBootstrap?.harnessId || '',
    credentialBootstrapArgsSeparator: provider?.credentialBootstrap?.argsSeparator || '',
  });

  const [activeTab, setActiveTab] = useDrawerTab('providerTab', 'connection', PROVIDER_FORM_TAB_IDS);
  // Preset structure (#7565). A DERIVED preset takes its program, endpoint,
  // credential, environment and catalog from the service it names on every
  // save; the server refuses a direct edit to one of those with a pointer at
  // the service, so the banner says so up front. A legacy record the server
  // reports convertible gets the one-click conversion here.
  const isDerived = provider?.presetKind === 'derived';
  const [converting, setConverting] = useState(false);
  const convertToDerived = async () => {
    setConverting(true);
    const derived = await api.deriveProviderPreset(provider.id).catch(() => null);
    setConverting(false);
    if (!derived) return;
    toast.success(`Now derived from service "${derived.serviceId}"`);
    onSave();
  };

  const [newEnvKey, setNewEnvKey] = useState('');
  const [newEnvValue, setNewEnvValue] = useState('');
  const [newEnvSecret, setNewEnvSecret] = useState(false);

  // A CLI create may declare that the same harness ALSO runs as a TUI, which
  // mints both records at once (`modes` on POST /api/providers). Neither field
  // is ever part of a provider record, so both stay out of `formData` — a
  // scratch value that rode the `...formData` spread would have to be deleted
  // back out of every payload.
  //
  // Editing never offers it: pairing describes two records being created
  // together, which a save against one existing record cannot mean. An existing
  // CLI record gains its TUI sibling from the card's "Add interactive mode"
  // instead (`POST /api/providers/:id/modes/tui`), which mints it from what is
  // already stored rather than from a re-submitted form.
  const canPairModes = !provider && formData.type === 'cli';
  const [alsoTui, setAlsoTui] = useState(false);
  const [tuiArgs, setTuiArgs] = useState('');

  // The CLI record stays the one this form edits; the TUI sibling is minted
  // from it, so `formData.type` remains a real type and every type-derived
  // control below behaves exactly as it does for a plain CLI provider.
  const createsModePair = canPairModes && alsoTui;
  const isClaudeTui = formData.type === 'tui'
    && isClaudeCommandProvider({ ...provider, ...formData, id: provider?.id });

  // Live installed Ollama/LM Studio models, folded into the model pickers so a
  // local provider shows what's actually installed — not just the stale `models`
  // list stored on the provider record (the "Command R+ / Gemma missing" bug).
  // Passed down from the page, which already holds this status for the cards.
  const liveModelsFor = (p) => {
    const backend = localBackendForProvider(p);
    return backend ? localModels[backend] : [];
  };

  const liveHardwareFor = (p) => {
    const backend = localBackendForProvider(p);
    return backend ? localModels.hardwareCompatibilityByBackend?.[backend] || {} : {};
  };

  // Generation pickers (default + light/medium/heavy tiers) — drop embedding-only
  // models (and internal sentinels) so an embedding can't be chosen as a model
  // that runs prompts, consistent with the fallback picker below.
  const mergedModels = mergeModelLists(formData.models, liveModelsFor(formData));
  // The tier models the access policy must never scope out. Memoized on the six
  // fields themselves rather than on `formData`, so typing in any other field
  // cannot re-scope the whole catalog in the policy editor's preview.
  const modelAccessKeeps = useMemo(
    () => configuredModelsOf(formData),
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [formData.defaultModel, formData.lightModel, formData.mediumModel,
      formData.heavyModel, formData.ultraModel, formData.fallbackModel],
  );
  // The server publishes compatibility for both the provider runtime and any
  // explicitly annotated model. Unknown probe results stay in the list; only a
  // definitive mismatch is hidden.
  // The live windows a local daemon is SERVING (off the readiness poll) fold in
  // here rather than onto `provider`, because this object is derived and shown,
  // never saved — `modelContextWindows` is not a form field, so an observation
  // can reach "Budgeter uses …" and the model-option labels without any risk of
  // a Save writing a running process's window into the record (#7441).
  // The daemon's LAUNCH ceiling folds in the same way and for the same reason
  // (#7472): it is resolved server-side from the ambient `OLLAMA_CONTEXT_LENGTH`
  // the browser cannot read, and a typed-but-unsaved `numCtx` still outranks it
  // — the clamp reads the record's own rung first.
  const capabilityProvider = withRuntimeContextWindow(mergeObservedContextWindows({
    ...provider,
    ...formData,
    id: provider?.id,
    models: mergedModels,
    modelHardwareCompatibility: {
      ...provider?.modelHardwareCompatibility,
      ...liveHardwareFor(formData),
    },
  }, daemonReadiness?.contextWindows), daemonReadiness?.runtimeContextWindow);
  // Scoped by the same Model Access policy `ProviderModelAccess` previews above,
  // so the Default Model + tier selects can't offer a model the policy hides —
  // they used to read straight off `mergedModels` and ignore the policy entirely.
  // `keep` preserves an already-configured tier's model even if the (possibly
  // just-edited) policy would now exclude it, matching the editor's own preview.
  const availableModels = scopeModelsByAccess(
    filterHardwareCompatibleProviderModels(
      filterGenerationModels(mergedModels),
      capabilityProvider,
    ),
    normalizeModelAccess(formData.modelAccess),
    { keep: modelAccessKeeps },
  );
  const configuredModels = [
    formData.defaultModel,
    formData.lightModel,
    formData.mediumModel,
    formData.heavyModel,
    formData.ultraModel,
  ].filter((model) => model
    && !isEmbeddingModel(model)
    && !availableModels.includes(model)
    && !isProviderModelHardwareCompatible(capabilityProvider, model));
  // A provider can pin its tiers to the "use the CLI's own default" sentinel
  // while still publishing a real model catalog (Antigravity: `agy models`
  // lists real ids, but PortOS leaves the tiers on agy's own default). The
  // sentinel is filtered out of `availableModels`, so without an explicit
  // option for it the four selects below would hold a value matching no option
  // and render blank — reading as "no model configured" when one is.
  const configuredDefault = configuredDefaultIn(mergedModels);
  // The markers that identify a backed provider (`ollamaBacked`, `llamaBacked`,
  // `gatewayBacked`) are NOT form fields, so a shape built from `formData`
  // alone loses them — which hid the effort ladder on the OpenCode-Ollama
  // providers, whose ladder is keyed on `ollamaBacked`. Merge the live edits
  // over the stored record instead, so edits to command/endpoint/envVars count
  // immediately while the markers survive.
  // Shared option list for the Default Model + tier selects, so the sentinel
  // option can't be added to some and missed on others. Only the empty row's
  // meaning differs: no Default Model is "None", while an empty tier INHERITS
  // the Default Model (resolveProviderModelTier) — so a preset only sets the
  // tiers it wants on a different model (#8149).
  const sortedTierModels = sortProviderModels([...new Set([...configuredModels, ...availableModels])]);
  const modelSelectOptions = (emptyLabel = 'None') => (
    <>
      <option value="">{emptyLabel}</option>
      {configuredDefault && (
        <option value={configuredDefault}>Use the CLI&apos;s configured default</option>
      )}
      {sortedTierModels.map(model => (
        <option key={model} value={model} disabled={!availableModels.includes(model)}>
          {modelOptionLabel(model, localModels.ctxById, capabilityProvider)}
          {!availableModels.includes(model) ? ' (unavailable on this machine)' : ''}
        </option>
      ))}
    </>
  );

  // Filter out current provider from fallback options (treat undefined enabled as enabled)
  const fallbackOptions = allProviders.filter(p => p.id !== provider?.id
    && p.enabled !== false
    && (isProviderHardwareCompatible(p) || p.id === formData.fallbackProvider));

  // The fallback model is a model OF the selected fallback provider, so its
  // option list comes from that provider's `models` — merged with the live
  // installed list for local backends, and embedding-only models dropped (a
  // fallback runs prompts, so `nomic-embed-text` must never be selectable here).
  const selectedFallbackProvider = allProviders.find(p => p.id === formData.fallbackProvider);
  const fallbackCapabilityProvider = selectedFallbackProvider && {
    ...selectedFallbackProvider,
    modelHardwareCompatibility: {
      ...selectedFallbackProvider.modelHardwareCompatibility,
      ...liveHardwareFor(selectedFallbackProvider),
    },
  };
  const fallbackModelOptions = filterGenerationModels(
    mergeModelLists(selectedFallbackProvider?.models, liveModelsFor(selectedFallbackProvider)),
  );
  const compatibleFallbackModelOptions = sortProviderModels(filterHardwareCompatibleProviderModels(
    fallbackModelOptions,
    fallbackCapabilityProvider,
  ));
  const fallbackModelIsUnavailable = Boolean(
    formData.fallbackModel
    && !isEmbeddingModel(formData.fallbackModel)
    && !compatibleFallbackModelOptions.includes(formData.fallbackModel)
    && !isProviderModelHardwareCompatible(fallbackCapabilityProvider, formData.fallbackModel)
  );
  // `capabilityProvider`, not `formData`: the per-model windows model refresh
  // recorded (`modelContextWindows`) live on the RECORD and are not form fields,
  // so reading formData alone reported the assumed 128K for a model whose real
  // window PortOS already knows.
  const plannedContextLabel = formatContextLength(
    effectiveModelContextWindow(capabilityProvider, formData.defaultModel)
  );
  // `num_ctx` is meaningful for any provider whose tokens come from Ollama, not
  // just `api` ones: an `api` provider sends it on every request, while an
  // Ollama-backed CLI/TUI (claude-ollama, opencode-ollama) talks to the daemon
  // itself, so PortOS applies it by reloading Ollama at that window before the
  // run (server/services/ollamaAgentContext.js). Gating the field to `api` left
  // those providers stuck on Ollama's VRAM-based 32K auto-pick, which an agent
  // harness overruns mid-task. Reads `capabilityProvider` because the
  // `ollamaBacked` marker that identifies opencode-ollama (whose envVars carry
  // no ANTHROPIC_BASE_URL) is not a form field.
  const showsNumCtx = formData.type === 'api' || isOllamaBackedProvider(capabilityProvider);
  // Default sampling/reasoning controls, offered only for the backends PortOS
  // actually forwards them to (see `generationControlsFor`). Reads
  // `capabilityProvider` for the same reason `showsNumCtx` does: `llamaBacked`
  // and friends are record markers, not form fields.
  const generationControls = generationControlsFor(capabilityProvider);
  const parseOptionalIntField = (value) => {
    const input = String(value ?? '').trim();
    if (!input) return null;
    return /^\d+$/.test(input) ? Number(input) : value;
  };
  const parseNumberField = (value) => {
    const input = String(value ?? '').trim();
    return input === '' ? undefined : Number(input);
  };

  // Every constraint the inputs themselves declare (`required`, `type="url"`,
  // `min`/`max`), restated as a check the SUBMIT path runs. The drawer mounts
  // only the active tab, so the browser's own constraint validation sees just
  // that panel: a Save pressed from Models would otherwise ship an unparseable
  // endpoint or an out-of-range num_ctx straight to the server and surface it as
  // a generic API error with no pointer to the offending field. Returns the tab
  // that owns the first problem plus its message, or null when the form is
  // valid. Order matches the tab order so the user is sent to the earliest
  // offending panel.
  const findValidationError = () => {
    const text = (value) => String(value ?? '').trim();
    const outOfRange = (value, { min, max }) => {
      const input = text(value);
      if (input === '') return false;
      const parsed = Number(input);
      return !Number.isFinite(parsed) || parsed < min || parsed > max;
    };

    if (!text(formData.name)) return { tab: 'connection', message: 'Name is required' };
    if (isProcessProvider(formData) && !text(formData.command)) {
      return { tab: 'connection', message: 'Command is required' };
    }
    if (formData.type === 'api') {
      if (!text(formData.endpoint)) return { tab: 'connection', message: 'Endpoint is required' };
      // Mirrors the field's `type="url"` and the server's `z.string().url()`:
      // an absolute URL with a scheme.
      if (!URL.canParse(text(formData.endpoint))) {
        return { tab: 'connection', message: 'Endpoint must be a full URL, e.g. http://localhost:1234/v1' };
      }
    }
    if ((formData.type === 'tui' || createsModePair) && outOfRange(formData.tuiPromptDelayMs, PROVIDER_FIELD_RANGES.tuiPromptDelayMs)) {
      return { tab: 'connection', message: rangeMessage('Prompt Paste Delay', PROVIDER_FIELD_RANGES.tuiPromptDelayMs, 'ms') };
    }
    if (text(formData.timeout) !== '' && parseTimeoutMs(formData.timeout) == null) {
      return {
        tab: 'generation',
        message: `Timeout must be a whole number of ms between ${formatCount(TIMEOUT_INPUT_MIN_MS)} and ${formatCount(TIMEOUT_INPUT_MAX_MS)}`,
      };
    }
    if (outOfRange(formData.contextWindow, PROVIDER_FIELD_RANGES.contextWindow)) {
      return { tab: 'generation', message: rangeMessage('Planning Window', PROVIDER_FIELD_RANGES.contextWindow, 'tokens') };
    }
    if (showsNumCtx && outOfRange(formData.numCtx, PROVIDER_FIELD_RANGES.numCtx)) {
      return { tab: 'generation', message: rangeMessage('Local num_ctx', PROVIDER_FIELD_RANGES.numCtx, 'tokens') };
    }
    if (generationControls?.temperature && outOfRange(formData.temperature, PROVIDER_FIELD_RANGES.temperature)) {
      return { tab: 'generation', message: rangeMessage('Temperature', PROVIDER_FIELD_RANGES.temperature) };
    }
    if (generationControls?.topP && outOfRange(formData.topP, PROVIDER_FIELD_RANGES.topP)) {
      return { tab: 'generation', message: rangeMessage('Top-P', PROVIDER_FIELD_RANGES.topP) };
    }
    return null;
  };

  const handleSubmit = async (e) => {
    e.preventDefault();

    const invalid = findValidationError();
    if (invalid) {
      setActiveTab(invalid.tab);
      toast.error(invalid.message);
      return;
    }

    const tuiPromptDelay = parseInt(formData.tuiPromptDelayMs, 10);
    // Blank input is omitted so the server keeps the current value. Non-empty
    // invalid input (e.g. '1e3', '500', 'abc') is sent as the raw string so
    // Number() cannot silently save an exponent form the client/runner reject;
    // the server's digit-only preprocess leaves it alone and z.number() produces
    // a clear validation error.
    const parsedTimeout = parseTimeoutMs(formData.timeout);
    const timeoutInput = String(formData.timeout ?? '').trim();
    const data = {
      ...formData,
      models: modelListFromText(modelsText),
      args: argList(formData.args),
      headlessArgs: argList(formData.headlessArgs),
      contextWindow: parseOptionalIntField(formData.contextWindow),
      numCtx: showsNumCtx ? parseOptionalIntField(formData.numCtx) : null,
      // A blank generation field clears back to "let the backend pick" — `null`
      // rather than `undefined`, which the server's spread-merge would read as
      // "unchanged" and leave the old pin in place.
      ...(generationControls?.temperature ? { temperature: parseNumberField(formData.temperature) ?? null } : {}),
      ...(generationControls?.topP ? { topP: parseNumberField(formData.topP) ?? null } : {}),
      ...(generationControls?.thinking
        ? { thinking: formData.thinking === '' ? null : formData.thinking === 'true' }
        : {}),
    };
    // `data` opens as a spread of the WHOLE form, so a control this provider
    // doesn't offer rides along regardless of the branches above — and a blank
    // field is `''`, which is not a number (or a boolean) the server schema
    // accepts. Drop what can't be used; the server merges by spread, so
    // anything already stored is left alone.
    if (!generationControls?.temperature) delete data.temperature;
    if (!generationControls?.topP) delete data.topP;
    if (!generationControls?.thinking) delete data.thinking;
    // The generation/fallback pickers filter out embedding-only models, so a
    // stored embedding (from an older config) would be hidden in the UI yet
    // still spread into `data` and silently persisted on an unrelated edit.
    // Clear any embedding value that slipped through so the saved record matches
    // what the picker allows.
    for (const field of CONFIGURED_MODEL_KEYS) {
      if (isEmbeddingModel(data[field])) data[field] = '';
    }
    // An explicit `null` rather than `undefined` when the policy says nothing:
    // the server merges a PATCH by spread, which reads `undefined` as "unchanged"
    // and would leave a policy the user just cleared in place.
    data.modelAccess = normalizeModelAccess(formData.modelAccess);
    // Effort is meaningful only for providers/models that expose an effort
    // ladder. Clear a stale value when an edit switches to an effort-less
    // provider or Antigravity model; narrowed ladders are clamped by the
    // server and remain visible in the selector.
    if (!isProcessProvider(data) || !effortLevelsForProvider({ ...provider, ...data, id: provider?.id }, data.defaultModel)) {
      data.effort = '';
    }
    if (parsedTimeout != null) {
      data.timeout = parsedTimeout;
    } else if (timeoutInput === '') {
      delete data.timeout;
    } else {
      data.timeout = formData.timeout;
    }
    if (formData.type === 'tui') {
      if (Number.isFinite(tuiPromptDelay)) data.tuiPromptDelayMs = tuiPromptDelay;
      else delete data.tuiPromptDelayMs;
    } else {
      delete data.tuiPromptDelayMs;
    }
    // Both modes from one submit. Only what actually differs per mode is
    // declared: everything else on the body is shared by both records, which is
    // what lets the server pair them as one harness rather than two unrelated
    // routes. `cli: {}` because the body's own `args` / `headlessArgs` already
    // describe the CLI record — the key is there so the pair is declared.
    if (createsModePair) {
      data.modes = {
        cli: {},
        tui: {
          args: argList(tuiArgs),
          ...(Number.isFinite(tuiPromptDelay) ? { tuiPromptDelayMs: tuiPromptDelay } : {}),
        },
      };
    }
    // These controls belong only to the advertised Codex subscription
    // transport. Do not stamp false capability fields onto unrelated provider
    // records when their editor saves an ordinary connection change.
    if (provider?.textTransport !== 'codex-app-server') {
      delete data.textTransportEnabled;
      delete data.textTransportReadRiskAcknowledged;
    }
    // `--ignore-user-config` is a Codex flag. Never stamp it onto a record
    // running another vendor's binary, where it would be a stored lie.
    if (!isCodexProvider({ ...provider, ...data, id: provider?.id })) {
      delete data.ignoreUserConfig;
      delete data.codexAuthMode;
    } else {
      data.codexAuthMode = data.codexAuthMode || null;
    }
    // `/low-priority` is a Claude TUI-only command. Do not persist a dormant
    // toggle on a CLI/headless or another vendor's provider record.
    if (formData.type !== 'tui' || !isClaudeCommandProvider({ ...provider, ...data, id: provider?.id })) {
      delete data.lowPriorityOnUsageLimit;
    }

    // Fold the five flat scratch fields into the nested shape the server
    // schema expects. Only a CLI/TUI provider carries the field at all (the
    // section above is gated the same way): an api-type save must not ship
    // `credentialBootstrap: null` onto a record that never had one. A named
    // bootstrap command becomes the object; an emptied one becomes an explicit
    // `null` (absent means "unchanged" on a PATCH) — never
    // `{ command: '' }`, which would fail the server's `min(1)` check on every
    // unrelated save.
    const bootstrapCommand = formData.credentialBootstrapCommand.trim();
    if (formData.type === 'cli' || formData.type === 'tui') {
      data.credentialBootstrap = bootstrapCommand
        ? {
          ...(formData.credentialBootstrapSetupCommand.trim() ? { setupCommand: formData.credentialBootstrapSetupCommand.trim() } : {}),
          command: bootstrapCommand,
          args: argList(formData.credentialBootstrapArgs),
          ...(formData.credentialBootstrapEnvCommand.trim()
            ? { envCommand: formData.credentialBootstrapEnvCommand.split('\n').filter(line => line.trim()) } : {}),
          ...(formData.credentialBootstrapHarnessId.trim() ? { harnessId: formData.credentialBootstrapHarnessId.trim() } : {}),
          ...(formData.credentialBootstrapArgsSeparator.trim() ? { argsSeparator: formData.credentialBootstrapArgsSeparator.trim() } : {}),
        }
        : null;
    }
    delete data.credentialBootstrapSetupCommand;
    delete data.credentialBootstrapCommand;
    delete data.credentialBootstrapEnvCommand;
    delete data.credentialBootstrapArgs;
    delete data.credentialBootstrapHarnessId;
    delete data.credentialBootstrapArgsSeparator;

    // Only send apiKey if user entered a new value (avoid overwriting existing key with empty string)
    if (!data.apiKey && provider) {
      delete data.apiKey;
    }

    if (provider) {
      await api.updateProvider(provider.id, data);
    } else {
      await api.createProvider(data);
    }

    onSave();
  };

  return (
    <Drawer
      open
      onClose={onClose}
      title={provider ? 'Edit Provider' : 'Add Provider'}
      subtitle={provider ? provider.name : undefined}
      size="lg"
      tabs={PROVIDER_FORM_TABS}
      activeTab={activeTab}
      onTabChange={setActiveTab}
      // A long multi-tab config form: an accidental Esc or backdrop click
      // mid-edit would discard work across every tab.
      closeOnEsc={false}
      closeOnBackdrop={false}
    >
        {/* The Drawer body remounts per active tab (key={currentTab}), so this
            whole form subtree is torn down and rebuilt on every tab switch. All
            mutable state (formData and the new-env-var row) therefore lives in
            this component, above the Drawer — never inside the panels below. */}
        <form onSubmit={handleSubmit} className="space-y-4">
          {provider?.hardwareCompatibility?.state === 'unavailable' && (
            <Banner tone="warning" icon={AlertTriangle}>
              <p>
                {hardwareUnavailableReason('This provider', provider.hardwareCompatibility)}.
                Its models are hidden from selection until the host matches those requirements.
              </p>
            </Banner>
          )}
          {activeTab === 'connection' && (
            <div className="space-y-4">
              {isDerived && (
                <Banner tone="info" icon={Link2}>
                  <p>
                    Derived from service <code className="font-mono">{provider.serviceId}</code> via{' '}
                    <code className="font-mono">{provider.harnessId}</code> ({provider.method}). The command, endpoint,
                    API key, backend environment and model catalog come from that service and are refreshed on every
                    save. To edit this service's credentials, open{' '}
                    <Link to={`/ai/services/${encodeURIComponent(provider.serviceId)}`} className="text-port-accent hover:underline">
                      service settings for {provider.serviceId}
                    </Link>. Name, arguments, timeouts, model pins, effort and generation settings are this preset's own.
                  </p>
                </Banner>
              )}
              {provider && !isDerived && provider.presetDerivable && (
                <Banner tone="info" icon={Link2}>
                  <p>
                    This preset holds its own copy of a service's endpoint and credentials. Converting it derives those
                    from the service it already runs on, so a change to the service reaches it automatically.
                  </p>
                  <button
                    type="button"
                    onClick={convertToDerived}
                    disabled={converting}
                    className="mt-2 px-3 py-1.5 text-sm rounded-lg border border-port-accent/50 text-port-accent hover:bg-port-accent/10 disabled:opacity-50"
                  >
                    {converting ? 'Converting…' : 'Convert to derived preset'}
                  </button>
                </Banner>
              )}
              <FormField label="Name *">
                <input
                  type="text"
                  value={formData.name}
                  onChange={(e) => setFormData(prev => ({ ...prev, name: e.target.value }))}
                  required
                  className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                />
              </FormField>

              {/* Connection-owned fields (type, command, endpoint, key, inline
                  bootstrap) are the SERVICE's on a derived preset (#7565): the
                  server re-materializes them on every save and refuses an edit
                  that would move one, so the form shows none of them (#7567).
                  The values stay in `formData` untouched, which is what keeps
                  the per-tab validation satisfied. */}
              {!isDerived && (
              <FormField label="Type *">
                <select
                  value={formData.type}
                  onChange={(e) => setFormData(prev => ({ ...prev, type: e.target.value }))}
                  className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                >
                  <option value="cli">CLI</option>
                  <option value="tui">TUI</option>
                  <option value="api">API</option>
                </select>
              </FormField>
              )}

              {(formData.type === 'cli' || formData.type === 'tui') && (
                <>
                  {!isDerived && (
                  <FormField label="Command *">
                    <input
                      type="text"
                      value={formData.command}
                      onChange={(e) => setFormData(prev => ({ ...prev, command: e.target.value }))}
                      placeholder={formData.type === 'tui' ? 'codex' : 'claude'}
                      required={formData.type === 'cli' || formData.type === 'tui'}
                      className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                    />
                    {/* Informational only — an off-allowlist command saves fine and runs
                        fine in direct-spawn mode; it just can't be launched by the CoS
                        Agent Runner. Rejecting the save would break that valid config. */}
                    {isRunnerAllowedCommand(formData.command, runnerAllowedCommands) === false && (
                      <Banner tone="warning" icon={AlertTriangle} className="mt-2">
                        <p>
                          <code className="font-mono break-all">{formData.command}</code> is not on the CoS Agent Runner’s
                          command allowlist, so <code className="font-mono">/spawn</code> and{' '}
                          <code className="font-mono">/spawn-tui</code> will refuse it. Saving is fine — the provider still
                          runs in direct-spawn mode and everywhere else.
                        </p>
                        <p className="mt-1 text-port-warning/80 break-words">
                          Allowlisted: {runnerAllowedCommands.join(', ')}
                        </p>
                      </Banner>
                    )}
                  </FormField>
                  )}

                  <FormField label="Arguments (space-separated)">
                    <input
                      type="text"
                      value={formData.args}
                      onChange={(e) => setFormData(prev => ({ ...prev, args: e.target.value }))}
                      placeholder={formData.type === 'tui' ? '--dangerously-skip-permissions' : '--print -p'}
                      className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                    />
                  </FormField>

                  {/* Most coding harnesses run both headless and interactively.
                      PortOS stores one execution mode per record, so configuring
                      both used to mean adding the same command twice and hoping
                      the two records matched closely enough to be treated as one
                      harness. Checking this mints the pair from this one form. */}
                  {canPairModes && (
                    <div>
                      <label htmlFor="provider-also-tui" className="flex items-start gap-2 text-sm text-gray-300 cursor-pointer">
                        <input
                          id="provider-also-tui"
                          type="checkbox"
                          checked={alsoTui}
                          onChange={(e) => setAlsoTui(e.target.checked)}
                          className="mt-1"
                        />
                        <span>
                          This command also runs as a TUI
                          <span className="block text-xs text-gray-500">
                            Creates both modes from these shared provider and model settings. The TUI keeps its
                            own arguments and prompt timing.
                          </span>
                        </span>
                      </label>
                    </div>
                  )}

                  {createsModePair && (
                    <FormField label="TUI Arguments (space-separated)">
                      <input
                        type="text"
                        value={tuiArgs}
                        onChange={(e) => setTuiArgs(e.target.value)}
                        placeholder="--dangerously-skip-permissions"
                        className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                      />
                    </FormField>
                  )}

                  {/* Generic support for a harness whose auth is provisioned by an
                      external CLI at spawn time — e.g. a company-internal tool that
                      mints a short-lived token for a proxy and execs the harness
                      itself — instead of a static API key PortOS stores. When set,
                      PortOS spawns the bootstrap command in front of the harness
                      invocation above, exactly as typing `<bootstrap> run <harness>
                      ...` would. Setup Command is shown for the user to run
                      themselves; PortOS never executes it. */}
                  {!isDerived && (
                  <>
                  <p className="text-sm text-gray-400 -mb-1">Credential Bootstrap (optional)</p>
                  <FormField label="Setup Command" compact>
                    <input
                      type="text"
                      value={formData.credentialBootstrapSetupCommand}
                      onChange={(e) => setFormData(prev => ({ ...prev, credentialBootstrapSetupCommand: e.target.value }))}
                      placeholder="One-time step shown to you, e.g. npm install -g @your-org/token-cli"
                      className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                    />
                  </FormField>
                  <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
                    <FormField label="Bootstrap Command" compact>
                      <input
                        type="text"
                        value={formData.credentialBootstrapCommand}
                        onChange={(e) => setFormData(prev => ({ ...prev, credentialBootstrapCommand: e.target.value }))}
                        placeholder="e.g. token-cli"
                        className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                      />
                    </FormField>
                    <FormField label="Bootstrap Args" compact>
                      <input
                        type="text"
                        value={formData.credentialBootstrapArgs}
                        onChange={(e) => setFormData(prev => ({ ...prev, credentialBootstrapArgs: e.target.value }))}
                        placeholder="e.g. run"
                        className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                      />
                    </FormField>
                    <FormField label="Harness ID (optional)" compact>
                      <input
                        type="text"
                        value={formData.credentialBootstrapHarnessId}
                        onChange={(e) => setFormData(prev => ({ ...prev, credentialBootstrapHarnessId: e.target.value }))}
                        placeholder={`Defaults to Command above (${formData.command || 'e.g. claude'})`}
                        className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                      />
                    </FormField>
                    <FormField label="Args Separator (optional)" compact>
                      <input
                        type="text"
                        value={formData.credentialBootstrapArgsSeparator}
                        onChange={(e) => setFormData(prev => ({ ...prev, credentialBootstrapArgsSeparator: e.target.value }))}
                        placeholder="e.g. --"
                        className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                      />
                    </FormField>
                  </div>
                  <FormField label="Review credential command (optional)" compact>
                    <textarea
                      id="provider-bootstrap-env-command"
                      value={formData.credentialBootstrapEnvCommand}
                      onChange={(e) => setFormData(prev => ({ ...prev, credentialBootstrapEnvCommand: e.target.value }))}
                      placeholder={'token-cli\nprint-env'}
                      rows={3}
                      className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                    />
                  </FormField>
                  <p className="text-xs text-gray-500">
                    Optional, and only for tool-free reviews. Leave it empty unless this app
                    PRINTS credentials instead of running the harness itself — a review already
                    runs through the wrap above, so an ordinary bootstrap app needs nothing here.
                    Enter the executable, then one argument per line (no shell quoting).
                    It must print KEY=value lines for provider authentication; JSON and other lines are ignored.
                    Only recognized provider credentials and API endpoints are passed to the reviewer.
                    Credentials are cached in memory for one minute. Do not put secrets in command arguments.
                  </p>
                  <p className="text-xs text-gray-500">
                    For a CLI that provisions its own short-lived credential (e.g. a token for a proxy) and then
                    runs the harness itself. <strong>Setup Command</strong> is a one-time step shown to you —
                    PortOS never runs it. When a Bootstrap Command is set, PortOS spawns it in front of the harness
                    (<code>{formData.credentialBootstrapCommand || '<bootstrap>'} {formData.credentialBootstrapArgs}{' '}
                    {formData.credentialBootstrapHarnessId || formData.command || '<harness>'}{' '}
                    {formData.credentialBootstrapArgsSeparator} …</code>) instead of the harness directly, and never
                    routes this provider through the CoS Agent Runner (its fixed list of known harness binaries has
                    no way to recognize a custom bootstrap binary). <strong>Harness ID</strong> overrides what PortOS calls the harness
                    when the bootstrap CLI uses its own name for it instead of the binary above (e.g.{' '}
                    <code>claude-code</code> rather than <code>claude</code>).{' '}
                    <strong>Args Separator</strong> (e.g. <code>--</code>) is inserted before the harness's own
                    arguments when the bootstrap CLI needs its flags kept apart from the harness's.
                    Bootstrap Args are stored and shown in clear text (agent records, run transcripts, the shell
                    line typed for a TUI session) — put a secret in Environment Variables marked secret, never here.{' '}
                    <strong>Your bootstrap CLI should <code>exec</code> the harness, or forward the signals it
                    receives to it.</strong> Stop, timeout and cancel signal the wrapped spawn's whole process
                    group, so a wrapper that forks is still taken down with its harness — but a wrapper that
                    re-parents the harness out of that group, or traps signals without passing them on, can leave
                    it running after PortOS has finished the run.
                  </p>
                  </>
                  )}

                  {/* The CLI/TUI backends that can authenticate: the vLLM compose
                      stack is started with VLLM_API_KEY, so without this field
                      there is nowhere to put it and the container 401s every
                      model refresh and every run. Reads `capabilityProvider`
                      because `vllmBacked` is a stored marker, not a form field.
                      SGLang has its own field below — its key is OPTIONAL (only
                      set when the operator ran `--api-key`), so the two cannot
                      share one placeholder without telling half the operators to
                      paste a secret that does not exist. */}
                  {!isDerived && capabilityProvider?.vllmBacked && (
                    <FormField label="API Key">
                      <input
                        type="password"
                        value={formData.apiKey}
                        onChange={(e) => setFormData(prev => ({ ...prev, apiKey: e.target.value }))}
                        placeholder={provider?.hasApiKey ? 'Key set — leave blank to keep' : 'Paste VLLM_API_KEY from the stack’s .env'}
                        className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                      />
                      <p className="text-xs text-gray-500 mt-1">
                        The <code>VLLM_API_KEY</code> your compose stack was started with. PortOS puts it on the
                        spawned OpenCode provider and on the model-refresh probe; the container rejects both without it.
                      </p>
                    </FormField>
                  )}

                  {!isDerived && capabilityProvider?.sglangBacked && (
                    <FormField label="API Key (optional)">
                      <input
                        type="password"
                        value={formData.apiKey}
                        onChange={(e) => setFormData(prev => ({ ...prev, apiKey: e.target.value }))}
                        placeholder={provider?.hasApiKey ? 'Key set — leave blank to keep' : 'Only if you started SGLang with --api-key'}
                        className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                      />
                      <p className="text-xs text-gray-500 mt-1">
                        SGLang serves unauthenticated unless you started it with <code>--api-key</code>. Leave this
                        blank in that case — PortOS attaches a key only when one is set.
                        {isClaudeCommandProvider(capabilityProvider)
                          ? <> This <strong>Claude</strong> harness reads it for the model-refresh probe only; the
                            credential its runs authenticate with is <code>ANTHROPIC_AUTH_TOKEN</code> under
                            Environment Variables, so set that to the same key too.</>
                          : <> It rides both the spawned OpenCode provider and the model-refresh probe.</>}
                      </p>
                    </FormField>
                  )}

                  {formData.type === 'cli' && (
                    <FormField label="Headless Args (for simple prompt tasks)">
                      <input
                        type="text"
                        value={formData.headlessArgs}
                        onChange={(e) => setFormData(prev => ({ ...prev, headlessArgs: e.target.value }))}
                        placeholder='--no-session-persistence --disable-slash-commands --tools ""'
                        className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                      />
                      <p className="text-xs text-gray-500 mt-1">
                        Extra CLI flags for lightweight prompt-in/text-out mode (brain classifier, etc.)
                      </p>
                    </FormField>
                  )}

                  {(formData.type === 'tui' || createsModePair) && (
                    <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
                      <FormField label="Prompt Paste Delay (ms)">
                        <input
                          type="number"
                          min={PROVIDER_FIELD_RANGES.tuiPromptDelayMs.min}
                          max={PROVIDER_FIELD_RANGES.tuiPromptDelayMs.max}
                          value={formData.tuiPromptDelayMs}
                          onChange={(e) => setFormData(prev => ({ ...prev, tuiPromptDelayMs: e.target.value }))}
                          className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                        />
                      </FormField>
                      <p className="sm:col-span-2 text-xs text-gray-500">
                        TUI providers stay attached while the provider is silent; they finish on the completion sentinel, process exit, or explicit failure.
                      </p>
                    </div>
                  )}
                  {isClaudeTui && (
                    <div className="space-y-1">
                      <label htmlFor="lowPriorityOnUsageLimit" className="flex items-start gap-2 cursor-pointer">
                        <input
                          id="lowPriorityOnUsageLimit"
                          type="checkbox"
                          aria-describedby="lowPriorityOnUsageLimit-help"
                          checked={formData.lowPriorityOnUsageLimit}
                          onChange={(e) => setFormData(prev => ({ ...prev, lowPriorityOnUsageLimit: e.target.checked }))}
                          className="mt-1"
                        />
                        <span className="text-sm text-gray-300">
                          Continue with Claude Code&apos;s <code>/low-priority</code> mode at its session usage limit
                        </span>
                      </label>
                      <p id="lowPriorityOnUsageLimit-help" className="text-xs text-gray-500 ml-6">
                        This may use weekly allowance and pause while capacity is unavailable. Claude Code must offer the mode for that session.
                      </p>
                    </div>
                  )}
                </>
              )}

              {formData.type === 'api' && (
                <>
                  {!isDerived && (
                  <>
                  <FormField label="Endpoint *">
                    <input
                      type="url"
                      value={formData.endpoint}
                      onChange={(e) => setFormData(prev => ({ ...prev, endpoint: e.target.value }))}
                      placeholder="http://localhost:1234/v1"
                      required={formData.type === 'api'}
                      className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                    />
                  </FormField>

                  <FormField label="API Key">
                    <input
                      type="password"
                      value={formData.apiKey}
                      onChange={(e) => setFormData(prev => ({ ...prev, apiKey: e.target.value }))}
                      placeholder={provider?.hasApiKey
                        ? 'Key set — leave blank to keep'
                        : isLocalEndpoint(formData.endpoint)
                          ? 'Not needed for local endpoints'
                          : 'Paste the key from your provider dashboard'}
                      className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                    />
                    <p className="text-xs text-gray-500 mt-1">
                      This field is the only place API providers read a key from — it's stored on this
                      provider and sent as an <code>Authorization: Bearer</code> header on every request.
                      No environment variable is involved. Hosted APIs (Cerebras, Grok, NVIDIA, OrcaRouter, OpenRouter, …) require
                      one; local backends (Ollama, LM Studio) don't.
                    </p>
                  </FormField>
                  </>
                  )}

                  <FormField label="Custom endpoint">
                    <label htmlFor="allowCustomEndpoint" className="flex items-start gap-2 cursor-pointer">
                      <input
                        id="allowCustomEndpoint"
                        type="checkbox"
                        checked={formData.allowCustomEndpoint}
                        onChange={(e) => setFormData(prev => ({ ...prev, allowCustomEndpoint: e.target.checked }))}
                        className="mt-1"
                      />
                      <span className="text-sm text-gray-300">
                        Allow sending the API key to this custom (non-local, non-allowlisted) endpoint.
                        Loopback/LAN and known providers (OpenAI, Anthropic, OpenRouter, …) are always allowed;
                        cloud-metadata hosts are always blocked. Leave off unless you trust this host.
                      </span>
                    </label>
                  </FormField>
                </>
              )}

              {isCodexProvider({ ...provider, ...formData, id: provider?.id }) && isProcessProvider(formData) && (
                <FormField label="Codex user config">
                  <label htmlFor="ignoreUserConfig" className="flex items-start gap-2 cursor-pointer">
                    <input
                      id="ignoreUserConfig"
                      type="checkbox"
                      checked={formData.ignoreUserConfig}
                      onChange={(e) => setFormData(prev => ({ ...prev, ignoreUserConfig: e.target.checked }))}
                      className="mt-1"
                    />
                    <span className="text-sm text-gray-300">
                      Ignore <code>~/.codex/config.toml</code> when PortOS runs this provider
                      (<code>--ignore-user-config</code>). Leave off to keep today’s behavior. Turn it on when a
                      third-party bridge has re-pointed Codex model routing and you want PortOS runs pinned to
                      the account this card reports on.
                    </span>
                  </label>
                </FormField>
              )}

              {isCodexProvider({ ...provider, ...formData, id: provider?.id }) && isProcessProvider(formData) && (
                <FormField label="Codex authentication">
                  <select
                    id="codexAuthMode"
                    value={formData.codexAuthMode}
                    onChange={(e) => setFormData(prev => ({ ...prev, codexAuthMode: e.target.value }))}
                    className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white"
                  >
                    <option value="">Codex default (its own login state)</option>
                    <option value="chatgpt">ChatGPT subscription</option>
                    <option value="api">API key (API billing)</option>
                  </select>
                  <span className="text-xs text-gray-500 mt-1 block">
                    Pins Codex to one login method (<code>forced_login_method</code>). Some models are only
                    available to one method; ChatGPT subscription and API-key access are billed separately.
                  </span>
                </FormField>
              )}

              <label className="flex items-center gap-2">
                <input
                  type="checkbox"
                  checked={formData.enabled}
                  onChange={(e) => setFormData(prev => ({ ...prev, enabled: e.target.checked }))}
                  className="w-4 h-4 rounded border-port-border bg-port-bg"
                />
                <span className="text-sm text-gray-400">Enabled</span>
              </label>

              {provider?.textTransport === 'codex-app-server' && (
                <div className="max-w-3xl rounded-lg border border-port-warning/40 bg-port-warning/10 p-3 space-y-3">
                  <div className="space-y-1">
                    <p className="text-sm font-medium text-port-warning">ChatGPT subscription text calls</p>
                    <p className="text-xs text-gray-300">
                      PortOS blocks writes, network access, MCP servers, and web search for these calls.
                      Codex can still read local files by absolute path, so untrusted prompt text could make
                      a saved response contain local file contents.
                    </p>
                  </div>
                  <div className="space-y-2">
                    <div className="flex items-start gap-2">
                      <input
                        id="codex-text-read-risk"
                        type="checkbox"
                        checked={formData.textTransportReadRiskAcknowledged}
                        onChange={(e) => setFormData(prev => ({
                          ...prev,
                          textTransportReadRiskAcknowledged: e.target.checked,
                          textTransportEnabled: e.target.checked ? prev.textTransportEnabled : false,
                        }))}
                        className="mt-0.5 w-4 h-4 rounded border-port-border bg-port-bg"
                      />
                      <label htmlFor="codex-text-read-risk" className="text-sm text-gray-300">
                        I understand that Codex may read local files during generic text calls.
                      </label>
                    </div>
                    <div className="flex items-start gap-2">
                      <input
                        id="codex-text-transport-enabled"
                        type="checkbox"
                        checked={formData.textTransportEnabled}
                        disabled={!formData.textTransportReadRiskAcknowledged}
                        onChange={(e) => setFormData(prev => ({ ...prev, textTransportEnabled: e.target.checked }))}
                        className="mt-0.5 w-4 h-4 rounded border-port-border bg-port-bg disabled:opacity-50"
                      />
                      <label
                        htmlFor="codex-text-transport-enabled"
                        className={`text-sm ${formData.textTransportReadRiskAcknowledged ? 'text-gray-300' : 'text-gray-500'}`}
                      >
                        Allow this provider to serve generic text calls.
                      </label>
                    </div>
                  </div>
                </div>
              )}

              {gatewayForProvider(provider) && (
                <GatewayKeyHint
                  gateway={gatewayForProvider(provider)}
                  sibling={allProviders.find(p => p.id === gatewayForProvider(provider).id)}
                  onEdit={onEditProvider}
                />
              )}
            </div>
          )}

          {activeTab === 'models' && (
            <div className="space-y-4">
              <FormField label={<>
                  Available Models
                  {formData.type === 'api' && <span className="text-xs text-gray-500 ml-2">(Use Refresh button after saving)</span>}
                </>}>
                <textarea
                  value={modelsText}
                  onChange={(e) => {
                    const text = e.target.value;
                    setModelsText(text);
                    const models = modelListFromText(text);
                    setFormData(prev => ({ ...prev, models }));
                  }}
                  placeholder="model-1, model-2, model-3"
                  rows={2}
                  className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white resize-none focus:border-port-accent focus:outline-hidden"
                />
                <p className="text-xs text-gray-500 mt-1">
                  Comma-separated list of available models. For API providers, use Refresh to auto-populate.
                </p>
              </FormField>

              {/* Which of that catalog this install is entitled to run. Fed the
                  FULL list (`formData.models`, seeded from `modelCatalog`), because
                  the policy is authored against everything the upstream
                  advertises — the scoped view is what it produces. */}
              <ProviderModelAccess
                catalog={formData.models || []}
                value={formData.modelAccess}
                configuredModels={modelAccessKeeps}
                onChange={(modelAccess) => setFormData(prev => ({ ...prev, modelAccess }))}
              />

              <FormField label="Default Model">
                {availableModels.length > 0 ? (
                  <select
                    value={formData.defaultModel}
                    onChange={(e) => setFormData(prev => ({ ...prev, defaultModel: e.target.value }))}
                    className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                  >
                    {modelSelectOptions()}
                  </select>
                ) : (
                  <input
                    type="text"
                    value={formData.defaultModel}
                    onChange={(e) => setFormData(prev => ({ ...prev, defaultModel: e.target.value }))}
                    placeholder="claude-sonnet-4-20250514"
                    className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                  />
                )}
                <p className="text-xs text-gray-500 mt-1">
                  {availableModels.length > 0
                    ? 'Model to use when no tier is specified'
                    : 'Save and test provider to fetch available models'}
                </p>
              </FormField>

              <EffortSelect
                provider={isProcessProvider(capabilityProvider) ? capabilityProvider : null}
                model={formData.defaultModel}
                value={formData.effort}
                onChange={(effort) => setFormData(prev => ({ ...prev, effort }))}
                label="Default Effort"
                hint={generationControls
                  ? 'Reasoning effort used when a run does not specify one — passed to the local model as reasoningEffort.'
                  : 'Reasoning effort used when a run does not specify one.'}
              />

              {/* Model Tiers */}
              <div className="border-t border-port-border pt-4 mt-4">
                <h4 className="text-sm font-medium text-gray-300 mb-3">Model Tier Overrides</h4>
                <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
                  {TIER_FIELDS.map(({ key, label, dot, placeholder }) => (
                    <FormField key={key} labelClassName="block text-xs text-gray-400 mb-1" label={<>
                        <span className={`inline-block w-2 h-2 rounded-full ${dot} mr-1`}></span>
                        {label}
                      </>}>
                      {availableModels.length > 0 ? (
                        <select
                          value={formData[key]}
                          onChange={(e) => setFormData(prev => ({ ...prev, [key]: e.target.value }))}
                          className="w-full px-2 py-1.5 bg-port-bg border border-port-border rounded-lg text-white text-sm focus:border-port-accent focus:outline-hidden"
                        >
                          {modelSelectOptions(key === 'ultraModel' ? 'Inherit Heavy' : 'Inherit Default Model')}
                        </select>
                      ) : (
                        <input
                          type="text"
                          value={formData[key]}
                          onChange={(e) => setFormData(prev => ({ ...prev, [key]: e.target.value }))}
                          placeholder={placeholder}
                          className="w-full px-2 py-1.5 bg-port-bg border border-port-border rounded-lg text-white text-sm focus:border-port-accent focus:outline-hidden"
                        />
                      )}
                    </FormField>
                  ))}
                </div>
                <p className="text-xs text-gray-500 mt-2">
                  {availableModels.length > 0
                    ? 'Optional. A task, role, or prompt stage that names a tier runs on its model here; a blank tier uses the Default Model (Ultra uses Heavy first). Nothing picks a tier automatically.'
                    : 'Save provider, then use Test or Refresh to fetch available models'}
                </p>
              </div>

              {/* Fallback Provider */}
              <div className="border-t border-port-border pt-4 mt-4">
                <FormField label="Fallback Provider">
                <select
                  value={formData.fallbackProvider}
                  onChange={(e) => setFormData(prev => ({
                    ...prev,
                    fallbackProvider: e.target.value,
                    // The model belongs to the fallback provider; clear it when the
                    // provider changes so a stale model from the previous pick
                    // doesn't carry over.
                    fallbackModel: ''
                  }))}
                  className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                >
                  <option value="">None (use system default)</option>
                  {fallbackOptions.map(p => (
                    <option key={p.id} value={p.id} disabled={!isProviderHardwareCompatible(p)}>
                      {p.name}{!isProviderHardwareCompatible(p) ? ' (unavailable on this machine)' : ''}
                    </option>
                  ))}
                </select>
                <p className="text-xs text-gray-500 mt-1">
                  If this provider hits a usage limit or becomes unavailable, tasks will automatically use the fallback provider.
                </p>
                </FormField>

                {formData.fallbackProvider && (
                  <FormField label="Fallback Model" className="mt-3">
                    {compatibleFallbackModelOptions.length > 0 ? (
                      <select
                        value={formData.fallbackModel}
                        onChange={(e) => setFormData(prev => ({ ...prev, fallbackModel: e.target.value }))}
                        className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                      >
                        <option value="">Use fallback provider's default</option>
                        {fallbackModelIsUnavailable && (
                          <option value={formData.fallbackModel} disabled>
                            {modelOptionLabel(formData.fallbackModel, localModels.ctxById, selectedFallbackProvider)} (unavailable on this machine)
                          </option>
                        )}
                        {compatibleFallbackModelOptions.map(model => (
                          <option key={model} value={model}>{modelOptionLabel(model, localModels.ctxById, selectedFallbackProvider)}</option>
                        ))}
                      </select>
                    ) : (
                      <input
                        type="text"
                        value={formData.fallbackModel}
                        onChange={(e) => setFormData(prev => ({ ...prev, fallbackModel: e.target.value }))}
                        placeholder="Use fallback provider's default"
                        className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                      />
                    )}
                    <p className="text-xs text-gray-500 mt-1">
                      Model to run on the fallback provider. Leave blank to use that provider's default model.
                    </p>
                  </FormField>
                )}
              </div>
            </div>
          )}

          {activeTab === 'generation' && (
            <div className="space-y-4">
              <FormField label="Timeout (ms)">
                <input
                  type="number"
                  inputMode="numeric"
                  min={TIMEOUT_INPUT_MIN_MS}
                  max={TIMEOUT_INPUT_MAX_MS}
                  step={TIMEOUT_INPUT_STEP_MS}
                  value={formData.timeout}
                  onChange={(e) => setFormData(prev => ({ ...prev, timeout: e.target.value }))}
                  className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                />
                <p className="text-xs text-gray-500 mt-1">
                  {(() => {
                    // Same parser the submit path uses, so the displayed
                    // duration always matches what would be saved. parseTimeoutMs
                    // returns null for out-of-range/invalid → fall back to the
                    // generic cap message.
                    const ms = parseTimeoutMs(formData.timeout);
                    return ms != null
                      ? `≈ ${formatDurationMs(ms)} per run`
                      : `Per-call cap. Server max: ${formatCount(TIMEOUT_INPUT_MAX_MS)} ms (${formatDurationMs(TIMEOUT_INPUT_MAX_MS)}).`;
                  })()}
                </p>
              </FormField>

              <div className="border-t border-port-border pt-4 mt-4">
                <h4 className="text-sm font-medium text-gray-300 mb-3">Context Window</h4>
                <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
                  <FormField label="Planning Window">
                    <input
                      type="number"
                      inputMode="numeric"
                      min={PROVIDER_FIELD_RANGES.contextWindow.min}
                      max={PROVIDER_FIELD_RANGES.contextWindow.max}
                      value={formData.contextWindow}
                      onChange={(e) => setFormData(prev => ({ ...prev, contextWindow: e.target.value }))}
                      placeholder="Auto from model"
                      className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                    />
                    <p className="text-xs text-gray-500 mt-1">
                      {plannedContextLabel ? `Budgeter uses ${plannedContextLabel}` : 'Leave blank to use model/provider defaults'}
                    </p>
                  </FormField>

                  {showsNumCtx && (
                    <FormField label="Local num_ctx">
                      <input
                        type="number"
                        inputMode="numeric"
                        min={PROVIDER_FIELD_RANGES.numCtx.min}
                        max={PROVIDER_FIELD_RANGES.numCtx.max}
                        value={formData.numCtx}
                        onChange={(e) => setFormData(prev => ({ ...prev, numCtx: e.target.value }))}
                        placeholder="Ollama request size"
                        className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                      />
                      <p className="text-xs text-gray-500 mt-1">
                        {formData.type === 'api'
                          ? 'Sent to compatible local backends; used for planning when no model window is known.'
                          : 'PortOS reloads the Ollama daemon at this window before the run — the CLI/TUI talks to Ollama directly, so nothing else can raise it. Leave blank to keep Ollama\'s VRAM-based auto-pick; make sure the model still fits at the larger size.'}
                      </p>
                    </FormField>
                  )}
                </div>
              </div>

              {generationControls && (
                <div className="border-t border-port-border pt-4 mt-4">
                  <h4 className="text-sm font-medium text-gray-300 mb-3">Generation Defaults</h4>
                  <p className="text-xs text-gray-500 mb-3">
                    Applied to every run this provider starts — HTTP, CLI, and TUI alike. OpenCode wrappers
                    receive them as its <code className="text-gray-400">agent.build</code> options; a task can
                    still override temperature and thinking for one run. Every field left blank is simply not
                    sent, so the backend keeps its own default.
                  </p>
                  <div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
                    {generationControls.temperature && (
                      <FormField label="Temperature">
                        <input
                          type="number"
                          min={PROVIDER_FIELD_RANGES.temperature.min}
                          max={PROVIDER_FIELD_RANGES.temperature.max}
                          step="0.1"
                          value={formData.temperature}
                          onChange={(e) => setFormData(prev => ({ ...prev, temperature: e.target.value }))}
                          placeholder="Backend default"
                          className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                        />
                        <p className="text-xs text-gray-500 mt-1">Local Ollama agent runs fall back to 0.6 when this is blank.</p>
                      </FormField>
                    )}
                    {generationControls.topP && (
                      <FormField label="Top-P">
                        <input
                          type="number"
                          min={PROVIDER_FIELD_RANGES.topP.min}
                          max={PROVIDER_FIELD_RANGES.topP.max}
                          step="0.05"
                          value={formData.topP}
                          onChange={(e) => setFormData(prev => ({ ...prev, topP: e.target.value }))}
                          placeholder="Backend default"
                          className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                        />
                        <p className="text-xs text-gray-500 mt-1">Nucleus sampling. Leave blank to send no top_p at all.</p>
                      </FormField>
                    )}
                    {generationControls.thinking && (
                      /* Tri-state rather than a checkbox: a checkbox cannot say
                         "leave the model's own reasoning mode alone", so it
                         forced a pin onto every provider the moment anyone
                         pressed Save. */
                      <FormField label="Thinking mode">
                        <select
                          value={formData.thinking}
                          onChange={(e) => setFormData(prev => ({ ...prev, thinking: e.target.value }))}
                          className="w-full px-3 py-2 bg-port-bg border border-port-border rounded-lg text-white focus:border-port-accent focus:outline-hidden"
                        >
                          <option value="">Model default</option>
                          <option value="true">Enabled</option>
                          <option value="false">Disabled</option>
                        </select>
                        <p className="text-xs text-gray-500 mt-1">
                          Ollama receives its native <code className="text-gray-400">think</code> flag; llama.cpp and
                          MTPLX get <code className="text-gray-400">enable_thinking</code> through the chat template;
                          a Claude harness on Ollama gets <code className="text-gray-400">MAX_THINKING_TOKENS</code>.
                          Models without a reasoning mode ignore it.
                        </p>
                      </FormField>
                    )}
                  </div>
                </div>
              )}
            </div>
          )}

          {activeTab === 'environment' && (
            <div className="space-y-4">
              {/* Consumed only when spawning CLI/TUI child processes; API runs
                  never read them (auth is the API Key field on the Connection
                  tab). For API type the add-row is hidden so a key can't be
                  "set" here by mistake, but existing entries stay
                  editable/removable. */}
              <div>
                {formData.type === 'api' && Object.entries(formData.envVars).length > 0 && (
                  <p className="text-xs text-port-warning mb-2">
                    API providers ignore environment variables — these entries have no effect.
                    Put the key in the API Key field on the Connection tab.
                  </p>
                )}
                {Object.entries(formData.envVars).length > 0 && (
                  <div className="space-y-2 mb-3">
                    {Object.entries(formData.envVars).map(([key, value]) => {
                      const isSecret = formData.secretEnvVars.includes(key);
                      return (
                        <div key={key} className="flex items-center gap-2">
                          <code className="text-xs text-gray-300 bg-port-bg px-2 py-1.5 rounded border border-port-border shrink-0">{key}</code>
                          <input
                            type={isSecret ? 'password' : 'text'}
                            aria-label={`${key} value`}
                            value={value}
                            onChange={(e) => setFormData(prev => ({
                              ...prev,
                              envVars: { ...prev.envVars, [key]: e.target.value }
                            }))}
                            className="flex-1 min-w-0 px-2 py-1.5 bg-port-bg border border-port-border rounded text-white text-sm focus:border-port-accent focus:outline-hidden"
                          />
                          <button
                            type="button"
                            onClick={() => setFormData(prev => ({
                              ...prev,
                              secretEnvVars: isSecret
                                ? prev.secretEnvVars.filter(k => k !== key)
                                : [...prev.secretEnvVars, key]
                            }))}
                            className={`px-2 py-1.5 text-xs rounded transition-colors shrink-0 ${
                              isSecret
                                ? 'text-port-warning bg-port-warning/20 hover:bg-port-warning/30'
                                : 'text-gray-400 hover:bg-port-border/50'
                            }`}
                            title={isSecret ? 'Secret (click to unmask)' : 'Not secret (click to mask)'}
                          >
                            {isSecret ? '🔒' : '🔓'}
                          </button>
                          <button
                            type="button"
                            onClick={() => setFormData(prev => {
                              const { [key]: _, ...rest } = prev.envVars;
                              return {
                                ...prev,
                                envVars: rest,
                                secretEnvVars: prev.secretEnvVars.filter(k => k !== key)
                              };
                            })}
                            className="px-2 py-1.5 text-xs text-port-error hover:bg-port-error/20 rounded transition-colors shrink-0"
                          >
                            Remove
                          </button>
                        </div>
                      );
                    })}
                  </div>
                )}
                {formData.type !== 'api' && (
                <div className="flex gap-2">
                  <input
                    type="text"
                    value={newEnvKey}
                    onChange={(e) => setNewEnvKey(e.target.value.toUpperCase())}
                    placeholder="KEY"
                    aria-label="New environment variable name"
                    className="w-1/3 px-2 py-1.5 bg-port-bg border border-port-border rounded text-white text-sm focus:border-port-accent focus:outline-hidden font-mono"
                  />
                  <input
                    type={newEnvSecret ? 'password' : 'text'}
                    value={newEnvValue}
                    onChange={(e) => setNewEnvValue(e.target.value)}
                    placeholder="value"
                    aria-label="New environment variable value"
                    className="flex-1 px-2 py-1.5 bg-port-bg border border-port-border rounded text-white text-sm focus:border-port-accent focus:outline-hidden"
                  />
                  <label className="flex items-center gap-1 text-xs text-gray-400 shrink-0 cursor-pointer" title="Mark as secret (value will be masked on provider list)">
                    <input
                      type="checkbox"
                      checked={newEnvSecret}
                      onChange={(e) => setNewEnvSecret(e.target.checked)}
                      className="w-3.5 h-3.5 rounded border-port-border bg-port-bg"
                    />
                    Secret
                  </label>
                  <button
                    type="button"
                    onClick={() => {
                      if (newEnvKey.trim()) {
                        setFormData(prev => ({
                          ...prev,
                          envVars: { ...prev.envVars, [newEnvKey.trim()]: newEnvValue },
                          secretEnvVars: newEnvSecret
                            ? [...prev.secretEnvVars, newEnvKey.trim()]
                            : prev.secretEnvVars
                        }));
                        setNewEnvKey('');
                        setNewEnvValue('');
                        setNewEnvSecret(false);
                      }
                    }}
                    disabled={!newEnvKey.trim()}
                    className="px-3 py-1.5 text-sm bg-port-border hover:bg-port-border/80 text-white rounded transition-colors disabled:opacity-50 shrink-0"
                  >
                    Add
                  </button>
                </div>
                )}
                <p className="text-xs text-gray-500 mt-2">
                  {formData.type === 'api'
                    ? 'Not used by API providers — auth goes in the API Key field on the Connection tab. Env vars only apply to CLI/TUI process providers.'
                    : 'Environment variables passed to the CLI process (e.g., CLAUDE_CODE_USE_BEDROCK=1, AWS_PROFILE).'}
                </p>
              </div>
            </div>
          )}

          <div className="flex justify-end gap-3 pt-4">
            <button
              type="button"
              onClick={onClose}
              className="px-4 py-2 text-gray-400 hover:text-white"
            >
              Cancel
            </button>
            <button
              type="submit"
              className="px-6 py-2 bg-port-accent hover:bg-port-accent/80 text-white rounded-lg transition-colors"
            >
              {provider ? 'Save' : 'Create'}
            </button>
          </div>
        </form>
    </Drawer>
  );
}