The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { |
| 2 | type BackgroundActivityProfile, |
| 3 | type BackgroundActivitySettings, |
| 4 | DEFAULT_BACKGROUND_ACTIVITY_PROFILE, |
| 5 | DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL, |
| 6 | DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL, |
| 7 | type ServerSettings, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import * as Duration from "effect/Duration"; |
| 10 | |
| 11 | export interface ResolvedBackgroundActivitySettings { |
| 12 | readonly profile: BackgroundActivityProfile; |
| 13 | readonly automaticGitFetchInterval: Duration.Duration; |
| 14 | readonly providerHealthRefreshInterval: Duration.Duration; |
| 15 | readonly hostPowerMonitorActiveInterval: Duration.Duration; |
| 16 | readonly hostPowerMonitorIdleInterval: Duration.Duration; |
| 17 | readonly pauseWhenHostLocked: boolean; |
| 18 | readonly pauseWhenHostLowPower: boolean; |
| 19 | readonly pauseWhenClientLowPower: boolean; |
| 20 | readonly pauseWhenOnBattery: boolean; |
| 21 | } |
| 22 | |
| 23 | const PRESET_SETTINGS: Record<BackgroundActivityProfile, ResolvedBackgroundActivitySettings> = { |
| 24 | performance: { |
| 25 | profile: "performance", |
| 26 | automaticGitFetchInterval: Duration.seconds(15), |
| 27 | providerHealthRefreshInterval: Duration.minutes(1), |
| 28 | hostPowerMonitorActiveInterval: Duration.seconds(30), |
| 29 | hostPowerMonitorIdleInterval: Duration.minutes(2), |
| 30 | pauseWhenHostLocked: true, |
| 31 | pauseWhenHostLowPower: false, |
| 32 | pauseWhenClientLowPower: false, |
| 33 | pauseWhenOnBattery: false, |
| 34 | }, |
| 35 | balanced: { |
| 36 | profile: "balanced", |
| 37 | automaticGitFetchInterval: DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL, |
| 38 | providerHealthRefreshInterval: DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL, |
| 39 | hostPowerMonitorActiveInterval: Duration.seconds(30), |
| 40 | hostPowerMonitorIdleInterval: Duration.minutes(5), |
| 41 | pauseWhenHostLocked: true, |
| 42 | pauseWhenHostLowPower: true, |
| 43 | pauseWhenClientLowPower: true, |
| 44 | pauseWhenOnBattery: false, |
| 45 | }, |
| 46 | "battery-saver": { |
| 47 | profile: "battery-saver", |
| 48 | automaticGitFetchInterval: Duration.seconds(0), |
| 49 | providerHealthRefreshInterval: Duration.minutes(15), |
| 50 | hostPowerMonitorActiveInterval: Duration.minutes(1), |
| 51 | hostPowerMonitorIdleInterval: Duration.minutes(10), |
| 52 | pauseWhenHostLocked: true, |
| 53 | pauseWhenHostLowPower: true, |
| 54 | pauseWhenClientLowPower: true, |
| 55 | pauseWhenOnBattery: true, |
| 56 | }, |
| 57 | }; |
| 58 | |
| 59 | export function getBackgroundActivityPresetSettings( |
| 60 | profile: BackgroundActivityProfile, |
| 61 | ): ResolvedBackgroundActivitySettings { |
| 62 | return PRESET_SETTINGS[profile]; |
| 63 | } |
| 64 | |
| 65 | export function getBackgroundActivityBaseProfile( |
| 66 | backgroundActivity: BackgroundActivitySettings, |
| 67 | ): BackgroundActivityProfile { |
| 68 | if (backgroundActivity.profile === "custom") { |
| 69 | return backgroundActivity.baseProfile ?? DEFAULT_BACKGROUND_ACTIVITY_PROFILE; |
| 70 | } |
| 71 | return backgroundActivity.profile; |
| 72 | } |
| 73 | |
| 74 | export function resolveBackgroundActivitySettings( |
| 75 | backgroundActivity: BackgroundActivitySettings, |
| 76 | ): ResolvedBackgroundActivitySettings { |
| 77 | const baseProfile = getBackgroundActivityBaseProfile(backgroundActivity); |
| 78 | const preset = PRESET_SETTINGS[baseProfile]; |
| 79 | const overrides = backgroundActivity.profile === "custom" ? backgroundActivity.overrides : {}; |
| 80 | return { |
| 81 | profile: baseProfile, |
| 82 | automaticGitFetchInterval: |
| 83 | overrides.automaticGitFetchInterval ?? preset.automaticGitFetchInterval, |
| 84 | providerHealthRefreshInterval: |
| 85 | overrides.providerHealthRefreshInterval ?? preset.providerHealthRefreshInterval, |
| 86 | hostPowerMonitorActiveInterval: |
| 87 | overrides.hostPowerMonitorActiveInterval ?? preset.hostPowerMonitorActiveInterval, |
| 88 | hostPowerMonitorIdleInterval: |
| 89 | overrides.hostPowerMonitorIdleInterval ?? preset.hostPowerMonitorIdleInterval, |
| 90 | pauseWhenHostLocked: overrides.pauseWhenHostLocked ?? preset.pauseWhenHostLocked, |
| 91 | pauseWhenHostLowPower: overrides.pauseWhenHostLowPower ?? preset.pauseWhenHostLowPower, |
| 92 | pauseWhenClientLowPower: overrides.pauseWhenClientLowPower ?? preset.pauseWhenClientLowPower, |
| 93 | pauseWhenOnBattery: overrides.pauseWhenOnBattery ?? preset.pauseWhenOnBattery, |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | function durationsEqual(a: Duration.Duration, b: Duration.Duration): boolean { |
| 98 | return Duration.toMillis(a) === Duration.toMillis(b); |
| 99 | } |
| 100 | |
| 101 | function resolvedSettingsEqual( |
| 102 | a: ResolvedBackgroundActivitySettings, |
| 103 | b: ResolvedBackgroundActivitySettings, |
| 104 | ): boolean { |
| 105 | return ( |
| 106 | durationsEqual(a.automaticGitFetchInterval, b.automaticGitFetchInterval) && |
| 107 | durationsEqual(a.providerHealthRefreshInterval, b.providerHealthRefreshInterval) && |
| 108 | durationsEqual(a.hostPowerMonitorActiveInterval, b.hostPowerMonitorActiveInterval) && |
| 109 | durationsEqual(a.hostPowerMonitorIdleInterval, b.hostPowerMonitorIdleInterval) && |
| 110 | a.pauseWhenHostLocked === b.pauseWhenHostLocked && |
| 111 | a.pauseWhenHostLowPower === b.pauseWhenHostLowPower && |
| 112 | a.pauseWhenClientLowPower === b.pauseWhenClientLowPower && |
| 113 | a.pauseWhenOnBattery === b.pauseWhenOnBattery |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | export function normalizeBackgroundActivitySettings( |
| 118 | backgroundActivity: BackgroundActivitySettings, |
| 119 | ): BackgroundActivitySettings { |
| 120 | if (backgroundActivity.profile !== "custom") { |
| 121 | return { |
| 122 | schemaVersion: 1, |
| 123 | profile: backgroundActivity.profile, |
| 124 | overrides: {}, |
| 125 | }; |
| 126 | } |
| 127 | |
| 128 | const resolved = resolveBackgroundActivitySettings(backgroundActivity); |
| 129 | const profiles: ReadonlyArray<BackgroundActivityProfile> = [ |
| 130 | getBackgroundActivityBaseProfile(backgroundActivity), |
| 131 | "balanced", |
| 132 | "performance", |
| 133 | "battery-saver", |
| 134 | ]; |
| 135 | for (const profile of profiles) { |
| 136 | if (resolvedSettingsEqual(resolved, PRESET_SETTINGS[profile])) { |
| 137 | return { |
| 138 | schemaVersion: 1, |
| 139 | profile, |
| 140 | overrides: {}, |
| 141 | }; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | const baseProfile = getBackgroundActivityBaseProfile(backgroundActivity); |
| 146 | const preset = PRESET_SETTINGS[baseProfile]; |
| 147 | const overrides: BackgroundActivitySettings["overrides"] = { |
| 148 | ...(!durationsEqual(resolved.automaticGitFetchInterval, preset.automaticGitFetchInterval) |
| 149 | ? { automaticGitFetchInterval: resolved.automaticGitFetchInterval } |
| 150 | : {}), |
| 151 | ...(!durationsEqual( |
| 152 | resolved.providerHealthRefreshInterval, |
| 153 | preset.providerHealthRefreshInterval, |
| 154 | ) |
| 155 | ? { providerHealthRefreshInterval: resolved.providerHealthRefreshInterval } |
| 156 | : {}), |
| 157 | ...(!durationsEqual( |
| 158 | resolved.hostPowerMonitorActiveInterval, |
| 159 | preset.hostPowerMonitorActiveInterval, |
| 160 | ) |
| 161 | ? { hostPowerMonitorActiveInterval: resolved.hostPowerMonitorActiveInterval } |
| 162 | : {}), |
| 163 | ...(!durationsEqual(resolved.hostPowerMonitorIdleInterval, preset.hostPowerMonitorIdleInterval) |
| 164 | ? { hostPowerMonitorIdleInterval: resolved.hostPowerMonitorIdleInterval } |
| 165 | : {}), |
| 166 | ...(resolved.pauseWhenHostLocked !== preset.pauseWhenHostLocked |
| 167 | ? { pauseWhenHostLocked: resolved.pauseWhenHostLocked } |
| 168 | : {}), |
| 169 | ...(resolved.pauseWhenHostLowPower !== preset.pauseWhenHostLowPower |
| 170 | ? { pauseWhenHostLowPower: resolved.pauseWhenHostLowPower } |
| 171 | : {}), |
| 172 | ...(resolved.pauseWhenClientLowPower !== preset.pauseWhenClientLowPower |
| 173 | ? { pauseWhenClientLowPower: resolved.pauseWhenClientLowPower } |
| 174 | : {}), |
| 175 | ...(resolved.pauseWhenOnBattery !== preset.pauseWhenOnBattery |
| 176 | ? { pauseWhenOnBattery: resolved.pauseWhenOnBattery } |
| 177 | : {}), |
| 178 | }; |
| 179 | |
| 180 | return { |
| 181 | schemaVersion: 1, |
| 182 | profile: "custom", |
| 183 | baseProfile, |
| 184 | overrides, |
| 185 | }; |
| 186 | } |
| 187 | |
| 188 | export function resolveServerBackgroundActivitySettings( |
| 189 | settings: ServerSettings, |
| 190 | ): ResolvedBackgroundActivitySettings { |
| 191 | const defaultBackgroundActivity: BackgroundActivitySettings = { |
| 192 | schemaVersion: 1, |
| 193 | profile: DEFAULT_BACKGROUND_ACTIVITY_PROFILE, |
| 194 | overrides: {}, |
| 195 | }; |
| 196 | const backgroundActivityIsDefault = |
| 197 | settings.backgroundActivity.profile === defaultBackgroundActivity.profile && |
| 198 | settings.backgroundActivity.baseProfile === undefined && |
| 199 | Object.keys(settings.backgroundActivity.overrides).length === 0; |
| 200 | const legacyProfile = settings.backgroundActivityProfile; |
| 201 | const hasLegacyOverrides = |
| 202 | legacyProfile !== DEFAULT_BACKGROUND_ACTIVITY_PROFILE || |
| 203 | Duration.toMillis(settings.automaticGitFetchInterval) !== |
| 204 | Duration.toMillis(DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL) || |
| 205 | Duration.toMillis(settings.providerHealthRefreshInterval) !== |
| 206 | Duration.toMillis(DEFAULT_PROVIDER_HEALTH_REFRESH_INTERVAL); |
| 207 | if (backgroundActivityIsDefault && hasLegacyOverrides) { |
| 208 | return resolveBackgroundActivitySettings({ |
| 209 | schemaVersion: 1, |
| 210 | profile: |
| 211 | Duration.toMillis(settings.automaticGitFetchInterval) === |
| 212 | Duration.toMillis( |
| 213 | getBackgroundActivityPresetSettings(legacyProfile).automaticGitFetchInterval, |
| 214 | ) && |
| 215 | Duration.toMillis(settings.providerHealthRefreshInterval) === |
| 216 | Duration.toMillis( |
| 217 | getBackgroundActivityPresetSettings(legacyProfile).providerHealthRefreshInterval, |
| 218 | ) |
| 219 | ? legacyProfile |
| 220 | : "custom", |
| 221 | baseProfile: legacyProfile, |
| 222 | overrides: { |
| 223 | ...(Duration.toMillis(settings.automaticGitFetchInterval) !== |
| 224 | Duration.toMillis( |
| 225 | getBackgroundActivityPresetSettings(legacyProfile).automaticGitFetchInterval, |
| 226 | ) |
| 227 | ? { automaticGitFetchInterval: settings.automaticGitFetchInterval } |
| 228 | : {}), |
| 229 | ...(Duration.toMillis(settings.providerHealthRefreshInterval) !== |
| 230 | Duration.toMillis( |
| 231 | getBackgroundActivityPresetSettings(legacyProfile).providerHealthRefreshInterval, |
| 232 | ) |
| 233 | ? { providerHealthRefreshInterval: settings.providerHealthRefreshInterval } |
| 234 | : {}), |
| 235 | }, |
| 236 | }); |
| 237 | } |
| 238 | return resolveBackgroundActivitySettings(settings.backgroundActivity); |
| 239 | } |
| 240 | |
| 241 | export function normalizeServerBackgroundActivitySettings( |
| 242 | settings: ServerSettings, |
| 243 | ): BackgroundActivitySettings { |
| 244 | const resolved = resolveServerBackgroundActivitySettings(settings); |
| 245 | return normalizeBackgroundActivitySettings({ |
| 246 | schemaVersion: 1, |
| 247 | profile: "custom", |
| 248 | baseProfile: resolved.profile, |
| 249 | overrides: { |
| 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 | }, |
| 259 | }); |
| 260 | } |