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, 04:45 UTC
| 1 | import type { |
| 2 | BackgroundActivityProfile, |
| 3 | BackgroundActivitySettings, |
| 4 | ProviderInstanceConfig, |
| 5 | PreviewViewportSetting, |
| 6 | ProviderInstanceId, |
| 7 | ServerSettings, |
| 8 | SidebarProjectGroupingMode, |
| 9 | UnifiedSettings, |
| 10 | } from "@t3tools/contracts"; |
| 11 | import { |
| 12 | DEFAULT_UNIFIED_SETTINGS, |
| 13 | MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS, |
| 14 | MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS, |
| 15 | } from "@t3tools/contracts/settings"; |
| 16 | import { |
| 17 | getBackgroundActivityBaseProfile, |
| 18 | normalizeBackgroundActivitySettings, |
| 19 | normalizeServerBackgroundActivitySettings, |
| 20 | resolveServerBackgroundActivitySettings, |
| 21 | } from "@t3tools/shared/backgroundActivitySettings"; |
| 22 | import * as Duration from "effect/Duration"; |
| 23 | import * as Equal from "effect/Equal"; |
| 24 | import { msg } from "gt-react"; |
| 25 | |
| 26 | import { m, type MessageResolver } from "../../i18n/messages"; |
| 27 | |
| 28 | export function isProjectGroupingEnabled(mode: SidebarProjectGroupingMode): boolean { |
| 29 | return mode !== "separate"; |
| 30 | } |
| 31 | |
| 32 | export function projectGroupingModeFromToggle( |
| 33 | enabled: boolean, |
| 34 | lastEnabledMode: SidebarProjectGroupingMode = "repository", |
| 35 | ): SidebarProjectGroupingMode { |
| 36 | if (!enabled) return "separate"; |
| 37 | return lastEnabledMode === "repository_path" ? "repository_path" : "repository"; |
| 38 | } |
| 39 | |
| 40 | const LAST_ENABLED_PROJECT_GROUPING_MODE_KEY = "t3code:last-enabled-project-grouping-mode"; |
| 41 | |
| 42 | export function readLastEnabledProjectGroupingMode(): SidebarProjectGroupingMode { |
| 43 | try { |
| 44 | return localStorage.getItem(LAST_ENABLED_PROJECT_GROUPING_MODE_KEY) === "repository_path" |
| 45 | ? "repository_path" |
| 46 | : "repository"; |
| 47 | } catch { |
| 48 | return "repository"; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | export function rememberEnabledProjectGroupingMode(mode: SidebarProjectGroupingMode): void { |
| 53 | if (mode === "separate") return; |
| 54 | try { |
| 55 | localStorage.setItem(LAST_ENABLED_PROJECT_GROUPING_MODE_KEY, mode); |
| 56 | } catch {} |
| 57 | } |
| 58 | |
| 59 | export function hasChangedBackgroundActivitySettings( |
| 60 | settings: Pick< |
| 61 | UnifiedSettings, |
| 62 | | "backgroundActivity" |
| 63 | | "backgroundActivityProfile" |
| 64 | | "automaticGitFetchInterval" |
| 65 | | "providerHealthRefreshInterval" |
| 66 | >, |
| 67 | ): boolean { |
| 68 | return ( |
| 69 | !Equal.equals(settings.backgroundActivity, DEFAULT_UNIFIED_SETTINGS.backgroundActivity) || |
| 70 | settings.backgroundActivityProfile !== DEFAULT_UNIFIED_SETTINGS.backgroundActivityProfile || |
| 71 | !Equal.equals( |
| 72 | settings.automaticGitFetchInterval, |
| 73 | DEFAULT_UNIFIED_SETTINGS.automaticGitFetchInterval, |
| 74 | ) || |
| 75 | !Equal.equals( |
| 76 | settings.providerHealthRefreshInterval, |
| 77 | DEFAULT_UNIFIED_SETTINGS.providerHealthRefreshInterval, |
| 78 | ) |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | type TypographySettings = Pick< |
| 83 | UnifiedSettings, |
| 84 | | "fontFamilySans" |
| 85 | | "fontFamilyComposer" |
| 86 | | "fontFamilyCode" |
| 87 | | "fontFamilyTerminal" |
| 88 | | "fontSizeInterface" |
| 89 | | "fontSizePrompt" |
| 90 | | "fontSizeCode" |
| 91 | | "fontSizeTerminal" |
| 92 | >; |
| 93 | |
| 94 | export function getChangedTypographySettingLabels( |
| 95 | settings: TypographySettings, |
| 96 | resolve: MessageResolver = m, |
| 97 | ): string[] { |
| 98 | return [ |
| 99 | ...(settings.fontFamilySans !== DEFAULT_UNIFIED_SETTINGS.fontFamilySans || |
| 100 | settings.fontSizeInterface !== DEFAULT_UNIFIED_SETTINGS.fontSizeInterface |
| 101 | ? [resolve(msg("Interface font"))] |
| 102 | : []), |
| 103 | ...(settings.fontFamilyComposer !== DEFAULT_UNIFIED_SETTINGS.fontFamilyComposer || |
| 104 | settings.fontSizePrompt !== DEFAULT_UNIFIED_SETTINGS.fontSizePrompt |
| 105 | ? [resolve(msg("Prompt font"))] |
| 106 | : []), |
| 107 | ...(settings.fontFamilyCode !== DEFAULT_UNIFIED_SETTINGS.fontFamilyCode || |
| 108 | settings.fontSizeCode !== DEFAULT_UNIFIED_SETTINGS.fontSizeCode |
| 109 | ? [resolve(msg("Code font"))] |
| 110 | : []), |
| 111 | ...(settings.fontFamilyTerminal !== DEFAULT_UNIFIED_SETTINGS.fontFamilyTerminal || |
| 112 | settings.fontSizeTerminal !== DEFAULT_UNIFIED_SETTINGS.fontSizeTerminal |
| 113 | ? [resolve(msg("Terminal font"))] |
| 114 | : []), |
| 115 | ]; |
| 116 | } |
| 117 | |
| 118 | export type BrowserDefaultSettings = Pick< |
| 119 | UnifiedSettings, |
| 120 | | "browserDefaultViewport" |
| 121 | | "browserDefaultZoomFactor" |
| 122 | | "browserDefaultAppearance" |
| 123 | | "browserRecordingFrameRate" |
| 124 | | "browserRecordingShowKeyPresses" |
| 125 | | "browserRecordingShowMousePresses" |
| 126 | | "browserLinkTarget" |
| 127 | | "browserSearchEngine" |
| 128 | | "browserCustomSearchUrl" |
| 129 | | "browserAutoShowFloatingPreview" |
| 130 | | "pullRequestLinksOpenInApp" |
| 131 | >; |
| 132 | |
| 133 | function isSamePreviewViewport( |
| 134 | left: PreviewViewportSetting, |
| 135 | right: PreviewViewportSetting, |
| 136 | ): boolean { |
| 137 | if (left._tag !== right._tag) return false; |
| 138 | if (left._tag === "fill" || right._tag === "fill") return true; |
| 139 | if (left.width !== right.width || left.height !== right.height) return false; |
| 140 | return left._tag === "preset" && right._tag === "preset" |
| 141 | ? left.presetId === right.presetId |
| 142 | : true; |
| 143 | } |
| 144 | |
| 145 | export function getChangedBrowserSettingLabels( |
| 146 | settings: BrowserDefaultSettings, |
| 147 | resolve: MessageResolver = m, |
| 148 | ): string[] { |
| 149 | return [ |
| 150 | ...(isSamePreviewViewport( |
| 151 | settings.browserDefaultViewport, |
| 152 | DEFAULT_UNIFIED_SETTINGS.browserDefaultViewport, |
| 153 | ) |
| 154 | ? [] |
| 155 | : [resolve(msg("Browser viewport"))]), |
| 156 | ...(settings.browserDefaultZoomFactor !== DEFAULT_UNIFIED_SETTINGS.browserDefaultZoomFactor |
| 157 | ? [resolve(msg("Browser zoom"))] |
| 158 | : []), |
| 159 | ...(settings.browserDefaultAppearance !== DEFAULT_UNIFIED_SETTINGS.browserDefaultAppearance |
| 160 | ? [resolve(msg("Browser appearance"))] |
| 161 | : []), |
| 162 | ...(settings.browserRecordingFrameRate !== DEFAULT_UNIFIED_SETTINGS.browserRecordingFrameRate |
| 163 | ? [resolve(msg("Recording frame rate"))] |
| 164 | : []), |
| 165 | ...(settings.browserRecordingShowKeyPresses ? [resolve(msg("Recording key presses"))] : []), |
| 166 | ...(settings.browserRecordingShowMousePresses ? [resolve(msg("Recording mouse presses"))] : []), |
| 167 | ...(settings.browserLinkTarget !== DEFAULT_UNIFIED_SETTINGS.browserLinkTarget |
| 168 | ? [resolve(msg("Open links in"))] |
| 169 | : []), |
| 170 | ...(settings.browserSearchEngine !== DEFAULT_UNIFIED_SETTINGS.browserSearchEngine || |
| 171 | settings.browserCustomSearchUrl !== DEFAULT_UNIFIED_SETTINGS.browserCustomSearchUrl |
| 172 | ? [resolve(msg("Search engine"))] |
| 173 | : []), |
| 174 | ...(settings.browserAutoShowFloatingPreview !== |
| 175 | DEFAULT_UNIFIED_SETTINGS.browserAutoShowFloatingPreview |
| 176 | ? [resolve(msg("Floating preview"))] |
| 177 | : []), |
| 178 | ...(settings.pullRequestLinksOpenInApp !== DEFAULT_UNIFIED_SETTINGS.pullRequestLinksOpenInApp |
| 179 | ? [resolve(msg("Open pull requests in T3 Code"))] |
| 180 | : []), |
| 181 | ]; |
| 182 | } |
| 183 | |
| 184 | export function resolveBackgroundActivityProfileOption( |
| 185 | settings: ServerSettings, |
| 186 | ): BackgroundActivityProfile | "advanced" { |
| 187 | const resolved = resolveServerBackgroundActivitySettings(settings); |
| 188 | const normalized = normalizeBackgroundActivitySettings({ |
| 189 | schemaVersion: 1, |
| 190 | profile: "custom", |
| 191 | baseProfile: resolved.profile, |
| 192 | overrides: { |
| 193 | automaticGitFetchInterval: resolved.automaticGitFetchInterval, |
| 194 | providerHealthRefreshInterval: resolved.providerHealthRefreshInterval, |
| 195 | hostPowerMonitorActiveInterval: resolved.hostPowerMonitorActiveInterval, |
| 196 | hostPowerMonitorIdleInterval: resolved.hostPowerMonitorIdleInterval, |
| 197 | pauseWhenHostLocked: resolved.pauseWhenHostLocked, |
| 198 | pauseWhenHostLowPower: resolved.pauseWhenHostLowPower, |
| 199 | pauseWhenClientLowPower: resolved.pauseWhenClientLowPower, |
| 200 | pauseWhenOnBattery: resolved.pauseWhenOnBattery, |
| 201 | }, |
| 202 | }); |
| 203 | return normalized.profile === "custom" ? "advanced" : normalized.profile; |
| 204 | } |
| 205 | |
| 206 | export function backgroundActivitySharedPolicySettings( |
| 207 | settings: ServerSettings, |
| 208 | profile: BackgroundActivityProfile, |
| 209 | ): BackgroundActivitySettings { |
| 210 | const normalized = normalizeServerBackgroundActivitySettings(settings); |
| 211 | return { |
| 212 | schemaVersion: 1, |
| 213 | profile: "custom", |
| 214 | baseProfile: profile, |
| 215 | overrides: normalized.profile === "custom" ? normalized.overrides : {}, |
| 216 | }; |
| 217 | } |
| 218 | |
| 219 | export function buildProviderInstanceUpdatePatch(input: { |
| 220 | readonly settings: Pick<ServerSettings, "providerInstances">; |
| 221 | readonly instanceId: ProviderInstanceId; |
| 222 | readonly instance: ProviderInstanceConfig; |
| 223 | readonly textGenerationModelSelection?: |
| 224 | | ServerSettings["textGenerationModelSelection"] |
| 225 | | undefined; |
| 226 | }): Partial<UnifiedSettings> { |
| 227 | return { |
| 228 | providerInstances: { |
| 229 | ...input.settings.providerInstances, |
| 230 | [input.instanceId]: input.instance, |
| 231 | }, |
| 232 | ...(input.textGenerationModelSelection !== undefined |
| 233 | ? { textGenerationModelSelection: input.textGenerationModelSelection } |
| 234 | : {}), |
| 235 | }; |
| 236 | } |
| 237 | |
| 238 | export const PROVIDER_HEALTH_INTERVAL_STEP_SECONDS = 30; |
| 239 | |
| 240 | type BackgroundActivityOverridePatch = Partial<{ |
| 241 | [K in keyof BackgroundActivitySettings["overrides"]]: |
| 242 | | BackgroundActivitySettings["overrides"][K] |
| 243 | | undefined; |
| 244 | }>; |
| 245 | |
| 246 | export function durationToSeconds(duration: Duration.Duration): number { |
| 247 | return Math.round(Duration.toMillis(duration) / 1_000); |
| 248 | } |
| 249 | |
| 250 | export function normalizeIntervalSeconds(value: number | null, minimum = 0): number { |
| 251 | if (value === null || !Number.isFinite(value)) { |
| 252 | return minimum; |
| 253 | } |
| 254 | return Math.max(minimum, Math.round(value)); |
| 255 | } |
| 256 | |
| 257 | export function backgroundActivityOverrideSettings( |
| 258 | current: BackgroundActivitySettings, |
| 259 | resolved: ReturnType<typeof resolveServerBackgroundActivitySettings>, |
| 260 | overrides: BackgroundActivityOverridePatch, |
| 261 | ) { |
| 262 | const nextOverrides: BackgroundActivityOverridePatch = { |
| 263 | automaticGitFetchInterval: resolved.automaticGitFetchInterval, |
| 264 | providerHealthRefreshInterval: resolved.providerHealthRefreshInterval, |
| 265 | hostPowerMonitorActiveInterval: resolved.hostPowerMonitorActiveInterval, |
| 266 | hostPowerMonitorIdleInterval: resolved.hostPowerMonitorIdleInterval, |
| 267 | pauseWhenHostLocked: resolved.pauseWhenHostLocked, |
| 268 | pauseWhenHostLowPower: resolved.pauseWhenHostLowPower, |
| 269 | pauseWhenClientLowPower: resolved.pauseWhenClientLowPower, |
| 270 | pauseWhenOnBattery: resolved.pauseWhenOnBattery, |
| 271 | ...overrides, |
| 272 | }; |
| 273 | for (const [key, value] of Object.entries(nextOverrides)) { |
| 274 | if (value === undefined) { |
| 275 | delete nextOverrides[key as keyof typeof nextOverrides]; |
| 276 | } |
| 277 | } |
| 278 | return { |
| 279 | backgroundActivity: { |
| 280 | schemaVersion: 1 as const, |
| 281 | profile: "custom" as const, |
| 282 | baseProfile: getBackgroundActivityBaseProfile(current), |
| 283 | overrides: nextOverrides as BackgroundActivitySettings["overrides"], |
| 284 | }, |
| 285 | }; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Saving a shorter age settles threads at once, so typing stays in `draft` until the edit is |
| 290 | * finished. `pending` is the submission in flight; `saved` is the only value a draft falls back to. |
| 291 | */ |
| 292 | export interface AutoSettleDaysField { |
| 293 | readonly saved: number; |
| 294 | readonly draft: string; |
| 295 | readonly dirty: boolean; |
| 296 | readonly pending: number | null; |
| 297 | readonly revision: number; |
| 298 | } |
| 299 | |
| 300 | export function autoSettleDaysField(saved: number): AutoSettleDaysField { |
| 301 | return { saved, draft: String(saved), dirty: false, pending: null, revision: 0 }; |
| 302 | } |
| 303 | |
| 304 | export function syncAutoSettleDaysField( |
| 305 | field: AutoSettleDaysField, |
| 306 | saved: number, |
| 307 | ): AutoSettleDaysField { |
| 308 | // An earlier save can be confirmed while a later one is still in flight. |
| 309 | const pending = field.pending === saved ? null : field.pending; |
| 310 | if (field.saved === saved && pending === field.pending) return field; |
| 311 | return { |
| 312 | ...field, |
| 313 | saved, |
| 314 | pending, |
| 315 | draft: field.dirty ? field.draft : String(pending ?? saved), |
| 316 | }; |
| 317 | } |
| 318 | |
| 319 | export function editAutoSettleDaysField( |
| 320 | field: AutoSettleDaysField, |
| 321 | draft: string, |
| 322 | ): AutoSettleDaysField { |
| 323 | return { ...field, draft, dirty: true }; |
| 324 | } |
| 325 | |
| 326 | export function cancelAutoSettleDaysField(field: AutoSettleDaysField): AutoSettleDaysField { |
| 327 | return { ...field, draft: String(field.pending ?? field.saved), dirty: false }; |
| 328 | } |
| 329 | |
| 330 | export function commitAutoSettleDaysField(field: AutoSettleDaysField): { |
| 331 | readonly field: AutoSettleDaysField; |
| 332 | readonly submit: number | null; |
| 333 | } { |
| 334 | const current = field.pending ?? field.saved; |
| 335 | const days = field.draft.trim() === "" ? Number.NaN : Number(field.draft); |
| 336 | if ( |
| 337 | !Number.isInteger(days) || |
| 338 | days < MIN_SIDEBAR_AUTO_SETTLE_AFTER_DAYS || |
| 339 | days > MAX_SIDEBAR_AUTO_SETTLE_AFTER_DAYS |
| 340 | ) { |
| 341 | return { field: { ...field, draft: String(current), dirty: false }, submit: null }; |
| 342 | } |
| 343 | const settled = { ...field, draft: String(days), dirty: false }; |
| 344 | // An edit ending on the saved value is sent again: a save can land on the shown environment |
| 345 | // and fail on another, and retyping the number is how it is retried. |
| 346 | if (!field.dirty || days === field.pending) return { field: settled, submit: null }; |
| 347 | return { field: { ...settled, pending: days, revision: field.revision + 1 }, submit: days }; |
| 348 | } |
| 349 | |
| 350 | export function failAutoSettleDaysField( |
| 351 | field: AutoSettleDaysField, |
| 352 | revision: number, |
| 353 | ): AutoSettleDaysField { |
| 354 | if (field.revision !== revision) return field; |
| 355 | return { ...field, pending: null, draft: field.dirty ? field.draft : String(field.saved) }; |
| 356 | } |