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 {...searchableSetting("source-control-writing-style")}
78 description={MODE_OPTIONS[style.mode].description}
79 resetAction={
80 isSourceControlWritingStyleDirty ? (
81 <SettingResetButton
82 label="source control writing style"
83 onClick={() =>
84 updateSettings({
85 sourceControlWritingStyle: {
86 mode: defaults.mode,
87 customInstructions: defaults.customInstructions,
88 },
89 })
90 }
91 />
92 ) : null
93 }
94 control={
95 <Select
96 value={style.mode}
97 onValueChange={(value) => {
98 const customInstructions = customInstructionsRef.current?.value.trim();
99 updateSettings({
100 sourceControlWritingStyle: {
101 mode: value as SourceControlWritingStyleMode,
102 ...(customInstructions !== undefined ? { customInstructions } : {}),
103 },
104 });
105 }}
106 >
107 <SelectTrigger className="w-full sm:w-56" aria-label="Source control writing style">
108 <SelectValue>{MODE_OPTIONS[style.mode].label}</SelectValue>
109 </SelectTrigger>
110 <SelectPopup align="end" alignItemWithTrigger={false}>
111 {(Object.keys(MODE_OPTIONS) as SourceControlWritingStyleMode[]).map((mode) => (
112 <SelectItem key={mode} hideIndicator value={mode}>
113 {MODE_OPTIONS[mode].label}
114 </SelectItem>
115 ))}
116 </SelectPopup>
117 </Select>
118 }
119 >
120 {style.mode === "custom" ? (
121 <div className="mt-3 max-w-2xl pb-3.5">
122 <Textarea
123 key={style.customInstructions}
124 ref={customInstructionsRef}
125 defaultValue={style.customInstructions}
126 onBlur={(event) => {
127 const customInstructions = event.target.value.trim();
128 if (customInstructions !== style.customInstructions) {
129 updateSettings({ sourceControlWritingStyle: { customInstructions } });
130 }
131 }}
132 rows={4}
133 placeholder="Keep titles concise. Use short bullet points in descriptions."
134 aria-label="Custom source control writing instructions"
135 />
136 </div>
137 ) : null}
138 </SettingsRow>
139
140 <SettingsRow
141 {...searchableSetting("follow-change-request-templates")}
142 description="Structures change request descriptions using the current repository's template when one is available."
143 resetAction={
144 style.followChangeRequestTemplates !== defaults.followChangeRequestTemplates ? (
145 <SettingResetButton
146 label="change request templates"
147 onClick={() =>
148 updateSettings({
149 sourceControlWritingStyle: {
150 followChangeRequestTemplates: defaults.followChangeRequestTemplates,
151 },
152 })
153 }
154 />
155 ) : null
156 }
157 control={
158 <Switch
159 checked={style.followChangeRequestTemplates}
160 onCheckedChange={(checked) =>
161 updateSettings({
162 sourceControlWritingStyle: {
163 followChangeRequestTemplates: Boolean(checked),
164 },
165 })
166 }
167 aria-label="Follow change request templates"
168 />
169 }
170 />
171
172 <SettingsRow
173 {...searchableSetting("source-control-writer-model")}
174 description="Optional model override for change descriptions, change request titles and descriptions, and branch or bookmark names. Off uses the global text generation model."
175 control={
176 <div className="flex flex-wrap items-center justify-end gap-2">
177 {usesDedicatedModel ? (
178 <ProviderModelPicker
179 activeInstanceId={activeSelection.instanceId}
180 model={activeSelection.model}
181 lockedProvider={null}
182 instanceEntries={instanceEntries}
183 modelOptionsByInstance={modelOptionsByInstance}
184 triggerVariant="outline"
185 triggerClassName="min-w-0 max-w-none shrink-0 text-foreground/90 hover:text-foreground"
186 triggerAriaLabel="Source control writer model"
187 onInstanceModelChange={(instanceId, model) => {
188 updateSettings({
189 sourceControlWriterModelSelection: createModelSelection(instanceId, model),
190 });
191 }}
192 />
193 ) : null}
194 <Switch
195 checked={usesDedicatedModel}
196 onCheckedChange={(checked) =>
197 updateSettings({
198 sourceControlWriterModelSelection: checked
199 ? createModelSelection(
200 defaultModelSelection.instanceId,
201 defaultModelSelection.model,
202 defaultModelSelection.options,
203 )
204 : null,
205 })
206 }
207 aria-label="Use a separate source control writer model"
208 />
209 </div>
210 }
211 />
212 </SettingsSection>
213 );
214}