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 idleClientTtl: resolved.idleClientTtl,
185 pauseWhenHostLocked: resolved.pauseWhenHostLocked,
186 pauseWhenHostLowPower: resolved.pauseWhenHostLowPower,
187 pauseWhenClientLowPower: resolved.pauseWhenClientLowPower,
188 pauseWhenOnBattery: resolved.pauseWhenOnBattery,
189 },
190 });
191 return normalized.profile === "custom" ? "advanced" : normalized.profile;
192}
193
194export function backgroundActivitySharedPolicySettings(
195 settings: ServerSettings,
196 profile: BackgroundActivityProfile,
197): BackgroundActivitySettings {
198 const normalized = normalizeServerBackgroundActivitySettings(settings);
199 return {
200 schemaVersion: 1,
201 profile: "custom",
202 baseProfile: profile,
203 overrides: normalized.profile === "custom" ? normalized.overrides : {},
204 };
205}
206
207export function buildProviderInstanceUpdatePatch(input: {
208 readonly settings: Pick<ServerSettings, "providerInstances">;
209 readonly instanceId: ProviderInstanceId;
210 readonly instance: ProviderInstanceConfig;
211 readonly textGenerationModelSelection?:
212 | ServerSettings["textGenerationModelSelection"]
213 | undefined;
214}): Partial<UnifiedSettings> {
215 return {
216 providerInstances: {
217 ...input.settings.providerInstances,
218 [input.instanceId]: input.instance,
219 },
220 ...(input.textGenerationModelSelection !== undefined
221 ? { textGenerationModelSelection: input.textGenerationModelSelection }
222 : {}),
223 };
224}
225
226export const PROVIDER_HEALTH_INTERVAL_STEP_SECONDS = 30;
227
228type BackgroundActivityOverridePatch = Partial<{
229 [K in keyof BackgroundActivitySettings["overrides"]]:
230 | BackgroundActivitySettings["overrides"][K]
231 | undefined;
232}>;
233
234export function durationToSeconds(duration: Duration.Duration): number {
235 return Math.round(Duration.toMillis(duration) / 1_000);
236}
237
238export function normalizeIntervalSeconds(value: number | null, minimum = 0): number {
239 if (value === null || !Number.isFinite(value)) {
240 return minimum;
241 }
242 return Math.max(minimum, Math.round(value));
243}
244
245export function backgroundActivityOverrideSettings(
246 current: BackgroundActivitySettings,
247 resolved: ReturnType<typeof resolveServerBackgroundActivitySettings>,
248 overrides: BackgroundActivityOverridePatch,
249) {
250 const nextOverrides: BackgroundActivityOverridePatch = {
251 automaticGitFetchInterval: resolved.automaticGitFetchInterval,
252 providerHealthRefreshInterval: resolved.providerHealthRefreshInterval,
253 hostPowerMonitorActiveInterval: resolved.hostPowerMonitorActiveInterval,
254 hostPowerMonitorIdleInterval: resolved.hostPowerMonitorIdleInterval,
255 idleClientTtl: resolved.idleClientTtl,
256 pauseWhenHostLocked: resolved.pauseWhenHostLocked,
257 pauseWhenHostLowPower: resolved.pauseWhenHostLowPower,
258 pauseWhenClientLowPower: resolved.pauseWhenClientLowPower,
259 pauseWhenOnBattery: resolved.pauseWhenOnBattery,
260 ...overrides,
261 };
262 for (const [key, value] of Object.entries(nextOverrides)) {
263 if (value === undefined) {
264 delete nextOverrides[key as keyof typeof nextOverrides];
265 }
266 }
267 return {
268 backgroundActivity: {
269 schemaVersion: 1 as const,
270 profile: "custom" as const,
271 baseProfile: getBackgroundActivityBaseProfile(current),
272 overrides: nextOverrides as BackgroundActivitySettings["overrides"],
273 },
274 };
275}