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