import { DEFAULT_SERVER_SETTINGS, EnvironmentId, type ModelSelection, type ProviderInstanceId, } from "@t3tools/contracts"; import { createModelSelection } from "@t3tools/shared/model"; import { useNavigate } from "@tanstack/react-router"; import { useT3ProjectFileState } from "../../hooks/useT3ProjectFileScripts"; 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 } 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. */ 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 mixedBrowser = useScopedSettingsMixed(["enableAgentBrowserAccess"]); const mixedAutoPull = useScopedSettingsMixed(["defaultAutoPull"]); const mixedMergeMethod = useScopedSettingsMixed(["pullRequestMergeMethod"]); const modelSource = useScopedSettingSource(["defaultModelSelection"]); const workspaceSource = useScopedSettingSource(["defaultThreadEnvMode"]); const isProjectScope = scope.kind === "project" || scope.kind === "checkout"; const unavailable = connectedEnvironments.length === 0; // A checkout's t3.json wins over the environment default when the project // has no override of its own; show which one "inherit" resolves to. const checkout = scope.kind === "checkout" ? scope.checkout : null; // The query is disabled without a checkout, so any id satisfies the hook. const t3File = useT3ProjectFileState( checkout?.environmentId ?? EnvironmentId.make("none"), category === "general" && checkout ? checkout.workspaceRoot : null, ); const repositoryEnvMode = t3File.file?.defaultThreadEnvMode ?? null; const inheritedEnvModeLabel = workspaceSource === "project" ? null : repositoryEnvMode ? `${resolveEnvModeLabel(repositoryEnvMode)} (t3.json)` : 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 }); }; return ( {category === "general" ? ( <> 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} triggerVariant="outline" triggerClassName={SETTINGS_PICKER_TRIGGER_CLASSNAME} onModelOptionsChange={(options) => setModel( createModelSelection(selection.instanceId, selection.model, options), ) } /> ) : null}
) : ( No providers available ) } /> updateSettings({ defaultRuntimeMode: DEFAULT_SERVER_SETTINGS.defaultRuntimeMode, }) } /> ) : null } control={ } /> updateSettings({ defaultThreadEnvMode: DEFAULT_SERVER_SETTINGS.defaultThreadEnvMode, }) } /> ) : null } control={ } /> ) : category === "source-control" ? ( <> updateSettings({ defaultAutoPull: false })} /> ) : null } control={ updateSettings({ defaultAutoPull: enabled })} /> } /> updateSettings({ pullRequestMergeMethod: null })} /> ) : null } control={ } /> ) : ( <> updateSettings({ enableAgentBrowserAccess: DEFAULT_SERVER_SETTINGS.enableAgentBrowserAccess, }) } /> ) : null } control={ updateSettings({ enableAgentBrowserAccess: enabled })} /> } /> )}
); }