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 { getCustomModelOptionsByInstance } from "../../modelSelection";
13import {
14 applyProviderInstanceSettings,
15 deriveProviderInstanceEntries,
16 resolveDefaultProviderModelSelection,
17 sortProviderInstanceEntries,
18} from "../../providerInstances";
19import { useEnvironmentProviders, useEnvironments } from "../../state/environments";
20import { EMPTY_SERVER_PROVIDERS } from "../../state/server";
21import { resolveEnvModeLabel, WORKTREE_SUBMODULES_LABELS } from "../BranchToolbar.logic";
22import { ProviderModelPicker } from "../chat/ProviderModelPicker";
23import { runtimeModeConfig, runtimeModeOptions } from "../chat/runtimeModeConfig";
24import { PULL_REQUEST_MERGE_METHOD_LABELS } from "../pullRequest/pullRequestDetail.logic";
25import { TraitsPicker } from "../chat/TraitsPicker";
26import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
27import { toastManager } from "../ui/toast";
28import { Switch } from "../ui/switch";
29import type { ProjectSettingsCategory } from "./ProjectSettingsPanel";
30import { searchableSetting } from "./settingsSearch";
31import { useSettingsScope } from "./SettingsScopeContext";
32import {
33 SETTINGS_PICKER_TRIGGER_CLASSNAME,
34 SettingResetButton,
35 SettingsRow,
36 SettingsSection,
37} from "./settingsLayout";
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, targets, 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 selection = resolveDefaultProviderModelSelection(providers, settings.defaultModelSelection);
65 const entries = sortProviderInstanceEntries(
66 applyProviderInstanceSettings(
67 deriveProviderInstanceEntries(providers),
68 settings,
69 target?.projectId ?? null,
70 ),
71 );
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 mixedPermissions = useScopedSettingsMixed(["defaultRuntimeMode"]);
81 const PermissionIcon = runtimeModeConfig[settings.defaultRuntimeMode].icon;
82 const mixedWorkspace = useScopedSettingsMixed(["defaultThreadEnvMode"]);
83 const mixedSubmodules = useScopedSettingsMixed(["worktreeSubmodules"]);
84 const mixedAutoPull = useScopedSettingsMixed(["defaultAutoPull"]);
85 const mixedAgentCredits = useScopedSettingsMixed(["removeAgentCreditsOnMerge"]);
86 const mixedMergeMethod = useScopedSettingsMixed(["pullRequestMergeMethod"]);
87 const modelSource = useScopedSettingSource(["defaultModelSelection"]);
88 const isProjectScope = scope.kind === "project" || scope.kind === "checkout";
89 const unavailable = connectedEnvironments.length === 0;
90 const effective = target
91 ? resolveProjectSettings(target.settings, null, null, null).settings
92 : null;
93
94 function modelDisabledReason(instanceId: ProviderInstanceId, model: string): string | null {
95 const sourceEntry = entries.find((entry) => entry.instanceId === instanceId);
96 for (const candidate of targets) {
97 const environment = environments.find(
98 (entry) => entry.environmentId === candidate.environmentId,
99 );
100 const providers = providersByEnvironment.get(candidate.environmentId);
101 if (!environment?.serverConfig || !providers) continue;
102 const entry = applyProviderInstanceSettings(
103 deriveProviderInstanceEntries(providers),
104 candidate.settings,
105 candidate.projectId,
106 ).find((option) => option.instanceId === instanceId);
107 const options = getCustomModelOptionsByInstance(
108 mergeEnvironmentSettings(candidate.settings, settings),
109 providers,
110 ).get(instanceId);
111 if (
112 !entry?.enabled ||
113 !entry.isAvailable ||
114 entry.driverKind !== sourceEntry?.driverKind ||
115 !options?.some((option) => option.slug === model && !option.isUnavailable)
116 ) {
117 return `This model is unavailable on ${environment?.label ?? "a selected environment"}. Select that environment to choose its model separately.`;
118 }
119 }
120 return null;
121 }
122
123 const setModel = (value: ModelSelection | null) => {
124 const reason = value ? modelDisabledReason(value.instanceId, value.model) : null;
125 if (reason) {
126 toastManager.add({ type: "error", title: "Default model not saved", description: reason });
127 return;
128 }
129 updateSettings({ defaultModelSelection: value });
130 };
131
132 const modelRow = (
133 <SettingsRow
134 serverScoped
135 settingKeys={["defaultModelSelection"]}
136 mixed={mixedModel}
137 id="default-model"
138 title="Model"
139 description={
140 isProjectScope
141 ? "Model for new threads in this project."
142 : "Default model for new threads. Projects can override it."
143 }
144 status={
145 unavailable || mixedModel || modelSource === "project"
146 ? undefined
147 : settings.defaultModelSelection === null
148 ? "Automatic"
149 : undefined
150 }
151 resetAction={
152 settings.defaultModelSelection !== null ? (
153 <SettingResetButton label="default model" onClick={() => setModel(null)} />
154 ) : null
155 }
156 control={
157 selection && activeEntry ? (
158 <div className="flex min-w-0 flex-wrap items-center justify-end gap-1.5">
159 <ProviderModelPicker
160 environmentId={representative?.environmentId ?? null}
161 activeInstanceId={selection.instanceId}
162 model={selection.model}
163 lockedProvider={null}
164 instanceEntries={entries}
165 modelOptionsByInstance={modelOptions}
166 triggerClassName={SETTINGS_PICKER_TRIGGER_CLASSNAME}
167 {...(mixedModel ? { triggerLabel: "Mixed" } : {})}
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 ? (
181 <TraitsPicker
182 provider={activeEntry.driverKind}
183 models={activeEntry.models}
184 model={selection.model}
185 prompt=""
186 onPromptChange={() => {}}
187 modelOptions={selection.options ?? []}
188 allowPromptInjectedEffort={false}
189 planModeEnabled={settings.planModeEnabled}
190 triggerClassName={SETTINGS_PICKER_TRIGGER_CLASSNAME}
191 onModelOptionsChange={(options) =>
192 setModel(createModelSelection(selection.instanceId, selection.model, options))
193 }
194 />
195 ) : null}
196 </div>
197 ) : (
198 <span className="text-sm text-muted-foreground">No providers available</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 return (
252 <SettingsSection
253 id={category === "source-control" ? "source-control-defaults" : "project-defaults"}
254 title={category === "source-control" ? "Repositories" : "New threads"}
255 >
256 {category === "project" ? (
257 <>
258 {modelRow}
259 {workspaceRow}
260 </>
261 ) : category === "general" ? (
262 <>
263 {modelRow}
264 <SettingsRow
265 serverScoped
266 settingKeys={["defaultRuntimeMode"]}
267 mixed={mixedPermissions}
268 {...searchableSetting("default-permissions")}
269 description={
270 isProjectScope
271 ? "Permissions for new threads in this project."
272 : "Default permissions for new threads. Projects can override them."
273 }
274 resetAction={
275 settings.defaultRuntimeMode !== DEFAULT_SERVER_SETTINGS.defaultRuntimeMode ? (
276 <SettingResetButton
277 label="default permissions"
278 onClick={() =>
279 updateSettings({
280 defaultRuntimeMode: DEFAULT_SERVER_SETTINGS.defaultRuntimeMode,
281 })
282 }
283 />
284 ) : null
285 }
286 control={
287 <Select
288 value={mixedPermissions ? null : settings.defaultRuntimeMode}
289 onValueChange={(value) => {
290 if (value) updateSettings({ defaultRuntimeMode: value });
291 }}
292 >
293 <SelectTrigger size="sm" aria-label="Default permissions">
294 {!mixedPermissions && (
295 <PermissionIcon className="size-3.5 shrink-0 text-muted-foreground" />
296 )}
297 <SelectValue>
298 {mixedPermissions
299 ? "Mixed"
300 : runtimeModeConfig[settings.defaultRuntimeMode].label}
301 </SelectValue>
302 </SelectTrigger>
303 <SelectPopup align="end" alignItemWithTrigger={false}>
304 {runtimeModeOptions.map((mode) => {
305 const option = runtimeModeConfig[mode];
306 const Icon = option.icon;
307 return (
308 <SelectItem key={mode} value={mode} className="min-w-64">
309 <div className="grid gap-0.5">
310 <span className="inline-flex items-center gap-1.5 font-medium">
311 <Icon className="size-3.5 shrink-0 text-muted-foreground" />
312 {option.label}
313 </span>
314 <span className="text-xs leading-4 text-muted-foreground">
315 {option.description}
316 </span>
317 </div>
318 </SelectItem>
319 );
320 })}
321 </SelectPopup>
322 </Select>
323 }
324 />
325 {workspaceRow}
326 <SettingsRow
327 serverScoped
328 settingKeys={["worktreeSubmodules"]}
329 mixed={mixedSubmodules}
330 {...searchableSetting("worktree-submodules")}
331 description={
332 isProjectScope
333 ? "How new worktrees in this project populate git submodules."
334 : "How new worktrees populate git submodules. Projects and their t3.json can override it."
335 }
336 resetAction={
337 !isProjectScope && settings.worktreeSubmodules !== null ? (
338 <SettingResetButton
339 label="worktree submodules"
340 onClick={() => updateSettings({ worktreeSubmodules: null })}
341 />
342 ) : null
343 }
344 control={
345 <Select
346 value={mixedSubmodules ? null : (effective?.worktreeSubmodules ?? null)}
347 onValueChange={(value) => {
348 if (isWorktreeSubmodules(value)) updateSettings({ worktreeSubmodules: value });
349 }}
350 >
351 <SelectTrigger size="sm" aria-label="Worktree submodules">
352 <SelectValue>
353 {(value: string | null) =>
354 isWorktreeSubmodules(value)
355 ? WORKTREE_SUBMODULES_LABELS[value]
356 : unavailable
357 ? "Unavailable"
358 : "Mixed"
359 }
360 </SelectValue>
361 </SelectTrigger>
362 <SelectPopup align="end" alignItemWithTrigger={false}>
363 {WORKTREE_SUBMODULES_OPTIONS.map((option) => (
364 <SelectItem key={option} value={option}>
365 {WORKTREE_SUBMODULES_LABELS[option]}
366 </SelectItem>
367 ))}
368 </SelectPopup>
369 </Select>
370 }
371 />
372 </>
373 ) : (
374 <>
375 <SettingsRow
376 serverScoped
377 settingKeys={["defaultAutoPull"]}
378 mixed={mixedAutoPull}
379 id="automatic-pull"
380 title="Automatically pull"
381 description={
382 isProjectScope
383 ? "Keeps this project's default branch current when the checkout has no local changes or commits."
384 : "Keeps the default branch current when the checkout has no local changes or commits. Projects can override it."
385 }
386 resetAction={
387 settings.defaultAutoPull ? (
388 <SettingResetButton
389 label="default automatic pull"
390 tooltip="Reset automatic pull to off"
391 onClick={() => updateSettings({ defaultAutoPull: false })}
392 />
393 ) : null
394 }
395 control={
396 <Switch
397 aria-label="Default automatic pull"
398 mixed={mixedAutoPull}
399 checked={mixedAutoPull ? false : settings.defaultAutoPull}
400 onCheckedChange={(enabled) => updateSettings({ defaultAutoPull: enabled })}
401 />
402 }
403 />
404 <SettingsRow
405 serverScoped
406 settingKeys={["removeAgentCreditsOnMerge"]}
407 mixed={mixedAgentCredits}
408 {...searchableSetting("remove-agent-credits-on-merge")}
409 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."
410 resetAction={
411 settings.removeAgentCreditsOnMerge ? (
412 <SettingResetButton
413 label="agent credit removal"
414 tooltip="Keep agent credits"
415 onClick={() => updateSettings({ removeAgentCreditsOnMerge: false })}
416 />
417 ) : null
418 }
419 control={
420 <Switch
421 aria-label="Remove agent credits when merging"
422 mixed={mixedAgentCredits}
423 checked={mixedAgentCredits ? false : settings.removeAgentCreditsOnMerge}
424 onCheckedChange={(enabled) =>
425 updateSettings({ removeAgentCreditsOnMerge: enabled })
426 }
427 />
428 }
429 />
430 <SettingsRow
431 serverScoped
432 settingKeys={["pullRequestMergeMethod"]}
433 mixed={mixedMergeMethod}
434 {...searchableSetting("pull-request-merge-method")}
435 description={
436 isProjectScope
437 ? "Pull requests in this project start with this method."
438 : "Pull requests start with this method. Last selected reuses whatever you chose most recently on this device."
439 }
440 resetAction={
441 settings.pullRequestMergeMethod !== null ? (
442 <SettingResetButton
443 label="default merge method"
444 tooltip="Reset to last selected"
445 onClick={() => updateSettings({ pullRequestMergeMethod: null })}
446 />
447 ) : null
448 }
449 control={
450 <Select
451 value={mixedMergeMethod ? null : (settings.pullRequestMergeMethod ?? "last")}
452 onValueChange={(value) => {
453 if (value === "last") updateSettings({ pullRequestMergeMethod: null });
454 else if (value === "merge" || value === "squash" || value === "rebase")
455 updateSettings({ pullRequestMergeMethod: value });
456 }}
457 >
458 <SelectTrigger size="sm" aria-label="Default pull request merge method">
459 <SelectValue>
460 {(value: string | null) =>
461 value === "merge" || value === "squash" || value === "rebase"
462 ? PULL_REQUEST_MERGE_METHOD_LABELS[value]
463 : value === "last"
464 ? "Last selected"
465 : "Mixed"
466 }
467 </SelectValue>
468 </SelectTrigger>
469 <SelectPopup align="end" alignItemWithTrigger={false}>
470 <SelectItem value="last">Last selected</SelectItem>
471 <SelectItem value="merge">{PULL_REQUEST_MERGE_METHOD_LABELS.merge}</SelectItem>
472 <SelectItem value="squash">{PULL_REQUEST_MERGE_METHOD_LABELS.squash}</SelectItem>
473 <SelectItem value="rebase">{PULL_REQUEST_MERGE_METHOD_LABELS.rebase}</SelectItem>
474 </SelectPopup>
475 </Select>
476 }
477 />
478 </>
479 )}
480 </SettingsSection>
481 );
482}