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 { getCustomModelOptionsByInstance } from "../../modelSelection"; import { applyProviderInstanceSettings, deriveProviderInstanceEntries, resolveDefaultProviderModelSelection, sortProviderInstanceEntries, } from "../../providerInstances"; import { 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"; /** * Rows for the settings a project may override. The same rows edit * environment defaults at an environment scope and project overrides at a * project or checkout scope; the scoped hooks route the write. */ 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 representative = target ? environments.find((environment) => environment.environmentId === target.environmentId) : undefined; const providers = representative?.serverConfig?.providers ?? EMPTY_SERVER_PROVIDERS; const selection = resolveDefaultProviderModelSelection(providers, settings.defaultModelSelection); const entries = sortProviderInstanceEntries( applyProviderInstanceSettings(deriveProviderInstanceEntries(providers), settings), ); const modelOptions = getCustomModelOptionsByInstance( settings, providers, selection?.instanceId, selection?.model, ); const activeEntry = entries.find((entry) => entry.instanceId === selection?.instanceId); const mixedModel = useScopedSettingsMixed(["defaultModelSelection"]); const mixedPermissions = useScopedSettingsMixed(["defaultRuntimeMode"]); const PermissionIcon = runtimeModeConfig[settings.defaultRuntimeMode].icon; const mixedWorkspace = useScopedSettingsMixed(["defaultThreadEnvMode"]); const mixedSubmodules = useScopedSettingsMixed(["worktreeSubmodules"]); const mixedBrowser = useScopedSettingsMixed(["enableAgentBrowserAccess"]); 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; // File-backed keys show their effective value; the target already carries // the checkout's t3.json, and a null file here only fills the built-in. // The reset arrow beside the title clears the tier (SettingsRow handles a // project override, the environment value is cleared here), so the picker // has no "inherit" item. 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 config = environment?.serverConfig; if (!config) continue; const entry = applyProviderInstanceSettings( deriveProviderInstanceEntries(config.providers), candidate.settings, ).find((option) => option.instanceId === instanceId); const options = getCustomModelOptionsByInstance( { ...settings, ...candidate.settings }, config.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 ? ( {}} modelOptions={selection.options ?? []} allowPromptInjectedEffort={false} planModeEnabled={settings.planModeEnabled} triggerClassName={SETTINGS_PICKER_TRIGGER_CLASSNAME} onModelOptionsChange={(options) => setModel(createModelSelection(selection.instanceId, selection.model, options)) } /> ) : null}
) : ( No providers available ) } /> ); const workspaceRow = ( updateSettings({ defaultThreadEnvMode: null })} /> ) : null } control={ } /> ); return ( {category === "project" ? ( <> {modelRow} {workspaceRow} ) : category === "general" ? ( <> {modelRow} updateSettings({ defaultRuntimeMode: DEFAULT_SERVER_SETTINGS.defaultRuntimeMode, }) } /> ) : null } control={ } /> {workspaceRow} updateSettings({ worktreeSubmodules: null })} /> ) : null } control={ } /> ) : category === "source-control" ? ( <> updateSettings({ defaultAutoPull: false })} /> ) : null } control={ updateSettings({ defaultAutoPull: enabled })} /> } /> updateSettings({ removeAgentCreditsOnMerge: false })} /> ) : null } control={ updateSettings({ removeAgentCreditsOnMerge: enabled }) } /> } /> updateSettings({ pullRequestMergeMethod: null })} /> ) : null } control={ } /> ) : ( <> updateSettings({ enableAgentBrowserAccess: DEFAULT_SERVER_SETTINGS.enableAgentBrowserAccess, }) } /> ) : null } control={ updateSettings({ enableAgentBrowserAccess: enabled })} /> } /> )} ); }