Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 type ModelSelection,
4 type ProviderInstanceId,
5 type WorktreeSubmodules,
6} from "@t3tools/contracts";
7import { createModelSelection } from "@t3tools/shared/model";
8import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
9import { useNavigate } from "@tanstack/react-router";
10
11import { mergeEnvironmentSettings } from "../../hooks/useSettings";
12import {
13 getCustomModelOptionsByInstance,
14 resolveEffectiveDefaultModelSelection,
15} from "../../modelSelection";
16import {
17 applyProviderInstanceSettings,
18 deriveProviderInstanceEntries,
19 resolveDefaultProviderModelSelection,
20 sortProviderInstanceEntries,
21} from "../../providerInstances";
22import { useEnvironmentProviders, useEnvironments } from "../../state/environments";
23import { EMPTY_SERVER_PROVIDERS } from "../../state/server";
24import { resolveEnvModeLabel, WORKTREE_SUBMODULES_LABELS } from "../BranchToolbar.logic";
25import { ProviderModelPicker } from "../chat/ProviderModelPicker";
26import { runtimeModeConfig, runtimeModeOptions } from "../chat/runtimeModeConfig";
27import { PULL_REQUEST_MERGE_METHOD_LABELS } from "../pullRequest/pullRequestDetail.logic";
28import { TraitsPicker } from "../chat/TraitsPicker";
29import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
30import { toastManager } from "../ui/toast";
31import { Switch } from "../ui/switch";
32import type { ProjectSettingsCategory } from "./ProjectSettingsPanel";
33import { searchableSetting } from "./settingsSearch";
34import { useSettingsScope } from "./SettingsScopeContext";
35import {
36 SETTINGS_PICKER_TRIGGER_CLASSNAME,
37 SettingResetButton,
38 SettingsRow,
39 SettingsSection,
40} from "./settingsLayout";
41import {
42 useScopedSettings,
43 useScopedSettingsMixed,
44 useScopedSettingSource,
45 useUpdateScopedSettings,
46} from "./useScopedSettings";
47
48const WORKTREE_SUBMODULES_OPTIONS = ["recursive", "top-level", "none"] as const;
49function isWorktreeSubmodules(value: string | null): value is WorktreeSubmodules {
50 return value !== null && (WORKTREE_SUBMODULES_OPTIONS as readonly string[]).includes(value);
51}
52
53export function ProjectDefaultsSettings({ category }: { category: ProjectSettingsCategory }) {
54 const { scope, target, targets, connectedEnvironments } = useSettingsScope();
55 const settings = useScopedSettings();
56 const updateSettings = useUpdateScopedSettings();
57 const navigate = useNavigate();
58 const { environments } = useEnvironments();
59 const providersByEnvironment = useEnvironmentProviders();
60 const representative = target
61 ? environments.find((environment) => environment.environmentId === target.environmentId)
62 : undefined;
63 const providers =
64 (representative?.serverConfig
65 ? providersByEnvironment.get(representative.environmentId)
66 : undefined) ?? EMPTY_SERVER_PROVIDERS;
67 const storedSelection = resolveDefaultProviderModelSelection(
68 providers,
69 settings.defaultModelSelection,
70 );
71 const selection = storedSelection
72 ? resolveEffectiveDefaultModelSelection(settings, providers, storedSelection)
73 : null;
74 const entries = sortProviderInstanceEntries(
75 applyProviderInstanceSettings(
76 deriveProviderInstanceEntries(providers),
77 settings,
78 target?.projectId ?? null,
79 ),
80 );
81 const modelOptions = getCustomModelOptionsByInstance(
82 settings,
83 providers,
84 selection?.instanceId,
85 selection?.model,
86 );
87 const activeEntry = entries.find((entry) => entry.instanceId === selection?.instanceId);
88 const mixedModel = useScopedSettingsMixed(["defaultModelSelection"]);
89 const mixedPermissions = useScopedSettingsMixed(["defaultRuntimeMode"]);
90 const PermissionIcon = runtimeModeConfig[settings.defaultRuntimeMode].icon;
91 const mixedWorkspace = useScopedSettingsMixed(["defaultThreadEnvMode"]);
92 const mixedSubmodules = useScopedSettingsMixed(["worktreeSubmodules"]);
93 const mixedAutoPull = useScopedSettingsMixed(["defaultAutoPull"]);
94 const mixedAgentCredits = useScopedSettingsMixed(["removeAgentCreditsOnMerge"]);
95 const mixedMergeMethod = useScopedSettingsMixed(["pullRequestMergeMethod"]);
96 const modelSource = useScopedSettingSource(["defaultModelSelection"]);
97 const isProjectScope = scope.kind === "project" || scope.kind === "checkout";
98 const unavailable = connectedEnvironments.length === 0;
99 const effective = target
100 ? resolveProjectSettings(target.settings, null, null, null).settings
101 : null;
102
103 function modelDisabledReason(instanceId: ProviderInstanceId, model: string): string | null {
104 const sourceEntry = entries.find((entry) => entry.instanceId === instanceId);
105 for (const candidate of targets) {
106 const environment = environments.find(
107 (entry) => entry.environmentId === candidate.environmentId,
108 );
109 const providers = providersByEnvironment.get(candidate.environmentId);
110 if (!environment?.serverConfig || !providers) continue;
111 const entry = applyProviderInstanceSettings(
112 deriveProviderInstanceEntries(providers),
113 candidate.settings,
114 candidate.projectId,
115 ).find((option) => option.instanceId === instanceId);
116 const options = getCustomModelOptionsByInstance(
117 mergeEnvironmentSettings(candidate.settings, settings),
118 providers,
119 ).get(instanceId);
120 if (
121 !entry?.enabled ||
122 !entry.isAvailable ||
123 entry.driverKind !== sourceEntry?.driverKind ||
124 !options?.some((option) => option.slug === model && !option.isUnavailable)
125 ) {
126 return `This model is unavailable on ${environment?.label ?? "a selected environment"}. Select that environment to choose its model separately.`;
127 }
128 }
129 return null;
130 }
131
132 const setModel = (value: ModelSelection | null) => {
133 const reason = value ? modelDisabledReason(value.instanceId, value.model) : null;
134 if (reason) {
135 toastManager.add({ type: "error", title: "Default model not saved", description: reason });
136 return;
137 }
138 updateSettings({ defaultModelSelection: value });
139 };
140
141 const modelRow = (
142 <SettingsRow
143 serverScoped
144 settingKeys={["defaultModelSelection"]}
145 mixed={mixedModel}
146 id="default-model"
147 title="Model"
148 description={
149 isProjectScope
150 ? "Model for new threads in this project."
151 : "Default model for new threads. Projects can override it."
152 }
153 status={
154 unavailable || mixedModel || modelSource === "project"
155 ? undefined
156 : settings.defaultModelSelection === null
157 ? "Automatic"
158 : undefined
159 }
160 resetAction={
161 settings.defaultModelSelection !== null ? (
162 <SettingResetButton label="default model" onClick={() => setModel(null)} />
163 ) : null
164 }
165 control={
166 selection && activeEntry ? (
167 <div className="flex min-w-0 flex-wrap items-center justify-end gap-1.5">
168 <ProviderModelPicker
169 environmentId={representative?.environmentId ?? null}
170 activeInstanceId={selection.instanceId}
171 model={selection.model}
172 lockedProvider={null}
173 instanceEntries={entries}
174 modelOptionsByInstance={modelOptions}
175 triggerClassName={SETTINGS_PICKER_TRIGGER_CLASSNAME}
176 {...(mixedModel ? { triggerLabel: "Mixed" } : {})}
177 getModelDisabledReason={modelDisabledReason}
178 onOpenProviderSetup={(instanceId) => {
179 if (representative)
180 void navigate({
181 to: "/settings/providers",
182 search: { environmentId: representative.environmentId, instanceId },
183 });
184 }}
185 onInstanceModelChange={(instanceId, model) =>
186 setModel(createModelSelection(instanceId, model))
187 }
188 />
189 {!mixedModel ? (
190 <TraitsPicker
191 provider={activeEntry.driverKind}
192 models={activeEntry.models}
193 model={selection.model}
194 prompt=""
195 onPromptChange={() => {}}
196 modelOptions={selection.options ?? []}
197 allowPromptInjectedEffort={false}
198 planModeEnabled={settings.planModeEnabled}
199 triggerClassName={SETTINGS_PICKER_TRIGGER_CLASSNAME}
200 onModelOptionsChange={(options) =>
201 setModel(createModelSelection(selection.instanceId, selection.model, options))
202 }
203 />
204 ) : null}
205 </div>
206 ) : (
207 <span className="text-sm text-muted-foreground">No providers available</span>
208 )
209 }
210 />
211 );
212 const workspaceRow = (
213 <SettingsRow
214 serverScoped
215 settingKeys={["defaultThreadEnvMode"]}
216 mixed={mixedWorkspace}
217 id={searchableSetting("new-threads").id}
218 title="Workspace"
219 description={
220 isProjectScope
221 ? "Where new threads in this project start."
222 : "Where new threads start. Projects and their t3.json can override it."
223 }
224 resetAction={
225 !isProjectScope && settings.defaultThreadEnvMode !== null ? (
226 <SettingResetButton
227 label="default workspace"
228 onClick={() => updateSettings({ defaultThreadEnvMode: null })}
229 />
230 ) : null
231 }
232 control={
233 <Select
234 value={mixedWorkspace ? null : (effective?.defaultThreadEnvMode ?? null)}
235 onValueChange={(value) => {
236 if (value === "local" || value === "worktree")
237 updateSettings({ defaultThreadEnvMode: value });
238 }}
239 >
240 <SelectTrigger size="sm" aria-label="Default workspace">
241 <SelectValue>
242 {(value: string | null) =>
243 value === "local" || value === "worktree"
244 ? resolveEnvModeLabel(value)
245 : unavailable
246 ? "Unavailable"
247 : "Mixed"
248 }
249 </SelectValue>
250 </SelectTrigger>
251 <SelectPopup align="end" alignItemWithTrigger={false}>
252 <SelectItem value="local">{resolveEnvModeLabel("local")}</SelectItem>
253 <SelectItem value="worktree">{resolveEnvModeLabel("worktree")}</SelectItem>
254 </SelectPopup>
255 </Select>
256 }
257 />
258 );
259
260 return (
261 <SettingsSection
262 id={category === "source-control" ? "source-control-defaults" : "project-defaults"}
263 title={category === "source-control" ? "Repositories" : "New threads"}
264 >
265 {category === "project" ? (
266 <>
267 {modelRow}
268 {workspaceRow}
269 </>
270 ) : category === "general" ? (
271 <>
272 {modelRow}
273 <SettingsRow
274 serverScoped
275 settingKeys={["defaultRuntimeMode"]}
276 mixed={mixedPermissions}
277 {...searchableSetting("default-permissions")}
278 description={
279 isProjectScope
280 ? "Permissions for new threads in this project."
281 : "Default permissions for new threads. Projects can override them."
282 }
283 resetAction={
284 settings.defaultRuntimeMode !== DEFAULT_SERVER_SETTINGS.defaultRuntimeMode ? (
285 <SettingResetButton
286 label="default permissions"
287 onClick={() =>
288 updateSettings({
289 defaultRuntimeMode: DEFAULT_SERVER_SETTINGS.defaultRuntimeMode,
290 })
291 }
292 />
293 ) : null
294 }
295 control={
296 <Select
297 value={mixedPermissions ? null : settings.defaultRuntimeMode}
298 onValueChange={(value) => {
299 if (value) updateSettings({ defaultRuntimeMode: value });
300 }}
301 >
302 <SelectTrigger size="sm" aria-label="Default permissions">
303 {!mixedPermissions && (
304 <PermissionIcon className="size-3.5 shrink-0 text-muted-foreground" />
305 )}
306 <SelectValue>
307 {mixedPermissions
308 ? "Mixed"
309 : runtimeModeConfig[settings.defaultRuntimeMode].label}
310 </SelectValue>
311 </SelectTrigger>
312 <SelectPopup align="end" alignItemWithTrigger={false}>
313 {runtimeModeOptions.map((mode) => {
314 const option = runtimeModeConfig[mode];
315 const Icon = option.icon;
316 return (
317 <SelectItem key={mode} value={mode} className="min-w-64">
318 <div className="grid gap-0.5">
319 <span className="inline-flex items-center gap-1.5 font-medium">
320 <Icon className="size-3.5 shrink-0 text-muted-foreground" />
321 {option.label}
322 </span>
323 <span className="text-xs leading-4 text-muted-foreground">
324 {option.description}
325 </span>
326 </div>
327 </SelectItem>
328 );
329 })}
330 </SelectPopup>
331 </Select>
332 }
333 />
334 {workspaceRow}
335 <SettingsRow
336 serverScoped
337 settingKeys={["worktreeSubmodules"]}
338 mixed={mixedSubmodules}
339 {...searchableSetting("worktree-submodules")}
340 description={
341 isProjectScope
342 ? "How new worktrees in this project populate git submodules."
343 : "How new worktrees populate git submodules. Projects and their t3.json can override it."
344 }
345 resetAction={
346 !isProjectScope && settings.worktreeSubmodules !== null ? (
347 <SettingResetButton
348 label="worktree submodules"
349 onClick={() => updateSettings({ worktreeSubmodules: null })}
350 />
351 ) : null
352 }
353 control={
354 <Select
355 value={mixedSubmodules ? null : (effective?.worktreeSubmodules ?? null)}
356 onValueChange={(value) => {
357 if (isWorktreeSubmodules(value)) updateSettings({ worktreeSubmodules: value });
358 }}
359 >
360 <SelectTrigger size="sm" aria-label="Worktree submodules">
361 <SelectValue>
362 {(value: string | null) =>
363 isWorktreeSubmodules(value)
364 ? WORKTREE_SUBMODULES_LABELS[value]
365 : unavailable
366 ? "Unavailable"
367 : "Mixed"
368 }
369 </SelectValue>
370 </SelectTrigger>
371 <SelectPopup align="end" alignItemWithTrigger={false}>
372 {WORKTREE_SUBMODULES_OPTIONS.map((option) => (
373 <SelectItem key={option} value={option}>
374 {WORKTREE_SUBMODULES_LABELS[option]}
375 </SelectItem>
376 ))}
377 </SelectPopup>
378 </Select>
379 }
380 />
381 </>
382 ) : (
383 <>
384 <SettingsRow
385 serverScoped
386 settingKeys={["defaultAutoPull"]}
387 mixed={mixedAutoPull}
388 id="automatic-pull"
389 title="Automatically pull"
390 description={
391 isProjectScope
392 ? "Keeps this project's default branch current when the checkout has no local changes or commits."
393 : "Keeps the default branch current when the checkout has no local changes or commits. Projects can override it."
394 }
395 resetAction={
396 settings.defaultAutoPull ? (
397 <SettingResetButton
398 label="default automatic pull"
399 tooltip="Reset automatic pull to off"
400 onClick={() => updateSettings({ defaultAutoPull: false })}
401 />
402 ) : null
403 }
404 control={
405 <Switch
406 aria-label="Default automatic pull"
407 mixed={mixedAutoPull}
408 checked={mixedAutoPull ? false : settings.defaultAutoPull}
409 onCheckedChange={(enabled) => updateSettings({ defaultAutoPull: enabled })}
410 />
411 }
412 />
413 <SettingsRow
414 serverScoped
415 settingKeys={["removeAgentCreditsOnMerge"]}
416 mixed={mixedAgentCredits}
417 {...searchableSetting("remove-agent-credits-on-merge")}
418 description="Remove recognized agent credit lines from GitHub merge and squash messages, keeping human co-authors. Includes auto-merge. Excludes merge queues, stack merges, and existing commits."
419 resetAction={
420 settings.removeAgentCreditsOnMerge ? (
421 <SettingResetButton
422 label="agent credit removal"
423 tooltip="Keep agent credits"
424 onClick={() => updateSettings({ removeAgentCreditsOnMerge: false })}
425 />
426 ) : null
427 }
428 control={
429 <Switch
430 aria-label="Remove agent credits when merging"
431 mixed={mixedAgentCredits}
432 checked={mixedAgentCredits ? false : settings.removeAgentCreditsOnMerge}
433 onCheckedChange={(enabled) =>
434 updateSettings({ removeAgentCreditsOnMerge: enabled })
435 }
436 />
437 }
438 />
439 <SettingsRow
440 serverScoped
441 settingKeys={["pullRequestMergeMethod"]}
442 mixed={mixedMergeMethod}
443 {...searchableSetting("pull-request-merge-method")}
444 description={
445 isProjectScope
446 ? "Pull requests in this project start with this method."
447 : "Pull requests start with this method. Last selected reuses whatever you chose most recently on this device."
448 }
449 resetAction={
450 settings.pullRequestMergeMethod !== null ? (
451 <SettingResetButton
452 label="default merge method"
453 tooltip="Reset to last selected"
454 onClick={() => updateSettings({ pullRequestMergeMethod: null })}
455 />
456 ) : null
457 }
458 control={
459 <Select
460 value={mixedMergeMethod ? null : (settings.pullRequestMergeMethod ?? "last")}
461 onValueChange={(value) => {
462 if (value === "last") updateSettings({ pullRequestMergeMethod: null });
463 else if (value === "merge" || value === "squash" || value === "rebase")
464 updateSettings({ pullRequestMergeMethod: value });
465 }}
466 >
467 <SelectTrigger size="sm" aria-label="Default pull request merge method">
468 <SelectValue>
469 {(value: string | null) =>
470 value === "merge" || value === "squash" || value === "rebase"
471 ? PULL_REQUEST_MERGE_METHOD_LABELS[value]
472 : value === "last"
473 ? "Last selected"
474 : "Mixed"
475 }
476 </SelectValue>
477 </SelectTrigger>
478 <SelectPopup align="end" alignItemWithTrigger={false}>
479 <SelectItem value="last">Last selected</SelectItem>
480 <SelectItem value="merge">{PULL_REQUEST_MERGE_METHOD_LABELS.merge}</SelectItem>
481 <SelectItem value="squash">{PULL_REQUEST_MERGE_METHOD_LABELS.squash}</SelectItem>
482 <SelectItem value="rebase">{PULL_REQUEST_MERGE_METHOD_LABELS.rebase}</SelectItem>
483 </SelectPopup>
484 </Select>
485 }
486 />
487 </>
488 )}
489 </SettingsSection>
490 );
491}