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

ProjectDefaultsSettings.tsx

501 lines19.9 KB

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