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