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

ProjectDefaultsSettings.tsx

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