Open History to see the last commit
1import type {
2 BackgroundActivityProfile,
3 BackgroundActivitySettings,
4 ProviderInstanceConfig,
5 PreviewViewportSetting,
6 ProviderInstanceId,
7 ServerSettings,
8 SidebarProjectGroupingMode,
9 UnifiedSettings,
10} from "@t3tools/contracts";
11import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
12import {
13 getBackgroundActivityBaseProfile,
14 normalizeBackgroundActivitySettings,
15 normalizeServerBackgroundActivitySettings,
16 resolveServerBackgroundActivitySettings,
17} from "@t3tools/shared/backgroundActivitySettings";
18import * as Duration from "effect/Duration";
19import * as Equal from "effect/Equal";
20
21export function isProjectGroupingEnabled(mode: SidebarProjectGroupingMode): boolean {
22 return mode !== "separate";
23}
24
25export function projectGroupingModeFromToggle(
26 enabled: boolean,
27 lastEnabledMode: SidebarProjectGroupingMode = "repository",
28): SidebarProjectGroupingMode {
29 if (!enabled) return "separate";
30 return lastEnabledMode === "repository_path" ? "repository_path" : "repository";
31}
32
33const LAST_ENABLED_PROJECT_GROUPING_MODE_KEY = "t3code:last-enabled-project-grouping-mode";
34
35export function readLastEnabledProjectGroupingMode(): SidebarProjectGroupingMode {
36 try {
37 return localStorage.getItem(LAST_ENABLED_PROJECT_GROUPING_MODE_KEY) === "repository_path"
38 ? "repository_path"
39 : "repository";
40 } catch {
41 return "repository";
42 }
43}
44
45export function rememberEnabledProjectGroupingMode(mode: SidebarProjectGroupingMode): void {
46 if (mode === "separate") return;
47 try {
48 localStorage.setItem(LAST_ENABLED_PROJECT_GROUPING_MODE_KEY, mode);
49 } catch {}
50}
51
52export function hasChangedBackgroundActivitySettings(
53 settings: Pick<
54 UnifiedSettings,
55 | "backgroundActivity"
56 | "backgroundActivityProfile"
57 | "automaticGitFetchInterval"
58 | "providerHealthRefreshInterval"
59 >,
60): boolean {
61 return (
62 !Equal.equals(settings.backgroundActivity, DEFAULT_UNIFIED_SETTINGS.backgroundActivity) ||
63 settings.backgroundActivityProfile !== DEFAULT_UNIFIED_SETTINGS.backgroundActivityProfile ||
64 !Equal.equals(
65 settings.automaticGitFetchInterval,
66 DEFAULT_UNIFIED_SETTINGS.automaticGitFetchInterval,
67 ) ||
68 !Equal.equals(
69 settings.providerHealthRefreshInterval,
70 DEFAULT_UNIFIED_SETTINGS.providerHealthRefreshInterval,
71 )
72 );
73}
74
75type TypographySettings = Pick<
76 UnifiedSettings,
77 | "fontFamilySans"
78 | "fontFamilyComposer"
79 | "fontFamilyCode"
80 | "fontFamilyTerminal"
81 | "fontSizeInterface"
82 | "fontSizePrompt"
83 | "fontSizeCode"
84 | "fontSizeTerminal"
85>;
86
87export function getChangedTypographySettingLabels(settings: TypographySettings): string[] {
88 return [
89 ...(settings.fontFamilySans !== DEFAULT_UNIFIED_SETTINGS.fontFamilySans ||
90 settings.fontSizeInterface !== DEFAULT_UNIFIED_SETTINGS.fontSizeInterface
91 ? ["Interface font"]
92 : []),
93 ...(settings.fontFamilyComposer !== DEFAULT_UNIFIED_SETTINGS.fontFamilyComposer ||
94 settings.fontSizePrompt !== DEFAULT_UNIFIED_SETTINGS.fontSizePrompt
95 ? ["Prompt font"]
96 : []),
97 ...(settings.fontFamilyCode !== DEFAULT_UNIFIED_SETTINGS.fontFamilyCode ||
98 settings.fontSizeCode !== DEFAULT_UNIFIED_SETTINGS.fontSizeCode
99 ? ["Code font"]
100 : []),
101 ...(settings.fontFamilyTerminal !== DEFAULT_UNIFIED_SETTINGS.fontFamilyTerminal ||
102 settings.fontSizeTerminal !== DEFAULT_UNIFIED_SETTINGS.fontSizeTerminal
103 ? ["Terminal font"]
104 : []),
105 ];
106}
107
108export type BrowserDefaultSettings = Pick<
109 UnifiedSettings,
110 | "browserDefaultViewport"
111 | "browserDefaultZoomFactor"
112 | "browserDefaultAppearance"
113 | "browserRecordingFrameRate"
114 | "browserRecordingShowKeyPresses"
115 | "browserRecordingShowMousePresses"
116 | "browserLinkTarget"
117 | "browserSearchEngine"
118 | "browserCustomSearchUrl"
119 | "browserAutoShowFloatingPreview"
120 | "pullRequestLinksOpenInApp"
121>;
122
123function isSamePreviewViewport(
124 left: PreviewViewportSetting,
125 right: PreviewViewportSetting,
126): boolean {
127 if (left._tag !== right._tag) return false;
128 if (left._tag === "fill" || right._tag === "fill") return true;
129 if (left.width !== right.width || left.height !== right.height) return false;
130 return left._tag === "preset" && right._tag === "preset"
131 ? left.presetId === right.presetId
132 : true;
133}
134
135export function getChangedBrowserSettingLabels(settings: BrowserDefaultSettings): string[] {
136 return [
137 ...(isSamePreviewViewport(
138 settings.browserDefaultViewport,
139 DEFAULT_UNIFIED_SETTINGS.browserDefaultViewport,
140 )
141 ? []
142 : ["Browser viewport"]),
143 ...(settings.browserDefaultZoomFactor !== DEFAULT_UNIFIED_SETTINGS.browserDefaultZoomFactor
144 ? ["Browser zoom"]
145 : []),
146 ...(settings.browserDefaultAppearance !== DEFAULT_UNIFIED_SETTINGS.browserDefaultAppearance
147 ? ["Browser appearance"]
148 : []),
149 ...(settings.browserRecordingFrameRate !== DEFAULT_UNIFIED_SETTINGS.browserRecordingFrameRate
150 ? ["Recording frame rate"]
151 : []),
152 ...(settings.browserRecordingShowKeyPresses ? ["Recording key presses"] : []),
153 ...(settings.browserRecordingShowMousePresses ? ["Recording mouse presses"] : []),
154 ...(settings.browserLinkTarget !== DEFAULT_UNIFIED_SETTINGS.browserLinkTarget
155 ? ["Open links in"]
156 : []),
157 ...(settings.browserSearchEngine !== DEFAULT_UNIFIED_SETTINGS.browserSearchEngine ||
158 settings.browserCustomSearchUrl !== DEFAULT_UNIFIED_SETTINGS.browserCustomSearchUrl
159 ? ["Search engine"]
160 : []),
161 ...(settings.browserAutoShowFloatingPreview !==
162 DEFAULT_UNIFIED_SETTINGS.browserAutoShowFloatingPreview
163 ? ["Floating preview"]
164 : []),
165 ...(settings.pullRequestLinksOpenInApp !== DEFAULT_UNIFIED_SETTINGS.pullRequestLinksOpenInApp
166 ? ["Open pull requests in T3 Code"]
167 : []),
168 ];
169}
170
171export function resolveBackgroundActivityProfileOption(
172 settings: ServerSettings,
173): BackgroundActivityProfile | "advanced" {
174 const resolved = resolveServerBackgroundActivitySettings(settings);
175 const normalized = normalizeBackgroundActivitySettings({
176 schemaVersion: 1,
177 profile: "custom",
178 baseProfile: resolved.profile,
179 overrides: {
180 automaticGitFetchInterval: resolved.automaticGitFetchInterval,
181 providerHealthRefreshInterval: resolved.providerHealthRefreshInterval,
182 hostPowerMonitorActiveInterval: resolved.hostPowerMonitorActiveInterval,
183 hostPowerMonitorIdleInterval: resolved.hostPowerMonitorIdleInterval,
184 pauseWhenHostLocked: resolved.pauseWhenHostLocked,
185 pauseWhenHostLowPower: resolved.pauseWhenHostLowPower,
186 pauseWhenClientLowPower: resolved.pauseWhenClientLowPower,
187 pauseWhenOnBattery: resolved.pauseWhenOnBattery,
188 },
189 });
190 return normalized.profile === "custom" ? "advanced" : normalized.profile;
191}
192
193export function backgroundActivitySharedPolicySettings(
194 settings: ServerSettings,
195 profile: BackgroundActivityProfile,
196): BackgroundActivitySettings {
197 const normalized = normalizeServerBackgroundActivitySettings(settings);
198 return {
199 schemaVersion: 1,
200 profile: "custom",
201 baseProfile: profile,
202 overrides: normalized.profile === "custom" ? normalized.overrides : {},
203 };
204}
205
206export function buildProviderInstanceUpdatePatch(input: {
207 readonly settings: Pick<ServerSettings, "providerInstances">;
208 readonly instanceId: ProviderInstanceId;
209 readonly instance: ProviderInstanceConfig;
210 readonly textGenerationModelSelection?:
211 | ServerSettings["textGenerationModelSelection"]
212 | undefined;
213}): Partial<UnifiedSettings> {
214 return {
215 providerInstances: {
216 ...input.settings.providerInstances,
217 [input.instanceId]: input.instance,
218 },
219 ...(input.textGenerationModelSelection !== undefined
220 ? { textGenerationModelSelection: input.textGenerationModelSelection }
221 : {}),
222 };
223}
224
225export const PROVIDER_HEALTH_INTERVAL_STEP_SECONDS = 30;
226
227type BackgroundActivityOverridePatch = Partial<{
228 [K in keyof BackgroundActivitySettings["overrides"]]:
229 | BackgroundActivitySettings["overrides"][K]
230 | undefined;
231}>;
232
233export function durationToSeconds(duration: Duration.Duration): number {
234 return Math.round(Duration.toMillis(duration) / 1_000);
235}
236
237export function normalizeIntervalSeconds(value: number | null, minimum = 0): number {
238 if (value === null || !Number.isFinite(value)) {
239 return minimum;
240 }
241 return Math.max(minimum, Math.round(value));
242}
243
244export function backgroundActivityOverrideSettings(
245 current: BackgroundActivitySettings,
246 resolved: ReturnType<typeof resolveServerBackgroundActivitySettings>,
247 overrides: BackgroundActivityOverridePatch,
248) {
249 const nextOverrides: BackgroundActivityOverridePatch = {
250 automaticGitFetchInterval: resolved.automaticGitFetchInterval,
251 providerHealthRefreshInterval: resolved.providerHealthRefreshInterval,
252 hostPowerMonitorActiveInterval: resolved.hostPowerMonitorActiveInterval,
253 hostPowerMonitorIdleInterval: resolved.hostPowerMonitorIdleInterval,
254 pauseWhenHostLocked: resolved.pauseWhenHostLocked,
255 pauseWhenHostLowPower: resolved.pauseWhenHostLowPower,
256 pauseWhenClientLowPower: resolved.pauseWhenClientLowPower,
257 pauseWhenOnBattery: resolved.pauseWhenOnBattery,
258 ...overrides,
259 };
260 for (const [key, value] of Object.entries(nextOverrides)) {
261 if (value === undefined) {
262 delete nextOverrides[key as keyof typeof nextOverrides];
263 }
264 }
265 return {
266 backgroundActivity: {
267 schemaVersion: 1 as const,
268 profile: "custom" as const,
269 baseProfile: getBackgroundActivityBaseProfile(current),
270 overrides: nextOverrides as BackgroundActivitySettings["overrides"],
271 },
272 };
273}