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

ProjectDefaultsSettings.tsx

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