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

ProjectDefaultsSettings.tsx

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