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