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