Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectDefaultsSettings.tsx

473 lines18.7 KB

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 applyProviderInstanceSettings,
16 deriveProviderInstanceEntries,
17 resolveDefaultProviderModelSelection,
18 sortProviderInstanceEntries,
19} from "../../providerInstances";
20import { useEnvironmentProviders, useEnvironments } from "../../state/environments";
21import { EMPTY_SERVER_PROVIDERS } from "../../state/server";
22import { resolveEnvModeLabel, WORKTREE_SUBMODULES_LABELS } from "../BranchToolbar.logic";
23import { ProviderModelPicker } from "../chat/ProviderModelPicker";
24import { runtimeModeConfig, runtimeModeOptions } from "../chat/runtimeModeConfig";
25import { PULL_REQUEST_MERGE_METHOD_LABELS } from "../pullRequest/pullRequestDetail.logic";
26import { TraitsPicker } from "../chat/TraitsPicker";
27import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
28import { toastManager } from "../ui/toast";
29import { Switch } from "../ui/switch";
30import type { ProjectSettingsCategory } from "./ProjectSettingsPanel";
31import { searchableSetting } from "./settingsSearch";
32import { useSettingsScope } from "./SettingsScopeContext";
33import {
34 SETTINGS_PICKER_TRIGGER_CLASSNAME,
35 SettingResetButton,
36 SettingsRow,
37 SettingsSection,
38} from "./settingsLayout";
39import { useScopedModelDisabledReason } from "./useScopedModelAvailability";
40import {
41 useScopedSettings,
42 useScopedSettingsMixed,
43 useScopedSettingSource,
44 useUpdateScopedSettings,
45} from "./useScopedSettings";
46​
47const WORKTREE_SUBMODULES_OPTIONS = ["recursive", "top-level", "none"] as const;
48function isWorktreeSubmodules(value: string | null): value is WorktreeSubmodules {
49 return value !== null && (WORKTREE_SUBMODULES_OPTIONS as readonly string[]).includes(value);
51​
52export function ProjectDefaultsSettings({ category }: { category: ProjectSettingsCategory }) {
53 const { scope, target, connectedEnvironments } = useSettingsScope();
54 const settings = useScopedSettings();
55 const updateSettings = useUpdateScopedSettings();
56 const navigate = useNavigate();
57 const { environments } = useEnvironments();
58 const providersByEnvironment = useEnvironmentProviders();
59 const representative = target
60 ? environments.find((environment) => environment.environmentId === target.environmentId)
61 : undefined;
62 const providers =
63 (representative?.serverConfig
64 ? providersByEnvironment.get(representative.environmentId)
65 : undefined) ?? EMPTY_SERVER_PROVIDERS;
66 const storedSelection = resolveDefaultProviderModelSelection(
67 providers,
68 settings.defaultModelSelection,
69 );
70 const selection = storedSelection
71 ? resolveEffectiveDefaultModelSelection(settings, providers, storedSelection)
72 : null;
73 const entries = sortProviderInstanceEntries(
74 applyProviderInstanceSettings(
75 deriveProviderInstanceEntries(providers),
76 settings,
77 target?.projectId ?? null,
78 ),
79 );
80 const modelOptions = getCustomModelOptionsByInstance(
81 settings,
82 providers,
83 selection?.instanceId,
84 selection?.model,
85 );
86 const activeEntry = entries.find((entry) => entry.instanceId === selection?.instanceId);
87 const mixedModel = useScopedSettingsMixed(["defaultModelSelection"]);
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 return (
228 <SettingsSection
229 id={category === "source-control" ? "source-control-defaults" : "project-defaults"}
230 title={category === "source-control" ? "Repositories" : "New threads"}
231 >
232 {category === "project" ? (
233 <>
234 {modelRow}
235 {workspaceRow}
236 </>
237 ) : category === "general" ? (
238 <>
239 {modelRow}
240 <SettingsRow
241 serverScoped
242 settingKeys={["defaultRuntimeMode"]}
243 mixed={mixedPermissions}
244 {...searchableSetting("default-permissions")}
245 description={
246 isProjectScope
247 ? "Permissions for new threads in this project."
248 : "Default permissions for new threads. Projects can override them."
249 }
250 resetAction={
251 settings.defaultRuntimeMode !== DEFAULT_SERVER_SETTINGS.defaultRuntimeMode ? (
252 <SettingResetButton
253 label="default permissions"
254 onClick={() =>
255 updateSettings({
256 defaultRuntimeMode: DEFAULT_SERVER_SETTINGS.defaultRuntimeMode,
257 })
258 }
259 />
260 ) : null
261 }
262 control={
263 <Select
264 value={mixedPermissions ? null : settings.defaultRuntimeMode}
265 onValueChange={(value) => {
266 if (value) updateSettings({ defaultRuntimeMode: value });
267 }}
268 >
269 <SelectTrigger size="sm" aria-label="Default permissions">
270 {!mixedPermissions && (
271 <PermissionIcon className="size-3.5 shrink-0 text-muted-foreground" />
272 )}
273 <SelectValue>
274 {mixedPermissions
275 ? "Mixed"
276 : runtimeModeConfig[settings.defaultRuntimeMode].label}
277 </SelectValue>
278 </SelectTrigger>
279 <SelectPopup align="end" alignItemWithTrigger={false}>
280 {runtimeModeOptions.map((mode) => {
281 const option = runtimeModeConfig[mode];
282 const Icon = option.icon;
283 return (
284 <SelectItem key={mode} value={mode} className="min-w-64">
285 <div className="grid gap-0.5">
286 <span className="inline-flex items-center gap-1.5 font-medium">
287 <Icon className="size-3.5 shrink-0 text-muted-foreground" />
288 {option.label}
289 </span>
290 <span className="text-xs leading-4 text-muted-foreground">
291 {option.description}
292 </span>
293 </div>
294 </SelectItem>
295 );
296 })}
297 </SelectPopup>
298 </Select>
299 }
300 />
301 {workspaceRow}
302 </>
303 ) : (
304 <>
305 <SettingsRow
306 serverScoped
307 settingKeys={["defaultAutoPull"]}
308 mixed={mixedAutoPull}
309 id="automatic-pull"
310 title="Automatically pull"
311 description={
312 isProjectScope
313 ? "Keeps this project's default branch current when the checkout has no local changes or commits."
314 : "Keeps the default branch current when the checkout has no local changes or commits. Projects can override it."
315 }
316 resetAction={
317 settings.defaultAutoPull ? (
318 <SettingResetButton
319 label="default automatic pull"
320 tooltip="Reset automatic pull to off"
321 onClick={() => updateSettings({ defaultAutoPull: false })}
322 />
323 ) : null
324 }
325 control={
326 <Switch
327 aria-label="Default automatic pull"
328 mixed={mixedAutoPull}
329 checked={mixedAutoPull ? false : settings.defaultAutoPull}
330 onCheckedChange={(enabled) => updateSettings({ defaultAutoPull: enabled })}
331 />
332 }
333 />
334 <SettingsRow
335 serverScoped
336 settingKeys={["removeAgentCreditsOnMerge"]}
337 mixed={mixedAgentCredits}
338 {...searchableSetting("remove-agent-credits-on-merge")}
339 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."
340 resetAction={
341 settings.removeAgentCreditsOnMerge ? (
342 <SettingResetButton
343 label="agent credit removal"
344 tooltip="Keep agent credits"
345 onClick={() => updateSettings({ removeAgentCreditsOnMerge: false })}
346 />
347 ) : null
348 }
349 control={
350 <Switch
351 aria-label="Remove agent credits when merging"
352 mixed={mixedAgentCredits}
353 checked={mixedAgentCredits ? false : settings.removeAgentCreditsOnMerge}
354 onCheckedChange={(enabled) =>
355 updateSettings({ removeAgentCreditsOnMerge: enabled })
356 }
357 />
358 }
359 />
360 <SettingsRow
361 serverScoped
362 settingKeys={["pullRequestMergeMethod"]}
363 mixed={mixedMergeMethod}
364 {...searchableSetting("pull-request-merge-method")}
365 description={
366 isProjectScope
367 ? "Pull requests in this project start with this method."
368 : "Pull requests start with this method. Last selected reuses whatever you chose most recently on this device."
369 }
370 resetAction={
371 settings.pullRequestMergeMethod !== null ? (
372 <SettingResetButton
373 label="default merge method"
374 tooltip="Reset to last selected"
375 onClick={() => updateSettings({ pullRequestMergeMethod: null })}
376 />
377 ) : null
378 }
379 control={
380 <Select
381 value={mixedMergeMethod ? null : (settings.pullRequestMergeMethod ?? "last")}
382 onValueChange={(value) => {
383 if (value === "last") updateSettings({ pullRequestMergeMethod: null });
384 else if (value === "merge" || value === "squash" || value === "rebase")
385 updateSettings({ pullRequestMergeMethod: value });
386 }}
387 >
388 <SelectTrigger size="sm" aria-label="Default pull request merge method">
389 <SelectValue>
390 {(value: string | null) =>
391 value === "merge" || value === "squash" || value === "rebase"
392 ? PULL_REQUEST_MERGE_METHOD_LABELS[value]
393 : value === "last"
394 ? "Last selected"
395 : "Mixed"
396 }
397 </SelectValue>
398 </SelectTrigger>
399 <SelectPopup align="end" alignItemWithTrigger={false}>
400 <SelectItem value="last">Last selected</SelectItem>
401 <SelectItem value="merge">{PULL_REQUEST_MERGE_METHOD_LABELS.merge}</SelectItem>
402 <SelectItem value="squash">{PULL_REQUEST_MERGE_METHOD_LABELS.squash}</SelectItem>
403 <SelectItem value="rebase">{PULL_REQUEST_MERGE_METHOD_LABELS.rebase}</SelectItem>
404 </SelectPopup>
405 </Select>
406 }
407 />
408 </>
409 )}
410 </SettingsSection>
411 );
413​
414export function WorktreeSubmodulesRow() {
415 const { scope, target, connectedEnvironments } = useSettingsScope();
416 const settings = useScopedSettings();
417 const updateSettings = useUpdateScopedSettings();
418 const mixedSubmodules = useScopedSettingsMixed(["worktreeSubmodules"]);
419 const isProjectScope = scope.kind === "project" || scope.kind === "checkout";
420 const unavailable = connectedEnvironments.length === 0;
421 const effective = target
422 ? resolveProjectSettings(target.settings, null, null, null).settings
423 : null;
424​
425 return (
426 <SettingsRow
427 serverScoped
428 settingKeys={["worktreeSubmodules"]}
429 mixed={mixedSubmodules}
430 {...searchableSetting("worktree-submodules")}
431 description={
432 isProjectScope
433 ? "How new worktrees in this project populate git submodules."
434 : "How new worktrees populate git submodules. Projects and their t3.json can override it."
435 }
436 resetAction={
437 !isProjectScope && settings.worktreeSubmodules !== null ? (
438 <SettingResetButton
439 label="worktree submodules"
440 onClick={() => updateSettings({ worktreeSubmodules: null })}
441 />
442 ) : null
443 }
444 control={
445 <Select
446 value={mixedSubmodules ? null : (effective?.worktreeSubmodules ?? null)}
447 onValueChange={(value) => {
448 if (isWorktreeSubmodules(value)) updateSettings({ worktreeSubmodules: value });
449 }}
450 >
451 <SelectTrigger size="sm" aria-label="Worktree submodules">
452 <SelectValue>
453 {(value: string | null) =>
454 isWorktreeSubmodules(value)
455 ? WORKTREE_SUBMODULES_LABELS[value]
456 : unavailable
457 ? "Unavailable"
458 : "Mixed"
459 }
460 </SelectValue>
461 </SelectTrigger>
462 <SelectPopup align="end" alignItemWithTrigger={false}>
463 {WORKTREE_SUBMODULES_OPTIONS.map((option) => (
464 <SelectItem key={option} value={option}>
465 {WORKTREE_SUBMODULES_LABELS[option]}
466 </SelectItem>
467 ))}
468 </SelectPopup>
469 </Select>
470 }
471 />
472 );