import { DEFAULT_SERVER_SETTINGS, type ModelSelection, type ProviderInstanceId, type WorktreeSubmodules, } from "@t3tools/contracts"; import { createModelSelection } from "@t3tools/shared/model"; import { resolveProjectSettings } from "@t3tools/shared/projectSettings"; import { useNavigate } from "@tanstack/react-router"; import { mergeEnvironmentSettings } from "../../hooks/useSettings"; import { getCustomModelOptionsByInstance, resolveEffectiveDefaultModelSelection, } from "../../modelSelection"; import { applyProviderInstanceSettings, deriveProviderInstanceEntries, resolveDefaultProviderModelSelection, sortProviderInstanceEntries, } from "../../providerInstances"; import { useEnvironmentProviders, useEnvironments } from "../../state/environments"; import { EMPTY_SERVER_PROVIDERS } from "../../state/server"; import { resolveEnvModeLabel, WORKTREE_SUBMODULES_LABELS } from "../BranchToolbar.logic"; import { ProviderModelPicker } from "../chat/ProviderModelPicker"; import { runtimeModeConfig, runtimeModeOptions } from "../chat/runtimeModeConfig"; import { PULL_REQUEST_MERGE_METHOD_LABELS } from "../pullRequest/pullRequestDetail.logic"; import { TraitsPicker } from "../chat/TraitsPicker"; import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select"; import { toastManager } from "../ui/toast"; import { Switch } from "../ui/switch"; import type { ProjectSettingsCategory } from "./ProjectSettingsPanel"; import { searchableSetting } from "./settingsSearch"; import { useSettingsScope } from "./SettingsScopeContext"; import { SETTINGS_PICKER_TRIGGER_CLASSNAME, SettingResetButton, SettingsRow, SettingsSection, } from "./settingsLayout"; import { useScopedSettings, useScopedSettingsMixed, useScopedSettingSource, useUpdateScopedSettings, } from "./useScopedSettings"; const WORKTREE_SUBMODULES_OPTIONS = ["recursive", "top-level", "none"] as const; function isWorktreeSubmodules(value: string | null): value is WorktreeSubmodules { return value !== null && (WORKTREE_SUBMODULES_OPTIONS as readonly string[]).includes(value); } export function ProjectDefaultsSettings({ category }: { category: ProjectSettingsCategory }) { const { scope, target, targets, connectedEnvironments } = useSettingsScope(); const settings = useScopedSettings(); const updateSettings = useUpdateScopedSettings(); const navigate = useNavigate(); const { environments } = useEnvironments(); const providersByEnvironment = useEnvironmentProviders(); const representative = target ? environments.find((environment) => environment.environmentId === target.environmentId) : undefined; const providers = (representative?.serverConfig ? providersByEnvironment.get(representative.environmentId) : undefined) ?? EMPTY_SERVER_PROVIDERS; const storedSelection = resolveDefaultProviderModelSelection( providers, settings.defaultModelSelection, ); const selection = storedSelection ? resolveEffectiveDefaultModelSelection(settings, providers, storedSelection) : null; const entries = sortProviderInstanceEntries( applyProviderInstanceSettings( deriveProviderInstanceEntries(providers), settings, target?.projectId ?? null, ), ); const modelOptions = getCustomModelOptionsByInstance( settings, providers, selection?.instanceId, selection?.model, ); const activeEntry = entries.find((entry) => entry.instanceId === selection?.instanceId); const mixedModel = useScopedSettingsMixed(["defaultModelSelection"]); const mixedFallback = useScopedSettingsMixed(["fallbackModelSelection"]); const fallbackSelection = settings.fallbackModelSelection ?? selection; const fallbackModelOptions = getCustomModelOptionsByInstance( settings, providers, fallbackSelection?.instanceId, fallbackSelection?.model, ); const mixedPermissions = useScopedSettingsMixed(["defaultRuntimeMode"]); const PermissionIcon = runtimeModeConfig[settings.defaultRuntimeMode].icon; const mixedWorkspace = useScopedSettingsMixed(["defaultThreadEnvMode"]); const mixedAutoPull = useScopedSettingsMixed(["defaultAutoPull"]); const mixedAgentCredits = useScopedSettingsMixed(["removeAgentCreditsOnMerge"]); const mixedMergeMethod = useScopedSettingsMixed(["pullRequestMergeMethod"]); const modelSource = useScopedSettingSource(["defaultModelSelection"]); const isProjectScope = scope.kind === "project" || scope.kind === "checkout"; const unavailable = connectedEnvironments.length === 0; const effective = target ? resolveProjectSettings(target.settings, null, null, null).settings : null; function modelDisabledReason(instanceId: ProviderInstanceId, model: string): string | null { const sourceEntry = entries.find((entry) => entry.instanceId === instanceId); for (const candidate of targets) { const environment = environments.find( (entry) => entry.environmentId === candidate.environmentId, ); const providers = providersByEnvironment.get(candidate.environmentId); if (!environment?.serverConfig || !providers) continue; const entry = applyProviderInstanceSettings( deriveProviderInstanceEntries(providers), candidate.settings, candidate.projectId, ).find((option) => option.instanceId === instanceId); const options = getCustomModelOptionsByInstance( mergeEnvironmentSettings(candidate.settings, settings), providers, ).get(instanceId); if ( !entry?.enabled || !entry.isAvailable || entry.driverKind !== sourceEntry?.driverKind || !options?.some((option) => option.slug === model && !option.isUnavailable) ) { return `This model is unavailable on ${environment?.label ?? "a selected environment"}. Select that environment to choose its model separately.`; } } return null; } const setModel = (value: ModelSelection | null) => { const reason = value ? modelDisabledReason(value.instanceId, value.model) : null; if (reason) { toastManager.add({ type: "error", title: "Default model not saved", description: reason }); return; } updateSettings({ defaultModelSelection: value }); }; const modelRow = ( setModel(null)} /> ) : null } control={ selection && activeEntry ? (
{ if (representative) void navigate({ to: "/settings/providers", search: { environmentId: representative.environmentId, instanceId }, }); }} onInstanceModelChange={(instanceId, model) => setModel(createModelSelection(instanceId, model)) } /> {!mixedModel ? ( setModel(createModelSelection(selection.instanceId, selection.model, options)) } /> ) : null}
) : ( No providers available ) } /> ); const workspaceRow = ( updateSettings({ defaultThreadEnvMode: null })} /> ) : null } control={ } /> ); const fallbackRow = !isProjectScope ? ( updateSettings({ fallbackModelSelection: null })} /> ) : null } control={ fallbackSelection ? ( { const reason = modelDisabledReason(instanceId, model); if (reason) { toastManager.add({ type: "error", title: "Fallback model not saved", description: reason, }); return; } updateSettings({ fallbackModelSelection: createModelSelection(instanceId, model) }); }} /> ) : ( No providers available ) } /> ) : null; return ( {category === "project" ? ( <> {modelRow} {fallbackRow} {workspaceRow} ) : category === "general" ? ( <> {modelRow} {fallbackRow} updateSettings({ defaultRuntimeMode: DEFAULT_SERVER_SETTINGS.defaultRuntimeMode, }) } /> ) : null } control={ } /> {workspaceRow} ) : ( <> updateSettings({ defaultAutoPull: false })} /> ) : null } control={ updateSettings({ defaultAutoPull: enabled })} /> } /> updateSettings({ removeAgentCreditsOnMerge: false })} /> ) : null } control={ updateSettings({ removeAgentCreditsOnMerge: enabled }) } /> } /> updateSettings({ pullRequestMergeMethod: null })} /> ) : null } control={ } /> )} ); } export function WorktreeSubmodulesRow() { const { scope, target, connectedEnvironments } = useSettingsScope(); const settings = useScopedSettings(); const updateSettings = useUpdateScopedSettings(); const mixedSubmodules = useScopedSettingsMixed(["worktreeSubmodules"]); const isProjectScope = scope.kind === "project" || scope.kind === "checkout"; const unavailable = connectedEnvironments.length === 0; const effective = target ? resolveProjectSettings(target.settings, null, null, null).settings : null; return ( updateSettings({ worktreeSubmodules: null })} /> ) : null } control={ } /> ); }