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