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

ProjectDefaultsSettings.tsx

465 lines18.6 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 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);
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 mixedPermissions = useScopedSettingsMixed(["defaultRuntimeMode"]);
81 const PermissionIcon = runtimeModeConfig[settings.defaultRuntimeMode].icon;
82 const mixedWorkspace = useScopedSettingsMixed(["defaultThreadEnvMode"]);
83 const mixedAutoPull = useScopedSettingsMixed(["defaultAutoPull"]);
84 const mixedAgentCredits = useScopedSettingsMixed(["removeAgentCreditsOnMerge"]);
85 const mixedMergeMethod = useScopedSettingsMixed(["pullRequestMergeMethod"]);
86 const modelSource = useScopedSettingSource(["defaultModelSelection"]);
87 const isProjectScope = scope.kind === "project" || scope.kind === "checkout";
88 const unavailable = connectedEnvironments.length === 0;
89 const effective = target
90 ? resolveProjectSettings(target.settings, null, null, null).settings
91 : null;
92​
93 const modelDisabledReason = useScopedModelDisabledReason(settings, entries);
94​
95 const setModel = (value: ModelSelection | null) => {
96 const reason = value ? modelDisabledReason(value.instanceId, value.model) : null;
97 if (reason) {
98 toastManager.add({ type: "error", title: "Default model not saved", description: reason });
99 return;
100 }
101 updateSettings({ defaultModelSelection: value });
102 };
103​
104 const modelRow = (
105 <SettingsRow
106 serverScoped
107 settingKeys={["defaultModelSelection"]}
108 mixed={mixedModel}
109 id="default-model"
110 title="Model"
111 description={
112 isProjectScope
113 ? "Model for new threads in this project."
114 : !unavailable &&
115 !mixedModel &&
116 modelSource !== "project" &&
117 settings.defaultModelSelection === null
118 ? "Default model for new threads, selected automatically. Projects can override it."
119 : "Default model for new threads. Projects can override it."
120 }
121 resetAction={
122 settings.defaultModelSelection !== null ? (
123 <SettingResetButton label="default model" onClick={() => setModel(null)} />
124 ) : null
125 }
126 control={
127 selection && activeEntry ? (
128 <div className="flex min-w-0 flex-wrap items-center justify-end gap-1.5">
129 <ProviderModelPicker
130 environmentId={representative?.environmentId ?? null}
131 activeInstanceId={selection.instanceId}
132 model={selection.model}
133 lockedProvider={null}
134 instanceEntries={entries}
135 modelOptionsByInstance={modelOptions}
136 triggerClassName={SETTINGS_PICKER_TRIGGER_CLASSNAME}
137 {...(mixedModel ? { triggerLabel: "Mixed" } : {})}
138 getModelDisabledReason={modelDisabledReason}
139 onOpenProviderSetup={(instanceId) => {
140 if (representative)
141 void navigate({
142 to: "/settings/providers",
143 search: { environmentId: representative.environmentId, instanceId },
144 });
145 }}
146 onInstanceModelChange={(instanceId, model) =>
147 setModel(createModelSelection(instanceId, model))
148 }
149 />
150 {!mixedModel ? (
151 <TraitsPicker
152 provider={activeEntry.driverKind}
153 models={activeEntry.models}
154 model={selection.model}
155 modelOptions={selection.options ?? []}
156 allowPromptInjectedEffort={false}
157 planModeEnabled={settings.planModeEnabled}
158 triggerClassName={SETTINGS_PICKER_TRIGGER_CLASSNAME}
159 onModelOptionsChange={(options) =>
160 setModel(createModelSelection(selection.instanceId, selection.model, options))
161 }
162 />
163 ) : null}
164 </div>
165 ) : (
166 <span className="text-sm text-muted-foreground">No providers available</span>
167 )
168 }
169 />
170 );
171 const workspaceRow = (
172 <SettingsRow
173 serverScoped
174 settingKeys={["defaultThreadEnvMode"]}
175 mixed={mixedWorkspace}
176 id={searchableSetting("new-threads").id}
177 title="Workspace"
178 description={
179 isProjectScope
180 ? "Where new threads in this project start."
181 : "Where new threads start. Projects and their t3.json can override it."
182 }
183 resetAction={
184 !isProjectScope && settings.defaultThreadEnvMode !== null ? (
185 <SettingResetButton
186 label="default workspace"
187 onClick={() => updateSettings({ defaultThreadEnvMode: null })}
188 />
189 ) : null
190 }
191 control={
192 <Select
193 value={mixedWorkspace ? null : (effective?.defaultThreadEnvMode ?? null)}
194 onValueChange={(value) => {
195 if (value === "local" || value === "worktree")
196 updateSettings({ defaultThreadEnvMode: value });
197 }}
198 >
199 <SelectTrigger size="sm" aria-label="Default workspace">
200 <SelectValue>
201 {(value: string | null) =>
202 value === "local" || value === "worktree"
203 ? resolveEnvModeLabel(value)
204 : unavailable
205 ? "Unavailable"
206 : "Mixed"
207 }
208 </SelectValue>
209 </SelectTrigger>
210 <SelectPopup align="end" alignItemWithTrigger={false}>
211 <SelectItem value="local">{resolveEnvModeLabel("local")}</SelectItem>
212 <SelectItem value="worktree">{resolveEnvModeLabel("worktree")}</SelectItem>
213 </SelectPopup>
214 </Select>
215 }
216 />
217 );
218​
219 return (
220 <SettingsSection
221 id={category === "source-control" ? "source-control-defaults" : "project-defaults"}
222 title={category === "source-control" ? "Repositories" : "New threads"}
223 >
224 {category === "project" ? (
225 <>
226 {modelRow}
227 {workspaceRow}
228 </>
229 ) : category === "general" ? (
230 <>
231 {modelRow}
232 <SettingsRow
233 serverScoped
234 settingKeys={["defaultRuntimeMode"]}
235 mixed={mixedPermissions}
236 {...searchableSetting("default-permissions")}
237 description={
238 isProjectScope
239 ? "Permissions for new threads in this project."
240 : "Default permissions for new threads. Projects can override them."
241 }
242 resetAction={
243 settings.defaultRuntimeMode !== DEFAULT_SERVER_SETTINGS.defaultRuntimeMode ? (
244 <SettingResetButton
245 label="default permissions"
246 onClick={() =>
247 updateSettings({
248 defaultRuntimeMode: DEFAULT_SERVER_SETTINGS.defaultRuntimeMode,
249 })
250 }
251 />
252 ) : null
253 }
254 control={
255 <Select
256 value={mixedPermissions ? null : settings.defaultRuntimeMode}
257 onValueChange={(value) => {
258 if (value) updateSettings({ defaultRuntimeMode: value });
259 }}
260 >
261 <SelectTrigger size="sm" aria-label="Default permissions">
262 {!mixedPermissions && (
263 <PermissionIcon className="size-3.5 shrink-0 text-muted-foreground" />
264 )}
265 <SelectValue>
266 {mixedPermissions
267 ? "Mixed"
268 : runtimeModeConfig[settings.defaultRuntimeMode].label}
269 </SelectValue>
270 </SelectTrigger>
271 <SelectPopup align="end" alignItemWithTrigger={false}>
272 {runtimeModeOptions.map((mode) => {
273 const option = runtimeModeConfig[mode];
274 const Icon = option.icon;
275 return (
276 <SelectItem key={mode} value={mode} className="min-w-64">
277 <div className="grid gap-0.5">
278 <span className="inline-flex items-center gap-1.5 font-medium">
279 <Icon className="size-3.5 shrink-0 text-muted-foreground" />
280 {option.label}
281 </span>
282 <span className="text-xs leading-4 text-muted-foreground">
283 {option.description}
284 </span>
285 </div>
286 </SelectItem>
287 );
288 })}
289 </SelectPopup>
290 </Select>
291 }
292 />
293 {workspaceRow}
294 </>
295 ) : (
296 <>
297 <SettingsRow
298 serverScoped
299 settingKeys={["defaultAutoPull"]}
300 mixed={mixedAutoPull}
301 id="automatic-pull"
302 title="Automatically pull"
303 description={
304 isProjectScope
305 ? "Keeps this project's default branch current when the checkout has no local changes or commits."
306 : "Keeps the default branch current when the checkout has no local changes or commits. Projects can override it."
307 }
308 resetAction={
309 settings.defaultAutoPull ? (
310 <SettingResetButton
311 label="default automatic pull"
312 tooltip="Reset automatic pull to off"
313 onClick={() => updateSettings({ defaultAutoPull: false })}
314 />
315 ) : null
316 }
317 control={
318 <Switch
319 aria-label="Default automatic pull"
320 mixed={mixedAutoPull}
321 checked={mixedAutoPull ? false : settings.defaultAutoPull}
322 onCheckedChange={(enabled) => updateSettings({ defaultAutoPull: enabled })}
323 />
324 }
325 />
326 <SettingsRow
327 serverScoped
328 settingKeys={["removeAgentCreditsOnMerge"]}
329 mixed={mixedAgentCredits}
330 {...searchableSetting("remove-agent-credits-on-merge")}
331 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."
332 resetAction={
333 settings.removeAgentCreditsOnMerge ? (
334 <SettingResetButton
335 label="agent credit removal"
336 tooltip="Keep agent credits"
337 onClick={() => updateSettings({ removeAgentCreditsOnMerge: false })}
338 />
339 ) : null
340 }
341 control={
342 <Switch
343 aria-label="Remove agent credits when merging"
344 mixed={mixedAgentCredits}
345 checked={mixedAgentCredits ? false : settings.removeAgentCreditsOnMerge}
346 onCheckedChange={(enabled) =>
347 updateSettings({ removeAgentCreditsOnMerge: enabled })
348 }
349 />
350 }
351 />
352 <SettingsRow
353 serverScoped
354 settingKeys={["pullRequestMergeMethod"]}
355 mixed={mixedMergeMethod}
356 {...searchableSetting("pull-request-merge-method")}
357 description={
358 isProjectScope
359 ? "Pull requests in this project start with this method."
360 : "Pull requests start with this method. Last selected reuses whatever you chose most recently on this device."
361 }
362 resetAction={
363 settings.pullRequestMergeMethod !== null ? (
364 <SettingResetButton
365 label="default merge method"
366 tooltip="Reset to last selected"
367 onClick={() => updateSettings({ pullRequestMergeMethod: null })}
368 />
369 ) : null
370 }
371 control={
372 <Select
373 value={mixedMergeMethod ? null : (settings.pullRequestMergeMethod ?? "last")}
374 onValueChange={(value) => {
375 if (value === "last") updateSettings({ pullRequestMergeMethod: null });
376 else if (value === "merge" || value === "squash" || value === "rebase")
377 updateSettings({ pullRequestMergeMethod: value });
378 }}
379 >
380 <SelectTrigger size="sm" aria-label="Default pull request merge method">
381 <SelectValue>
382 {(value: string | null) =>
383 value === "merge" || value === "squash" || value === "rebase"
384 ? PULL_REQUEST_MERGE_METHOD_LABELS[value]
385 : value === "last"
386 ? "Last selected"
387 : "Mixed"
388 }
389 </SelectValue>
390 </SelectTrigger>
391 <SelectPopup align="end" alignItemWithTrigger={false}>
392 <SelectItem value="last">Last selected</SelectItem>
393 <SelectItem value="merge">{PULL_REQUEST_MERGE_METHOD_LABELS.merge}</SelectItem>
394 <SelectItem value="squash">{PULL_REQUEST_MERGE_METHOD_LABELS.squash}</SelectItem>
395 <SelectItem value="rebase">{PULL_REQUEST_MERGE_METHOD_LABELS.rebase}</SelectItem>
396 </SelectPopup>
397 </Select>
398 }
399 />
400 </>
401 )}
402 </SettingsSection>
403 );
405​
406export function WorktreeSubmodulesRow() {
407 const { scope, target, connectedEnvironments } = useSettingsScope();
408 const settings = useScopedSettings();
409 const updateSettings = useUpdateScopedSettings();
410 const mixedSubmodules = useScopedSettingsMixed(["worktreeSubmodules"]);
411 const isProjectScope = scope.kind === "project" || scope.kind === "checkout";
412 const unavailable = connectedEnvironments.length === 0;
413 const effective = target
414 ? resolveProjectSettings(target.settings, null, null, null).settings
415 : null;
416​
417 return (
418 <SettingsRow
419 serverScoped
420 settingKeys={["worktreeSubmodules"]}
421 mixed={mixedSubmodules}
422 {...searchableSetting("worktree-submodules")}
423 description={
424 isProjectScope
425 ? "How new worktrees in this project populate git submodules."
426 : "How new worktrees populate git submodules. Projects and their t3.json can override it."
427 }
428 resetAction={
429 !isProjectScope && settings.worktreeSubmodules !== null ? (
430 <SettingResetButton
431 label="worktree submodules"
432 onClick={() => updateSettings({ worktreeSubmodules: null })}
433 />
434 ) : null
435 }
436 control={
437 <Select
438 value={mixedSubmodules ? null : (effective?.worktreeSubmodules ?? null)}
439 onValueChange={(value) => {
440 if (isWorktreeSubmodules(value)) updateSettings({ worktreeSubmodules: value });
441 }}
442 >
443 <SelectTrigger size="sm" aria-label="Worktree submodules">
444 <SelectValue>
445 {(value: string | null) =>
446 isWorktreeSubmodules(value)
447 ? WORKTREE_SUBMODULES_LABELS[value]
448 : unavailable
449 ? "Unavailable"
450 : "Mixed"
451 }
452 </SelectValue>
453 </SelectTrigger>
454 <SelectPopup align="end" alignItemWithTrigger={false}>
455 {WORKTREE_SUBMODULES_OPTIONS.map((option) => (
456 <SelectItem key={option} value={option}>
457 {WORKTREE_SUBMODULES_LABELS[option]}
458 </SelectItem>
459 ))}
460 </SelectPopup>
461 </Select>
462 }
463 />
464 );