Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { useNavigate } from "@tanstack/react-router";
3import { useRef } from "react";
4import type { ProviderInstanceId, SourceControlWritingStyleMode } from "@t3tools/contracts";
5import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
6import { createModelSelection } from "@t3tools/shared/model";
7import { resolveSourceControlWriterModelSelection } from "@t3tools/shared/serverSettings";
8
9import { usePrimarySettings, useUpdatePrimarySettings } from "../../hooks/useSettings";
10import {
11 applyProviderInstanceSettings,
12 deriveProviderInstanceEntries,
13 sortProviderInstanceEntries,
14} from "../../providerInstances";
15import {
16 getCustomModelOptionsByInstance,
17 resolveAppModelSelectionState,
18} from "../../modelSelection";
19import { primaryServerProvidersAtom } from "../../state/server";
20import { usePrimaryEnvironmentId } from "../../state/environments";
21import { ProviderModelPicker } from "../chat/ProviderModelPicker";
22import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
23import { Switch } from "../ui/switch";
24import { Textarea } from "../ui/textarea";
25import {
26 SETTINGS_PICKER_TRIGGER_CLASSNAME,
27 SettingResetButton,
28 SettingsRow,
29 SettingsSection,
30} from "./settingsLayout";
31import { searchableSetting } from "./settingsSearch";
32
33const MODE_OPTIONS: Record<SourceControlWritingStyleMode, { label: string; description: string }> =
34 {
35 repo_conventions: {
36 label: "Repository conventions",
37 description: "In each project, matches recent change descriptions and change request titles.",
38 },
39 conventional_commits: {
40 label: "Conventional Commits",
41 description: "Use Conventional Commit prefixes and keep change request text concise.",
42 },
43 custom: {
44 label: "Custom instructions",
45 description:
46 "Use your instructions for change descriptions and change requests in every project.",
47 },
48 };
49
50export function SourceControlWritingSettingsSection() {
51 const settings = usePrimarySettings();
52 const updateSettings = useUpdatePrimarySettings();
53 const navigate = useNavigate();
54 const environmentId = usePrimaryEnvironmentId();
55 const serverProviders = useAtomValue(primaryServerProvidersAtom);
56 const customInstructionsRef = useRef<HTMLTextAreaElement>(null);
57 const style = settings.sourceControlWritingStyle;
58 const defaults = DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle;
59 const isSourceControlWritingStyleDirty =
60 style.mode !== defaults.mode || style.customInstructions !== defaults.customInstructions;
61
62 const textGenerationProviders = serverProviders.filter(
63 (provider) => provider.supportsTextGeneration !== false,
64 );
65 const defaultModelSelection = resolveAppModelSelectionState(settings, textGenerationProviders);
66 const usesDedicatedModel = settings.sourceControlWriterModelSelection !== null;
67 const activeSelection = resolveAppModelSelectionState(
68 {
69 ...settings,
70 textGenerationModelSelection: resolveSourceControlWriterModelSelection(
71 settings,
72 textGenerationProviders,
73 ),
74 },
75 textGenerationProviders,
76 );
77 const instanceEntries = sortProviderInstanceEntries(
78 applyProviderInstanceSettings(deriveProviderInstanceEntries(textGenerationProviders), settings),
79 );
80 const canEnableDedicatedModel = instanceEntries.some(
81 (entry) =>
82 entry.instanceId === defaultModelSelection.instanceId && entry.enabled && entry.isAvailable,
83 );
84 const modelOptionsByInstance = getCustomModelOptionsByInstance(
85 settings,
86 textGenerationProviders,
87 activeSelection.instanceId,
88 activeSelection.model,
89 );
90
91 return (
92 <SettingsSection id="source-control-text-generation" title="Text generation">
93 <SettingsRow
94 serverScoped
95 {...searchableSetting("source-control-writing-style")}
96 description={MODE_OPTIONS[style.mode].description}
97 resetAction={
98 isSourceControlWritingStyleDirty ? (
99 <SettingResetButton
100 label="source control writing style"
101 onClick={() =>
102 updateSettings({
103 sourceControlWritingStyle: {
104 mode: defaults.mode,
105 customInstructions: defaults.customInstructions,
106 },
107 })
108 }
109 />
110 ) : null
111 }
112 control={
113 <Select
114 value={style.mode}
115 onValueChange={(value) => {
116 const customInstructions = customInstructionsRef.current?.value.trim();
117 updateSettings({
118 sourceControlWritingStyle: {
119 mode: value as SourceControlWritingStyleMode,
120 ...(customInstructions !== undefined ? { customInstructions } : {}),
121 },
122 });
123 }}
124 >
125 <SelectTrigger
126 size="sm"
127 className="w-full sm:w-56"
128 aria-label="Source control writing style"
129 >
130 <SelectValue>{MODE_OPTIONS[style.mode].label}</SelectValue>
131 </SelectTrigger>
132 <SelectPopup align="end" alignItemWithTrigger={false}>
133 {(Object.keys(MODE_OPTIONS) as SourceControlWritingStyleMode[]).map((mode) => (
134 <SelectItem key={mode} hideIndicator value={mode}>
135 {MODE_OPTIONS[mode].label}
136 </SelectItem>
137 ))}
138 </SelectPopup>
139 </Select>
140 }
141 >
142 {style.mode === "custom" ? (
143 <div className="mt-3 max-w-2xl pb-3.5">
144 <Textarea
145 key={style.customInstructions}
146 ref={customInstructionsRef}
147 defaultValue={style.customInstructions}
148 onBlur={(event) => {
149 const customInstructions = event.target.value.trim();
150 if (customInstructions !== style.customInstructions) {
151 updateSettings({ sourceControlWritingStyle: { customInstructions } });
152 }
153 }}
154 rows={4}
155 placeholder="Keep titles concise. Use short bullet points in descriptions."
156 aria-label="Custom source control writing instructions"
157 />
158 </div>
159 ) : null}
160 </SettingsRow>
161
162 <SettingsRow
163 serverScoped
164 {...searchableSetting("follow-change-request-templates")}
165 description="Use the repository's template for change request descriptions when available."
166 resetAction={
167 style.followChangeRequestTemplates !== defaults.followChangeRequestTemplates ? (
168 <SettingResetButton
169 label="change request templates"
170 onClick={() =>
171 updateSettings({
172 sourceControlWritingStyle: {
173 followChangeRequestTemplates: defaults.followChangeRequestTemplates,
174 },
175 })
176 }
177 />
178 ) : null
179 }
180 control={
181 <Switch
182 checked={style.followChangeRequestTemplates}
183 onCheckedChange={(checked) =>
184 updateSettings({
185 sourceControlWritingStyle: {
186 followChangeRequestTemplates: Boolean(checked),
187 },
188 })
189 }
190 aria-label="Follow change request templates"
191 />
192 }
193 />
194
195 <SettingsRow
196 serverScoped
197 {...searchableSetting("source-control-writer-model")}
198 description="Model for source control text and branch or bookmark names. Off uses the global default."
199 control={
200 <div className="flex flex-wrap items-center justify-end gap-2">
201 {usesDedicatedModel && !canEnableDedicatedModel ? (
202 <span className="text-sm text-muted-foreground">
203 No text generation providers available.
204 </span>
205 ) : null}
206 {usesDedicatedModel && canEnableDedicatedModel ? (
207 <ProviderModelPicker
208 activeInstanceId={activeSelection.instanceId}
209 model={activeSelection.model}
210 lockedProvider={null}
211 instanceEntries={instanceEntries}
212 modelOptionsByInstance={modelOptionsByInstance}
213 triggerVariant="outline"
214 triggerClassName={SETTINGS_PICKER_TRIGGER_CLASSNAME}
215 triggerAriaLabel="Source control writer model"
216 {...(environmentId
217 ? {
218 onOpenProviderSetup: (instanceId: ProviderInstanceId) => {
219 void navigate({
220 to: "/settings/providers",
221 search: { environmentId, instanceId },
222 });
223 },
224 }
225 : {})}
226 onInstanceModelChange={(instanceId, model) => {
227 updateSettings({
228 sourceControlWriterModelSelection: createModelSelection(instanceId, model),
229 });
230 }}
231 />
232 ) : null}
233 <Switch
234 checked={usesDedicatedModel}
235 disabled={!usesDedicatedModel && !canEnableDedicatedModel}
236 onCheckedChange={(checked) =>
237 updateSettings({
238 sourceControlWriterModelSelection: checked
239 ? createModelSelection(
240 defaultModelSelection.instanceId,
241 defaultModelSelection.model,
242 defaultModelSelection.options,
243 )
244 : null,
245 })
246 }
247 aria-label="Use a separate source control writer model"
248 />
249 </div>
250 }
251 />
252 </SettingsSection>
253 );
254}