Files
t3code/apps/web/src/components/settings/ProviderSettingsPanel.tsx
T
2026-10-10 04:10:38 +00:00

1320 lines
48 KiB
TypeScript

import { SettingsGroup } from "./SettingsGroup";
import { RefreshIcon } from "~/components/ui/refresh-icon";
import {
AuthSettingsWriteScope,
AuthProvidersManageScope,
AuthOrchestrationReadScope,
} from "@t3tools/contracts";
import { useEnvironmentScope, readEnvironmentScope } from "../../state/session";
import { useAtomValue } from "@effect/atom-react";
import { connectionStatusTitle } from "@t3tools/client-runtime/connection";
import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";
import {
isAtomCommandInterrupted,
squashAtomCommandFailure,
} from "@t3tools/client-runtime/state/runtime";
import {
defaultInstanceIdForDriver,
type EnvironmentId,
type AcpRegistryUrlAuthAction,
PROVIDER_DISPLAY_NAMES,
ProviderDriverKind,
type ProviderInstanceConfig,
type ProviderInstanceId,
resolveEnvironmentMachineKind,
resolveProviderInstanceEnabled,
} from "@t3tools/contracts";
import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
import {
getBackgroundActivityPresetSettings,
resolveServerBackgroundActivitySettings,
} from "@t3tools/shared/backgroundActivitySettings";
import * as Arr from "effect/Array";
import * as Duration from "effect/Duration";
import * as Equal from "effect/Equal";
import * as Result from "effect/Result";
import { PlusIcon } from "lucide-react";
import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
import { isDesktopLocalConnectionTarget } from "../../connection/desktopLocal";
import {
useEnvironmentSettings,
usePersistEnvironmentProviderInstanceMutation,
useUpdateClientSettings,
useUpdateEnvironmentSettings,
} from "../../hooks/useSettings";
import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
import { cn } from "../../lib/utils";
import { resolveAppModelSelectionState } from "../../modelSelection";
import {
useEnvironments,
usePrimaryEnvironmentId,
type EnvironmentPresentation,
} from "../../state/environments";
import { EMPTY_SERVER_PROVIDERS, serverEnvironment } from "../../state/server";
import { useEnvironmentSessionState } from "../../state/session";
import { useProjects } from "../../state/entities";
import { useAtomCommand } from "../../state/use-atom-command";
import { getRelativeTimeState } from "../../timestampFormat";
import {
ConnectionStatusDot,
connectionPhaseDotClassName,
connectionPhasePingClassName,
} from "../ConnectionStatusDot";
import {
isProviderSettingsUpdateCandidate,
isProviderUpdateActive,
type ProviderSettingsUpdateCandidate,
} from "../ProviderUpdateLaunchNotification.logic";
import { ProviderUpdatesAction } from "../ProviderUpdatesAction";
import { Button } from "../ui/button";
import {
Empty,
EmptyContent,
EmptyDescription,
EmptyHeader,
EmptyMedia,
EmptyTitle,
} from "../ui/empty";
import {
NumberField,
NumberFieldDecrement,
NumberFieldGroup,
NumberFieldIncrement,
NumberFieldInput,
} from "../ui/number-field";
import { ScrollArea } from "../ui/scroll-area";
import { Toggle, ToggleGroup } from "../ui/toggle-group";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import { stackedThreadToast, toastManager } from "../ui/toast";
import { AddProviderInstanceDialog } from "./AddProviderInstanceDialog";
import { ExpandableText } from "./ExpandableText";
import { ProviderInstanceCard } from "./ProviderInstanceCard";
import { UsageProviderSettings } from "./UsageProviderSettings";
import { ProviderSetupSection, readAntigravityAuthMethod } from "./ProviderSetupSection";
import { ProviderAuthenticationSection } from "./ProviderAuthenticationSection";
import { CodexSetupSection, CodexManagedRuntimeFields } from "./CodexSetupSection";
import { readCodexSetupMode } from "./CodexSetupSection.logic";
import { providerClients } from "./providerDriverMeta";
import { searchableSetting } from "./settingsSearch";
import {
backgroundActivityOverrideSettings,
buildProviderInstanceUpdatePatch,
durationToSeconds,
normalizeIntervalSeconds,
PROVIDER_HEALTH_INTERVAL_STEP_SECONDS,
} from "./SettingsPanels.logic";
import {
PolicyTooltip,
SettingResetButton,
SettingsPageContainer,
SettingsRow,
SettingsSection,
useRelativeTimeTick,
useSettingsSearchTargetId,
} from "./settingsLayout";
import {
buildProviderEnvironmentOptions,
classifyProviderEnvironmentAccess,
isProviderSettingsEnvironmentAvailable,
type ProviderEnvironmentAccess,
type ProviderOperateAccess,
resolveRemoteOperateAccess,
resolveSelectedProviderEnvironmentId,
} from "./ProviderSettingsPanel.logic";
function withoutProviderInstanceKey<V>(
record: Readonly<Record<ProviderInstanceId, V>> | undefined,
key: ProviderInstanceId,
): Record<ProviderInstanceId, V> {
const next = { ...record } as Record<ProviderInstanceId, V>;
delete next[key];
return next;
}
function withoutProviderInstanceFavorites(
favorites: ReadonlyArray<{ readonly provider: ProviderInstanceId; readonly model: string }>,
instanceId: ProviderInstanceId,
) {
return favorites.filter((favorite) => favorite.provider !== instanceId);
}
function providerConfigString(config: unknown, key: string): string | null {
if (config === null || typeof config !== "object") return null;
const value = (config as Record<string, unknown>)[key];
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
}
const PROVIDER_SETTINGS = providerClients.definitions.map((definition) => ({
provider: definition.driverKind,
hasDefaultInstance: definition.hasDefaultInstance !== false,
}));
function configuredBinaryPath(config: unknown): string {
if (config === null || typeof config !== "object" || !("binaryPath" in config)) return "";
return typeof config.binaryPath === "string" ? config.binaryPath.trim() : "";
}
function ProviderLastChecked({ lastCheckedAt }: { lastCheckedAt: string | null }) {
useRelativeTimeTick();
const lastCheckedRelative = getRelativeTimeState(lastCheckedAt);
if (lastCheckedRelative.status === "missing") {
return null;
}
if (lastCheckedRelative.status === "invalid") {
return <span>Checked unavailable</span>;
}
return (
<span>
{lastCheckedRelative.suffix ? (
<>
Checked <span className="font-mono tabular-nums">{lastCheckedRelative.value}</span>{" "}
{lastCheckedRelative.suffix}
</>
) : (
<>Checked {lastCheckedRelative.value}</>
)}
</span>
);
}
function providerEnvironmentDetail(environment: EnvironmentPresentation): string {
if (environment.entry.target._tag === "PrimaryConnectionTarget") return "Primary device";
if (environment.relayManaged) return "T3 Connect";
if (environment.entry.target._tag === "SshConnectionTarget") return "SSH";
if (isDesktopLocalConnectionTarget(environment.entry.target)) return "Local device";
return environment.displayUrl ?? "Remote device";
}
// Shared by the editor grid and the placeholder states so switching devices
// never changes the card's footprint.
const providerCardHeightClassName =
"@min-[48rem]/providers:h-[min(44rem,calc(100dvh-11rem))] @min-[48rem]/providers:min-h-[32rem]";
/**
* Same chrome as the provider editor (section heading, floating device tabs,
* tall card) for states that cannot render provider settings yet.
*/
function ProviderSettingsPlaceholder({
deviceTabs,
icon,
title,
description,
children,
}: {
readonly deviceTabs?: ReactNode;
readonly icon: ReactNode;
readonly title: string;
readonly description: string;
readonly children?: ReactNode;
}) {
return (
<SettingsSection {...searchableSetting("providers")} variant="plain">
{deviceTabs ? (
<div className="flex min-h-11 min-w-0 items-center px-3 sm:px-4">{deviceTabs}</div>
) : null}
<SettingsGroup
divided={false}
className={cn(
providerCardHeightClassName,
"scrollbar-gutter-both flex overflow-x-hidden overflow-y-auto",
)}
>
<Empty>
<EmptyMedia variant="icon">{icon}</EmptyMedia>
<EmptyHeader>
<EmptyTitle>{title}</EmptyTitle>
<EmptyDescription>{description}</EmptyDescription>
</EmptyHeader>
{children ? <EmptyContent className="max-w-xl">{children}</EmptyContent> : null}
</Empty>
</SettingsGroup>
</SettingsSection>
);
}
function EnvironmentUnavailablePlaceholder({
environment,
access,
deviceTabs,
}: {
readonly environment: EnvironmentPresentation;
readonly access: Exclude<ProviderEnvironmentAccess, { kind: "editable" | "read-only" }>;
readonly deviceTabs?: ReactNode;
}) {
const isLoading = access.kind === "loading";
const title = isLoading
? "Loading provider settings"
: access.kind === "error"
? "Could not connect to this device"
: "Provider settings are unavailable";
// Keep the description to a short status; the raw failure can be a
// multi-paragraph CLI dump, so it goes below, clamped and expandable.
const description = isLoading
? access.reason === "permissions"
? "Checking what this session is allowed to change."
: `Waiting for ${environment.label}'s configuration.`
: connectionStatusTitle(environment.connection);
const error = isLoading ? null : environment.connection.error;
// No spinner: this state can persist indefinitely for a wedged device, and a
// continuously repainting animation would run the whole time.
return (
<ProviderSettingsPlaceholder
deviceTabs={deviceTabs}
icon={
<EnvironmentMachineIcon kind={resolveEnvironmentMachineKind(environment.serverConfig)} />
}
title={title}
description={description}
>
{error ? (
<ExpandableText
key={environment.environmentId}
text={error}
className="w-full text-left font-mono text-xs leading-relaxed text-muted-foreground"
/>
) : null}
</ProviderSettingsPlaceholder>
);
}
interface ProviderSettingsTarget {
readonly environmentId?: EnvironmentId;
readonly instanceId?: ProviderInstanceId;
readonly scoped?: boolean;
readonly environmentIds?: readonly EnvironmentId[];
}
export function ProviderSettingsPanel(target: ProviderSettingsTarget) {
return (
<SettingsPageContainer className="@container/providers gap-8">
<ProviderSettingsPanelContent
key={`${target.environmentId ?? ""}:${target.instanceId ?? ""}`}
{...target}
/>
</SettingsPageContainer>
);
}
function ProviderSettingsPanelContent(target: ProviderSettingsTarget) {
const { environments, isReady } = useEnvironments();
const primaryEnvironmentId = usePrimaryEnvironmentId();
const searchTargetId = useSettingsSearchTargetId();
const options = useMemo(
() =>
buildProviderEnvironmentOptions(environments, primaryEnvironmentId, target.environmentIds),
[environments, primaryEnvironmentId, target.environmentIds],
);
// Raw user intent; the effective selection is re-derived every render so a
// device that drops out of the catalog falls back without erasing the pick —
// if it reappears (e.g. after a reconnect) the selection is restored.
const [selectedEnvironmentId, setSelectedEnvironmentId] = useState<EnvironmentId | null>(
target.environmentId ?? primaryEnvironmentId,
);
const targetEnvironmentMissing =
target.environmentId !== undefined &&
selectedEnvironmentId === target.environmentId &&
!options.some((environment) => environment.environmentId === target.environmentId);
const effectiveEnvironmentId =
target.scoped || targetEnvironmentMissing
? target.environmentId
: resolveSelectedProviderEnvironmentId(options, selectedEnvironmentId, primaryEnvironmentId);
const selectedEnvironment =
options.find((environment) => environment.environmentId === effectiveEnvironmentId) ?? null;
const selectedEnvironmentCanRenderSettings =
selectedEnvironment !== null &&
isProviderSettingsEnvironmentAvailable({
connectionPhase: selectedEnvironment.connection.phase,
hasServerConfig: selectedEnvironment.serverConfig !== null,
});
const searchableEnvironmentId = options.find((environment) =>
isProviderSettingsEnvironmentAvailable({
connectionPhase: environment.connection.phase,
hasServerConfig: environment.serverConfig !== null,
}),
)?.environmentId;
const searchableCursorEnvironmentId = options.find(
(environment) =>
environment.serverConfig?.environment.platform.os === "darwin" &&
isProviderSettingsEnvironmentAvailable({
connectionPhase: environment.connection.phase,
hasServerConfig: true,
}),
)?.environmentId;
useEffect(() => {
if (
!target.scoped &&
searchTargetId === searchableSetting("cursor-keychain-usage").id &&
(!selectedEnvironmentCanRenderSettings ||
selectedEnvironment?.serverConfig?.environment.platform.os !== "darwin") &&
searchableCursorEnvironmentId !== undefined
) {
setSelectedEnvironmentId(searchableCursorEnvironmentId);
return;
}
if (
!target.scoped &&
(searchTargetId === searchableSetting("provider-health-check-interval").id ||
searchTargetId === searchableSetting("usage-providers").id) &&
!selectedEnvironmentCanRenderSettings &&
searchableEnvironmentId !== undefined
) {
setSelectedEnvironmentId(searchableEnvironmentId);
}
}, [
searchTargetId,
searchableCursorEnvironmentId,
searchableEnvironmentId,
selectedEnvironment,
selectedEnvironmentCanRenderSettings,
target.scoped,
]);
const onlyPrimaryDevice =
options.length === 1 && options[0]?.entry.target._tag === "PrimaryConnectionTarget";
const deviceTabs =
!target.scoped && !onlyPrimaryDevice && options.length > 0 ? (
<ScrollArea radius="none" hideScrollbars scrollFade className="h-11 min-w-0 flex-1">
<ToggleGroup
aria-label="Devices"
variant="segmented"
className="my-2"
value={effectiveEnvironmentId ? [effectiveEnvironmentId] : []}
onValueChange={(next) => {
const environment = options.find((option) => option.environmentId === next[0]);
if (environment) setSelectedEnvironmentId(environment.environmentId);
}}
>
{options.map((environment) => {
const machine = resolveEnvironmentMachineKind(environment.serverConfig);
const detail = providerEnvironmentDetail(environment);
const statusText = connectionStatusTitle(environment.connection);
return (
<Tooltip key={environment.environmentId}>
<TooltipTrigger
render={
<Toggle value={environment.environmentId}>
<EnvironmentMachineIcon
kind={machine}
className="size-3.5 shrink-0"
aria-hidden
/>
<span className="max-w-40 truncate">{environment.label}</span>
{environment.connection.phase !== "connected" ? (
<ConnectionStatusDot
dotClassName={connectionPhaseDotClassName(environment.connection.phase)}
pingClassName={connectionPhasePingClassName(environment.connection.phase)}
/>
) : null}
<span className="sr-only">
{detail}, {statusText}
</span>
</Toggle>
}
/>
<TooltipPopup side="top">
{detail} · {statusText}
</TooltipPopup>
</Tooltip>
);
})}
</ToggleGroup>
</ScrollArea>
) : null;
return (
<>
{targetEnvironmentMissing ? (
<ProviderSettingsPlaceholder
deviceTabs={deviceTabs}
icon={<EnvironmentMachineIcon kind={resolveEnvironmentMachineKind(null)} />}
title="Device unavailable"
description="Reconnect this device to set up its provider, or select another device."
/>
) : null}
{options.length === 0 && !targetEnvironmentMissing ? (
<ProviderSettingsPlaceholder
icon={<EnvironmentMachineIcon kind={resolveEnvironmentMachineKind(null)} />}
title={isReady ? "No connected devices" : "Loading devices"}
description={
isReady
? "Connect an execution environment before configuring providers."
: "Reading connected execution environments."
}
/>
) : null}
{selectedEnvironment ? (
<SelectedEnvironmentProviderSettings
key={selectedEnvironment.environmentId}
environment={selectedEnvironment}
deviceTabs={deviceTabs}
targetInstanceId={
target.environmentId === undefined ||
selectedEnvironment.environmentId === target.environmentId
? target.instanceId
: undefined
}
/>
) : null}
</>
);
}
function SelectedEnvironmentProviderSettings({
environment,
deviceTabs,
targetInstanceId,
}: {
readonly environment: EnvironmentPresentation;
readonly deviceTabs?: ReactNode;
readonly targetInstanceId?: ProviderInstanceId | undefined;
}) {
return (
<RemoteSessionGatedProviderSettings
environment={environment}
deviceTabs={deviceTabs}
targetInstanceId={targetInstanceId}
/>
);
}
function RemoteSessionGatedProviderSettings({
environment,
deviceTabs,
targetInstanceId,
}: {
readonly environment: EnvironmentPresentation;
readonly deviceTabs?: ReactNode;
readonly targetInstanceId?: ProviderInstanceId | undefined;
}) {
const sessionState = useEnvironmentSessionState(environment.environmentId);
const operateAccess = resolveRemoteOperateAccess({
session: sessionState.data,
isPending: sessionState.isPending,
hasError: sessionState.hasError,
});
return (
<AccessGatedProviderSettings
environment={environment}
operateAccess={operateAccess}
deviceTabs={deviceTabs}
targetInstanceId={targetInstanceId}
/>
);
}
function AccessGatedProviderSettings({
environment,
operateAccess,
deviceTabs,
targetInstanceId,
}: {
readonly environment: EnvironmentPresentation;
readonly operateAccess: ProviderOperateAccess;
readonly deviceTabs?: ReactNode;
readonly targetInstanceId?: ProviderInstanceId | undefined;
}) {
const access = classifyProviderEnvironmentAccess({
connectionPhase: environment.connection.phase,
hasServerConfig: environment.serverConfig !== null,
operateAccess,
});
if (access.kind !== "editable" && access.kind !== "read-only") {
return (
<EnvironmentUnavailablePlaceholder
environment={environment}
access={access}
deviceTabs={deviceTabs}
/>
);
}
return (
<EnvironmentProviderSettings
environmentId={environment.environmentId}
environmentLabel={environment.label}
readOnly={access.kind === "read-only"}
deviceTabs={deviceTabs}
targetInstanceId={targetInstanceId}
/>
);
}
export function EnvironmentProviderSettings({
environmentId,
environmentLabel,
readOnly = false,
deviceTabs,
targetInstanceId,
}: {
readonly environmentId: EnvironmentId;
readonly environmentLabel: string;
readonly deviceTabs?: ReactNode;
readonly targetInstanceId?: ProviderInstanceId | undefined;
/**
* Grey out and freeze every write control when this session's credential
* lacks `providers:manage` on the environment. Selecting providers
* still works so the real configuration stays readable; switches, forms,
* are inert so no write is offered and then rejected.
*/
readonly readOnly?: boolean;
}) {
const settings = useEnvironmentSettings(environmentId);
const canWriteSettings = useEnvironmentScope(environmentId, AuthSettingsWriteScope);
const canRefreshProviders = useEnvironmentScope(environmentId, AuthOrchestrationReadScope);
const updateSettings = useUpdateEnvironmentSettings(environmentId);
const persistProviderInstance = usePersistEnvironmentProviderInstanceMutation(environmentId);
const updateClientSettings = useUpdateClientSettings();
const serverProviders =
useAtomValue(serverEnvironment.providersValueAtom(environmentId)) ?? EMPTY_SERVER_PROVIDERS;
const projects = useProjects().filter((project) => project.environmentId === environmentId);
const refreshServerProviders = useAtomCommand(serverEnvironment.refreshProviders, {
reportFailure: false,
});
const updateProvider = useAtomCommand(serverEnvironment.updateProvider, {
reportFailure: false,
});
const uninstallAcpRegistryManagedBinary = useAtomCommand(
serverEnvironment.uninstallAcpRegistryManagedBinary,
{ reportFailure: false },
);
const acceptAcpRegistryUrlAuth = useAtomCommand(serverEnvironment.acceptAcpRegistryUrlAuth, {
reportFailure: false,
});
const [isRefreshingProviders, setIsRefreshingProviders] = useState(false);
const [isAddInstanceDialogOpen, setIsAddInstanceDialogOpen] = useState(false);
const [selectedInstanceId, setSelectedInstanceId] = useState<ProviderInstanceId | null>(
targetInstanceId ?? null,
);
const [updatingProviderInstanceIds, setUpdatingProviderInstanceIds] = useState<
ReadonlySet<ProviderInstanceId>
>(() => new Set());
const refreshingRef = useRef(false);
const updatingInstanceIdsRef = useRef<Set<ProviderInstanceId>>(new Set());
const acceptUrlAuthentication = useCallback(
(instanceId: ProviderInstanceId, action: AcpRegistryUrlAuthAction) => {
void acceptAcpRegistryUrlAuth({
environmentId,
input: { instanceId, elicitationId: action.elicitationId },
}).then((result) => {
if (result._tag === "Success" && !result.value.accepted) {
toastManager.add({
type: "warning",
title: "Authentication request expired",
description: "Refresh the provider and start the authentication flow again.",
});
return;
}
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
toastManager.add({
type: "error",
title: "Could not continue authentication",
description:
error instanceof Error ? error.message : "The authentication request expired.",
});
}
});
},
[acceptAcpRegistryUrlAuth, environmentId],
);
const providerUpdateCandidateByInstanceId = useMemo(
() =>
new Map(
serverProviders
.filter(isProviderSettingsUpdateCandidate)
.map((candidate) => [candidate.instanceId, candidate]),
),
[serverProviders],
);
const visibleProviderSettings = PROVIDER_SETTINGS.filter(
(providerSettings) =>
providerSettings.provider !== "cursor" ||
serverProviders.some(
(provider) =>
provider.instanceId === defaultInstanceIdForDriver(ProviderDriverKind.make("cursor")),
),
);
const textGenerationModelSelection = resolveAppModelSelectionState(settings, serverProviders);
const textGenInstanceId = textGenerationModelSelection.instanceId;
const resolvedBackgroundActivity = resolveServerBackgroundActivitySettings(settings);
const providerHealthPreset = getBackgroundActivityPresetSettings(
resolvedBackgroundActivity.profile,
).providerHealthRefreshInterval;
const providerHealthRefreshIntervalSeconds = durationToSeconds(
resolvedBackgroundActivity.providerHealthRefreshInterval,
);
const defaultProviderHealthRefreshIntervalSeconds = durationToSeconds(providerHealthPreset);
const lastCheckedAt =
serverProviders.length > 0
? serverProviders.reduce(
(latest, provider) => (provider.checkedAt > latest ? provider.checkedAt : latest),
serverProviders[0]!.checkedAt,
)
: null;
const refreshProviders = useCallback(() => {
if (refreshingRef.current || !readEnvironmentScope(environmentId, AuthOrchestrationReadScope))
return;
refreshingRef.current = true;
setIsRefreshingProviders(true);
void (async () => {
const result = await refreshServerProviders({
environmentId,
input: { refreshModels: true },
});
refreshingRef.current = false;
setIsRefreshingProviders(false);
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
console.warn("Failed to refresh providers", {
operation: "refresh-providers",
environmentId,
...safeErrorLogAttributes(squashAtomCommandFailure(result)),
});
}
})();
}, [environmentId, refreshServerProviders]);
const runProviderUpdate = useCallback(
async (
candidate: Pick<ProviderSettingsUpdateCandidate, "driver" | "instanceId">,
targetVersion?: string,
) => {
if (!readEnvironmentScope(environmentId, AuthProvidersManageScope)) return;
// Ref-based re-entry guard, mirroring refreshProviders: a state updater
// may run after this function returns, so it cannot gate the dispatch.
if (updatingInstanceIdsRef.current.has(candidate.instanceId)) {
return;
}
updatingInstanceIdsRef.current.add(candidate.instanceId);
setUpdatingProviderInstanceIds((previous) => new Set(previous).add(candidate.instanceId));
const result = await updateProvider({
environmentId,
input: {
provider: candidate.driver,
instanceId: candidate.instanceId,
...(targetVersion ? { targetVersion } : {}),
},
});
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
toastManager.add(
stackedThreadToast({
type: "error",
title: `Could not update ${PROVIDER_DISPLAY_NAMES[candidate.driver] ?? candidate.driver}`,
description:
error instanceof Error
? error.message
: "The provider update command could not be started.",
}),
);
}
updatingInstanceIdsRef.current.delete(candidate.instanceId);
setUpdatingProviderInstanceIds((previous) => {
if (!previous.has(candidate.instanceId)) {
return previous;
}
const next = new Set(previous);
next.delete(candidate.instanceId);
return next;
});
},
[environmentId, updateProvider],
);
interface InstanceRow {
readonly instanceId: ProviderInstanceId;
readonly instance: ProviderInstanceConfig;
readonly driver: ProviderDriverKind;
readonly isDefault: boolean;
readonly isDirty?: boolean;
}
const instancesByDriver = new Map<
ProviderDriverKind,
Array<[ProviderInstanceId, ProviderInstanceConfig]>
>();
for (const [rawId, instance] of Object.entries(settings.providerInstances ?? {})) {
const driver = instance.driver;
const list = instancesByDriver.get(driver) ?? [];
list.push([rawId as ProviderInstanceId, instance]);
instancesByDriver.set(driver, list);
}
const defaultSlotIdsBySource = new Set<string>(
visibleProviderSettings.map((providerSettings) =>
String(defaultInstanceIdForDriver(providerSettings.provider)),
),
);
const rows: InstanceRow[] = [];
const visibleDriverKinds = new Set<ProviderDriverKind>(
visibleProviderSettings.map((providerSettings) => providerSettings.provider),
);
for (const providerSettings of visibleProviderSettings) {
const driver = providerSettings.provider;
const defaultInstanceId = defaultInstanceIdForDriver(driver);
const explicitInstance = settings.providerInstances?.[defaultInstanceId];
// An unconfigured default slot runs with the driver's default config.
const effectiveInstance: ProviderInstanceConfig = explicitInstance ?? { driver };
const isDirty = explicitInstance !== undefined;
// Drivers without a default instance list only their configured instances.
const hasDefaultSlot = providerSettings.hasDefaultInstance || explicitInstance !== undefined;
if (
hasDefaultSlot &&
(driver === "codex" ||
driver === "claudeAgent" ||
isDirty ||
resolveProviderInstanceEnabled(effectiveInstance) ||
defaultInstanceId === targetInstanceId)
) {
rows.push({
instanceId: defaultInstanceId,
instance: effectiveInstance,
driver,
isDefault: true,
isDirty,
});
}
for (const [id, instance] of instancesByDriver.get(providerSettings.provider) ?? []) {
if (id === defaultInstanceId) continue;
rows.push({ instanceId: id, instance, driver: instance.driver, isDefault: false });
}
}
for (const [driver, list] of instancesByDriver) {
if (visibleDriverKinds.has(driver)) continue;
for (const [id, instance] of list) {
rows.push({
instanceId: id,
instance,
driver: instance.driver,
isDefault: defaultSlotIdsBySource.has(String(id)),
});
}
}
const targetInstanceMissing =
targetInstanceId !== undefined &&
selectedInstanceId === targetInstanceId &&
!rows.some((row) => row.instanceId === targetInstanceId);
const selectedRow =
rows.find((row) => row.instanceId === selectedInstanceId) ??
(targetInstanceMissing ? null : (rows[0] ?? null));
const updateProviderInstance = async (
row: InstanceRow,
next: ProviderInstanceConfig,
options?: {
readonly textGenerationModelSelection?: Parameters<
typeof buildProviderInstanceUpdatePatch
>[0]["textGenerationModelSelection"];
},
) => {
const { providerInstances: _providerInstances, ...patch } = buildProviderInstanceUpdatePatch({
settings,
instanceId: row.instanceId,
instance: next,
textGenerationModelSelection: options?.textGenerationModelSelection,
});
const result = await persistProviderInstance(
{ operation: "upsert", instanceId: row.instanceId, instance: next },
patch,
);
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
toastManager.add({
type: "error",
title: "Could not update provider instance",
description: error instanceof Error ? error.message : "The settings update failed.",
});
}
};
const deleteProviderInstance = async (row: InstanceRow) => {
const updateResult = await persistProviderInstance({
operation: "remove",
instanceId: row.instanceId,
});
if (updateResult._tag === "Failure") {
const error = squashAtomCommandFailure(updateResult);
toastManager.add({
type: "error",
title: "Could not delete provider instance",
description: error instanceof Error ? error.message : "The settings update failed.",
});
return;
}
if (row.driver !== ProviderDriverKind.make("acpRegistry")) return;
if (providerConfigString(row.instance.config, "source") === "local") return;
const agentId = providerConfigString(row.instance.config, "agentId");
if (agentId === null) return;
// The server decides from its latest settings whether this was the last
// instance using the managed agent. A client-side snapshot check can race
// two removals and make both callers skip cleanup.
const uninstallResult = await uninstallAcpRegistryManagedBinary({
environmentId,
input: { agentId },
});
if (uninstallResult._tag === "Failure" && !isAtomCommandInterrupted(uninstallResult)) {
const error = squashAtomCommandFailure(uninstallResult);
toastManager.add({
type: "warning",
title: "Provider deleted, but managed files remain",
description: error instanceof Error ? error.message : "Managed binary cleanup failed.",
});
}
};
const updateProviderModelPreferences = (
instanceId: ProviderInstanceId,
next: {
readonly hiddenModels: ReadonlyArray<string>;
readonly modelOrder: ReadonlyArray<string>;
},
) => {
const hiddenModels = [...new Set(next.hiddenModels.filter((slug) => slug.trim().length > 0))];
const modelOrder = [...new Set(next.modelOrder.filter((slug) => slug.trim().length > 0))];
const rest = withoutProviderInstanceKey(settings.providerModelPreferences, instanceId);
updateClientSettings({
providerModelPreferences:
hiddenModels.length === 0 && modelOrder.length === 0
? rest
: {
...rest,
[instanceId]: {
hiddenModels,
modelOrder,
},
},
});
};
const updateProviderFavoriteModels = (
instanceId: ProviderInstanceId,
nextFavoriteModels: ReadonlyArray<string>,
) => {
const favoriteModels = [
...new Set(
Arr.filterMap(nextFavoriteModels, (slug) => {
const trimmedSlug = slug.trim();
return trimmedSlug.length > 0 ? Result.succeed(trimmedSlug) : Result.failVoid;
}),
),
];
updateClientSettings({
favorites: [
...withoutProviderInstanceFavorites(settings.favorites ?? [], instanceId),
...favoriteModels.map((model) => ({ provider: instanceId, model })),
],
});
};
const resetDefaultInstance = async (driverKind: ProviderDriverKind) => {
const result = await persistProviderInstance({
operation: "remove",
instanceId: defaultInstanceIdForDriver(driverKind),
});
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
toastManager.add({
type: "error",
title: "Could not reset provider instance",
description: error instanceof Error ? error.message : "The settings update failed.",
});
}
};
const renderProviderInstance = (row: InstanceRow, mode: "list" | "editor") => {
const driverOption = providerClients.get(row.driver);
const liveProvider = serverProviders.find(
(candidate) => candidate.instanceId === row.instanceId,
);
const updateCandidate = providerUpdateCandidateByInstanceId.get(row.instanceId);
const isInstanceUpdateRunning =
updatingProviderInstanceIds.has(row.instanceId) ||
(liveProvider !== undefined && isProviderUpdateActive(liveProvider));
const showInlineUpdateButton = updateCandidate !== undefined;
const canRunInlineUpdate = updateCandidate !== undefined && !isInstanceUpdateRunning;
const modelPreferences = settings.providerModelPreferences?.[row.instanceId] ?? {
hiddenModels: [],
modelOrder: [],
};
const favoriteModels = Arr.filterMap(settings.favorites ?? [], (favorite) =>
favorite.provider === row.instanceId ? Result.succeed(favorite.model) : Result.failVoid,
);
const resetLabel = driverOption?.label ?? String(row.driver);
return (
<ProviderInstanceCard
key={row.instanceId}
environmentId={environmentId}
acpProjects={projects}
onAcceptUrlAuth={
readOnly ? undefined : (action) => acceptUrlAuthentication(row.instanceId, action)
}
instanceId={row.instanceId}
instance={row.instance}
driverOption={driverOption}
liveProvider={liveProvider}
mode={mode}
selected={mode === "list" && selectedRow?.instanceId === row.instanceId}
onSelect={mode === "list" ? () => setSelectedInstanceId(row.instanceId) : undefined}
readOnly={readOnly}
runtime={
mode === "editor" &&
row.driver === "codex" &&
readCodexSetupMode(row.instance.config) === "managed" ? (
<CodexManagedRuntimeFields
environmentId={environmentId}
instanceId={row.instanceId}
provider={liveProvider}
/>
) : undefined
}
canWriteSettings={canWriteSettings}
setup={
mode === "editor" && row.driver === "antigravity" ? (
<ProviderSetupSection
environmentId={environmentId}
environmentLabel={environmentLabel}
instanceId={row.instanceId}
provider={liveProvider}
binaryPath={configuredBinaryPath(row.instance.config)}
authMethod={readAntigravityAuthMethod(row.instance.config)}
enabled={resolveProviderInstanceEnabled(row.instance)}
readOnly={readOnly}
onEnable={() => updateProviderInstance(row, { ...row.instance, enabled: true })}
/>
) : mode === "editor" &&
row.driver === "codex" &&
readCodexSetupMode(row.instance.config) === "managed" ? (
<CodexSetupSection
environmentId={environmentId}
instanceId={row.instanceId}
provider={liveProvider}
mode={readCodexSetupMode(row.instance.config)}
enabled={resolveProviderInstanceEnabled(row.instance)}
readOnly={readOnly}
onModeChange={(setupMode) =>
updateProviderInstance(row, {
...row.instance,
enabled: true,
config: {
...(row.instance.config !== null && typeof row.instance.config === "object"
? row.instance.config
: {}),
enabled: true,
setupMode,
},
})
}
/>
) : mode === "editor" &&
!readOnly &&
liveProvider &&
(liveProvider.setup?.canAuthenticate ||
(liveProvider.driver === "acpRegistry" && liveProvider.installed)) ? (
<ProviderAuthenticationSection
key={`${environmentId}:${row.instanceId}`}
environmentId={environmentId}
environmentLabel={environmentLabel}
instanceId={row.instanceId}
provider={liveProvider}
readOnly={readOnly}
/>
) : mode === "editor" &&
!readOnly &&
row.driver === "cursor" &&
liveProvider?.setup?.canAuthenticate === false ? (
<SettingsRow
title="Cursor account"
description="Using CURSOR_API_KEY. Remove it from this provider's environment to use browser sign-in."
/>
) : null
}
onUpdate={(next) => {
const wasEnabled = resolveProviderInstanceEnabled(row.instance);
const isDisabling = next.enabled === false && wasEnabled;
const shouldClearTextGen =
isDisabling &&
textGenInstanceId === row.instanceId &&
readEnvironmentScope(environmentId, AuthSettingsWriteScope);
updateProviderInstance(
row,
next,
shouldClearTextGen
? {
textGenerationModelSelection:
DEFAULT_UNIFIED_SETTINGS.textGenerationModelSelection,
}
: undefined,
);
}}
onDelete={
mode === "editor" && !row.isDefault ? () => deleteProviderInstance(row) : undefined
}
headerAction={
mode === "editor" && row.isDefault && row.isDirty ? (
<SettingResetButton
label={`${resetLabel} provider settings`}
onClick={() => resetDefaultInstance(row.driver)}
/>
) : null
}
hiddenModels={modelPreferences.hiddenModels}
favoriteModels={favoriteModels}
modelOrder={modelPreferences.modelOrder}
onHiddenModelsChange={(hiddenModels) =>
updateProviderModelPreferences(row.instanceId, {
...modelPreferences,
hiddenModels,
})
}
onFavoriteModelsChange={(next) => updateProviderFavoriteModels(row.instanceId, next)}
onModelOrderChange={(modelOrder) =>
updateProviderModelPreferences(row.instanceId, {
...modelPreferences,
modelOrder,
})
}
onInstallRecommended={
!readOnly &&
liveProvider?.compatibilityAdvisory?.message &&
liveProvider.compatibilityAdvisory.recommendedVersion &&
liveProvider.versionAdvisory?.canInstallVersion
? () => {
void runProviderUpdate(
liveProvider,
liveProvider.compatibilityAdvisory?.recommendedVersion ?? undefined,
);
}
: undefined
}
onRunUpdate={
!readOnly && showInlineUpdateButton && updateCandidate
? () => {
if (canRunInlineUpdate) void runProviderUpdate(updateCandidate);
}
: undefined
}
isUpdating={isInstanceUpdateRunning}
/>
);
};
return (
<>
<SettingsSection
{...searchableSetting("providers")}
variant="plain"
titleAction={
!readOnly ? (
<Tooltip>
<TooltipTrigger
render={
<Button
size="icon-xs"
variant="ghost-muted"
onClick={() => setIsAddInstanceDialogOpen(true)}
aria-label="Add provider"
>
<PlusIcon />
</Button>
}
/>
<TooltipPopup side="top">Add provider</TooltipPopup>
</Tooltip>
) : null
}
headerAction={
<div className="flex min-w-0 items-center gap-2">
<ProviderUpdatesAction />
<Tooltip>
<TooltipTrigger
render={
<Button
size="xs"
variant="ghost-muted"
disabled={isRefreshingProviders || !canRefreshProviders}
aria-busy={isRefreshingProviders}
onClick={() => void refreshProviders()}
>
<RefreshIcon refreshing={isRefreshingProviders} />
<span className="sr-only">Refresh provider status</span>
<span className="hidden min-w-0 truncate sm:inline">
{isRefreshingProviders ? (
"Refreshing providers"
) : (
<ProviderLastChecked lastCheckedAt={lastCheckedAt} />
)}
</span>
</Button>
}
/>
<TooltipPopup side="top">Refresh provider status</TooltipPopup>
</Tooltip>
</div>
}
>
{deviceTabs ? (
<div className="flex min-h-11 min-w-0 items-center gap-2 px-3 sm:px-4">{deviceTabs}</div>
) : null}
{readOnly ? (
<SettingsGroup divided={false} className="overflow-hidden">
<SettingsRow
title="Limited permissions"
description={`This session can view ${environmentLabel}'s providers but can't change their settings.`}
/>
</SettingsGroup>
) : null}
<SettingsGroup
divided={false}
className={cn(
providerCardHeightClassName,
"overflow-hidden @min-[48rem]/providers:grid @min-[48rem]/providers:grid-cols-[17rem_minmax(0,1fr)]",
)}
>
<div className="border-b border-border/60 bg-muted/10 @min-[48rem]/providers:flex @min-[48rem]/providers:min-h-0 @min-[48rem]/providers:flex-col @min-[48rem]/providers:border-r @min-[48rem]/providers:border-b-0">
<ScrollArea
scrollFade
chainVerticalScroll
className="@min-[48rem]/providers:min-h-0 @min-[48rem]/providers:flex-1"
>
<div className="divide-y divide-border/50">
{rows.map((row) => renderProviderInstance(row, "list"))}
{!readOnly ? (
<button
type="button"
className="flex w-full cursor-pointer items-center gap-3 px-3 py-3 text-left text-sm text-muted-foreground transition-colors outline-none hover:bg-muted/25 hover:text-foreground focus-visible:bg-muted/25 focus-visible:text-foreground sm:px-4"
onClick={() => setIsAddInstanceDialogOpen(true)}
>
<PlusIcon className="size-4 shrink-0" />
Add provider
</button>
) : null}
</div>
</ScrollArea>
</div>
<div className="min-w-0 @min-[48rem]/providers:min-h-0">
{selectedRow ? (
<ScrollArea scrollFade chainVerticalScroll className="@min-[48rem]/providers:h-full">
<div className="space-y-6 p-4">{renderProviderInstance(selectedRow, "editor")}</div>
</ScrollArea>
) : (
<div className="p-6 text-sm text-muted-foreground">
{targetInstanceMissing
? "This provider instance is no longer available on this device."
: "No providers configured."}
</div>
)}
</div>
</SettingsGroup>
</SettingsSection>
<UsageProviderSettings
key={environmentId}
environmentId={environmentId}
environmentLabel={environmentLabel}
sources={settings.usageLimitSources}
cursorKeychainUsageEnabled={settings.cursorKeychainUsageEnabled}
readOnly={readOnly}
/>
<SettingsSection title="Advanced">
<SettingsRow
id={searchableSetting("provider-health-check-interval").id}
title={
<span className="inline-flex items-center gap-1.5">
{searchableSetting("provider-health-check-interval").title}
<PolicyTooltip>
This interval is configured here, then the shared Background activity policy decides
whether provider probes may run when the timer fires. Custom intervals appear as
Advanced in General settings.
</PolicyTooltip>
</span>
}
description="Refresh provider status, versions, and models in the background. Set to 0 to disable."
resetAction={
providerHealthRefreshIntervalSeconds !== defaultProviderHealthRefreshIntervalSeconds ? (
<span
inert={!canWriteSettings}
className={!canWriteSettings ? "opacity-50" : undefined}
>
<SettingResetButton
label="provider health check interval"
onClick={() =>
updateSettings(
backgroundActivityOverrideSettings(
settings.backgroundActivity,
resolvedBackgroundActivity,
{ providerHealthRefreshInterval: undefined },
),
)
}
/>
</span>
) : null
}
control={
<div
inert={!canWriteSettings}
aria-disabled={!canWriteSettings || undefined}
className={cn(
"flex shrink-0 items-center gap-2",
!canWriteSettings && "opacity-50 select-none",
)}
>
<NumberField
value={providerHealthRefreshIntervalSeconds}
min={0}
step={PROVIDER_HEALTH_INTERVAL_STEP_SECONDS}
size="sm"
className="w-32"
onValueChange={(value) =>
updateSettings(
backgroundActivityOverrideSettings(
settings.backgroundActivity,
resolvedBackgroundActivity,
{
providerHealthRefreshInterval: Duration.seconds(
normalizeIntervalSeconds(value),
),
},
),
)
}
>
<NumberFieldGroup>
<NumberFieldDecrement aria-label="Decrease provider health check interval" />
<NumberFieldInput aria-label="Provider health check interval in seconds" />
<NumberFieldIncrement aria-label="Increase provider health check interval" />
</NumberFieldGroup>
</NumberField>
<span className="text-xs text-muted-foreground">seconds</span>
</div>
}
/>
</SettingsSection>
{isAddInstanceDialogOpen && !readOnly ? (
<AddProviderInstanceDialog
open
environmentId={environmentId}
environmentLabel={environmentLabel}
onOpenChange={setIsAddInstanceDialogOpen}
onCreated={setSelectedInstanceId}
/>
) : null}
</>
);
}