Open History to see the last commit
1import type {
2 BackgroundActivityProfile,
3 BackgroundActivitySettings,
4 ProviderDriverKind,
5 ProviderInstanceConfig,
6 ProviderInstanceId,
7 ServerSettings,
8 SidebarProjectGroupingMode,
9 UnifiedSettings,
10} from "@t3tools/contracts";
11import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
12import {
13 normalizeBackgroundActivitySettings,
14 normalizeServerBackgroundActivitySettings,
15 resolveServerBackgroundActivitySettings,
16} from "@t3tools/shared/backgroundActivitySettings";
17import * as Equal from "effect/Equal";
18
19export function isProjectGroupingEnabled(mode: SidebarProjectGroupingMode): boolean {
20 return mode !== "separate";
21}
22
23export function projectGroupingModeFromToggle(
24 enabled: boolean,
25 lastEnabledMode: SidebarProjectGroupingMode = "repository",
26): SidebarProjectGroupingMode {
27 if (!enabled) return "separate";
28 return lastEnabledMode === "repository_path" ? "repository_path" : "repository";
29}
30
31const LAST_ENABLED_PROJECT_GROUPING_MODE_KEY = "t3code:last-enabled-project-grouping-mode";
32
33export function readLastEnabledProjectGroupingMode(): SidebarProjectGroupingMode {
34 try {
35 return localStorage.getItem(LAST_ENABLED_PROJECT_GROUPING_MODE_KEY) === "repository_path"
36 ? "repository_path"
37 : "repository";
38 } catch {
39 return "repository";
40 }
41}
42
43export function rememberEnabledProjectGroupingMode(mode: SidebarProjectGroupingMode): void {
44 if (mode === "separate") return;
45 try {
46 localStorage.setItem(LAST_ENABLED_PROJECT_GROUPING_MODE_KEY, mode);
47 } catch {
48 // Storage can be unavailable in restricted browser contexts.
49 }
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
75export function resolveBackgroundActivityProfileOption(
76 settings: ServerSettings,
77): BackgroundActivityProfile | "advanced" {
78 const resolved = resolveServerBackgroundActivitySettings(settings);
79 const normalized = normalizeBackgroundActivitySettings({
80 schemaVersion: 1,
81 profile: "custom",
82 baseProfile: resolved.profile,
83 overrides: {
84 automaticGitFetchInterval: resolved.automaticGitFetchInterval,
85 providerHealthRefreshInterval: resolved.providerHealthRefreshInterval,
86 hostPowerMonitorActiveInterval: resolved.hostPowerMonitorActiveInterval,
87 hostPowerMonitorIdleInterval: resolved.hostPowerMonitorIdleInterval,
88 idleClientTtl: resolved.idleClientTtl,
89 pauseWhenHostLocked: resolved.pauseWhenHostLocked,
90 pauseWhenHostLowPower: resolved.pauseWhenHostLowPower,
91 pauseWhenClientLowPower: resolved.pauseWhenClientLowPower,
92 pauseWhenOnBattery: resolved.pauseWhenOnBattery,
93 },
94 });
95 return normalized.profile === "custom" ? "advanced" : normalized.profile;
96}
97
98export function backgroundActivitySharedPolicySettings(
99 settings: ServerSettings,
100 profile: BackgroundActivityProfile,
101): BackgroundActivitySettings {
102 const normalized = normalizeServerBackgroundActivitySettings(settings);
103 return {
104 schemaVersion: 1,
105 profile: "custom",
106 baseProfile: profile,
107 overrides: normalized.profile === "custom" ? normalized.overrides : {},
108 };
109}
110
111function collapseOtelSignalsUrl(input: {
112 readonly tracesUrl: string;
113 readonly metricsUrl: string;
114}): string | null {
115 const tracesSuffix = "/traces";
116 const metricsSuffix = "/metrics";
117 if (!input.tracesUrl.endsWith(tracesSuffix) || !input.metricsUrl.endsWith(metricsSuffix)) {
118 return null;
119 }
120
121 const tracesBase = input.tracesUrl.slice(0, -tracesSuffix.length);
122 const metricsBase = input.metricsUrl.slice(0, -metricsSuffix.length);
123 if (tracesBase !== metricsBase) {
124 return null;
125 }
126
127 return `${tracesBase}/{traces,metrics}`;
128}
129
130export function formatDiagnosticsDescription(input: {
131 readonly localTracingEnabled: boolean;
132 readonly otlpTracesEnabled: boolean;
133 readonly otlpTracesUrl?: string | undefined;
134 readonly otlpMetricsEnabled: boolean;
135 readonly otlpMetricsUrl?: string | undefined;
136}): string {
137 const mode = input.localTracingEnabled ? "Local trace file" : "Terminal logs only";
138 const tracesUrl = input.otlpTracesEnabled ? input.otlpTracesUrl : undefined;
139 const metricsUrl = input.otlpMetricsEnabled ? input.otlpMetricsUrl : undefined;
140
141 if (tracesUrl && metricsUrl) {
142 const collapsedUrl = collapseOtelSignalsUrl({ tracesUrl, metricsUrl });
143 return collapsedUrl
144 ? `${mode}. Exporting OTEL to ${collapsedUrl}.`
145 : `${mode}. Exporting OTEL traces to ${tracesUrl} and metrics to ${metricsUrl}.`;
146 }
147
148 if (tracesUrl) {
149 return `${mode}. Exporting OTEL traces to ${tracesUrl}.`;
150 }
151
152 if (metricsUrl) {
153 return `${mode}. Exporting OTEL metrics to ${metricsUrl}.`;
154 }
155
156 return `${mode}.`;
157}
158
159export function buildProviderInstanceUpdatePatch(input: {
160 readonly settings: Pick<ServerSettings, "providers" | "providerInstances">;
161 readonly instanceId: ProviderInstanceId;
162 readonly instance: ProviderInstanceConfig;
163 readonly driver: ProviderDriverKind;
164 readonly isDefault: boolean;
165 readonly textGenerationModelSelection?:
166 | ServerSettings["textGenerationModelSelection"]
167 | undefined;
168}): Partial<UnifiedSettings> {
169 type LegacyProviderSettings = ServerSettings["providers"][keyof ServerSettings["providers"]];
170 const legacyProviderDefaults = DEFAULT_UNIFIED_SETTINGS.providers as Record<
171 string,
172 LegacyProviderSettings | undefined
173 >;
174 const legacyProviderDefault = input.isDefault ? legacyProviderDefaults[input.driver] : undefined;
175 return {
176 ...(legacyProviderDefault !== undefined
177 ? {
178 providers: {
179 ...input.settings.providers,
180 [input.driver]: legacyProviderDefault,
181 } as ServerSettings["providers"],
182 }
183 : {}),
184 providerInstances: {
185 ...input.settings.providerInstances,
186 [input.instanceId]: input.instance,
187 },
188 ...(input.textGenerationModelSelection !== undefined
189 ? { textGenerationModelSelection: input.textGenerationModelSelection }
190 : {}),
191 };
192}