|
| 1 | +import { asNullableRecord as asRecord } from "@openclaw/normalization-core/record-coerce"; |
| 2 | +import { normalizeSidebarEntries } from "../app-navigation.ts"; |
| 3 | +import { isSupportedLocale } from "../i18n/index.ts"; |
| 4 | +import { |
| 5 | + normalizeChatFollowUpModeOverride, |
| 6 | + normalizeChatSendShortcut, |
| 7 | + UI_APPEARANCE_DEFAULTS, |
| 8 | + type ChatFollowUpMode, |
| 9 | + type ChatSendShortcut, |
| 10 | + type UiSettings, |
| 11 | +} from "./settings.ts"; |
| 12 | +import type { ThemeMode, ThemeName } from "./theme.ts"; |
| 13 | + |
| 14 | +const THEMES: ReadonlySet<ThemeName> = new Set(["claw", "knot", "dash", "custom"]); |
| 15 | +const THEME_MODES: ReadonlySet<ThemeMode> = new Set(["light", "dark", "system"]); |
| 16 | + |
| 17 | +type SyncedPrefSpec<T> = { |
| 18 | + extract: (value: unknown) => T | undefined; |
| 19 | + local: (settings: UiSettings) => T | undefined; |
| 20 | + write?: (value: T | undefined) => Partial<UiSettings>; |
| 21 | + canApply?: (value: T, settings: UiSettings) => boolean; |
| 22 | + clearable?: boolean; |
| 23 | + reset?: (settings: UiSettings) => Partial<UiSettings>; |
| 24 | +}; |
| 25 | + |
| 26 | +const prefSpec = <T>(specification: SyncedPrefSpec<T>) => specification; |
| 27 | + |
| 28 | +/** |
| 29 | + * One descriptor per synced pref: the source of truth for config ui.prefs. |
| 30 | + * Each key owns server validation, local normalization, and applicability. |
| 31 | + */ |
| 32 | +export const SYNCED_PREFS = { |
| 33 | + theme: prefSpec<ThemeName>({ |
| 34 | + extract: (value) => (THEMES.has(value as ThemeName) ? (value as ThemeName) : undefined), |
| 35 | + local: (settings) => settings.theme, |
| 36 | + write: (value) => ({ theme: value ?? UI_APPEARANCE_DEFAULTS.theme }), |
| 37 | + clearable: true, |
| 38 | + reset: () => ({ theme: UI_APPEARANCE_DEFAULTS.theme }), |
| 39 | + // A server "custom" theme is only honorable once this browser imported one; |
| 40 | + // the imported palette itself is too large to live in config. |
| 41 | + canApply: (value, settings) => value !== "custom" || Boolean(settings.customTheme), |
| 42 | + }), |
| 43 | + themeMode: prefSpec<ThemeMode>({ |
| 44 | + extract: (value) => (THEME_MODES.has(value as ThemeMode) ? (value as ThemeMode) : undefined), |
| 45 | + local: (settings) => settings.themeMode, |
| 46 | + write: (value) => ({ themeMode: value ?? UI_APPEARANCE_DEFAULTS.themeMode }), |
| 47 | + clearable: true, |
| 48 | + reset: () => ({ themeMode: UI_APPEARANCE_DEFAULTS.themeMode }), |
| 49 | + }), |
| 50 | + locale: prefSpec<string>({ |
| 51 | + extract: (value) => (typeof value === "string" && isSupportedLocale(value) ? value : undefined), |
| 52 | + local: (settings) => settings.locale, |
| 53 | + write: (value) => ({ locale: value }), |
| 54 | + clearable: true, |
| 55 | + reset: () => ({ locale: undefined }), |
| 56 | + }), |
| 57 | + chatShowThinking: prefSpec<boolean>({ |
| 58 | + extract: (value) => (typeof value === "boolean" ? value : undefined), |
| 59 | + local: (settings) => settings.chatShowThinking, |
| 60 | + }), |
| 61 | + chatShowToolCalls: prefSpec<boolean>({ |
| 62 | + extract: (value) => (typeof value === "boolean" ? value : undefined), |
| 63 | + local: (settings) => settings.chatShowToolCalls, |
| 64 | + }), |
| 65 | + chatPersistCommentary: prefSpec<boolean>({ |
| 66 | + extract: (value) => (typeof value === "boolean" ? value : undefined), |
| 67 | + local: (settings) => settings.chatPersistCommentary !== false, |
| 68 | + }), |
| 69 | + chatSendShortcut: prefSpec<ChatSendShortcut>({ |
| 70 | + extract: (value) => |
| 71 | + value === "enter" || value === "modifier-enter" |
| 72 | + ? normalizeChatSendShortcut(value) |
| 73 | + : undefined, |
| 74 | + local: (settings) => normalizeChatSendShortcut(settings.chatSendShortcut), |
| 75 | + write: (value) => ({ chatSendShortcut: value }), |
| 76 | + clearable: true, |
| 77 | + reset: () => ({ chatSendShortcut: undefined }), |
| 78 | + }), |
| 79 | + chatFollowUpMode: prefSpec<ChatFollowUpMode>({ |
| 80 | + extract: (value) => normalizeChatFollowUpModeOverride(value), |
| 81 | + local: (settings) => normalizeChatFollowUpModeOverride(settings.chatFollowUpMode), |
| 82 | + write: (value) => ({ chatFollowUpMode: value }), |
| 83 | + // Unset means "use the server-configured queue mode"; clearing must propagate, |
| 84 | + // so the push serializes an explicit null removal. |
| 85 | + clearable: true, |
| 86 | + reset: () => ({ chatFollowUpMode: undefined }), |
| 87 | + }), |
| 88 | + sidebarEntries: prefSpec<string[]>({ |
| 89 | + extract: (value) => normalizeSidebarEntries(value) ?? undefined, |
| 90 | + local: (settings) => settings.sidebarEntries, |
| 91 | + }), |
| 92 | +} as const; |
| 93 | + |
| 94 | +export type SyncedPrefKey = keyof typeof SYNCED_PREFS; |
| 95 | +export type ResettableServerUiPrefKey = |
| 96 | + | "theme" |
| 97 | + | "themeMode" |
| 98 | + | "locale" |
| 99 | + | "chatSendShortcut" |
| 100 | + | "chatFollowUpMode"; |
| 101 | +export type SyncedPrefValue<K extends SyncedPrefKey> = |
| 102 | + ReturnType<(typeof SYNCED_PREFS)[K]["extract"]> extends (infer T) | undefined ? T : never; |
| 103 | +export type ServerUiPrefs = { [K in SyncedPrefKey]?: SyncedPrefValue<K> | null }; |
| 104 | +export type ServerUiPrefProvenance = "default" | "pending" | "synced" | "device-local"; |
| 105 | +export type ServerUiPrefState<T> = { |
| 106 | + overridden: boolean; |
| 107 | + provenance: ServerUiPrefProvenance; |
| 108 | + resetValue: T | undefined; |
| 109 | + value: T | undefined; |
| 110 | +}; |
| 111 | + |
| 112 | +export const SYNCED_PREF_KEYS = Object.keys(SYNCED_PREFS) as SyncedPrefKey[]; |
| 113 | + |
| 114 | +export function prefValuesEqual(left: unknown, right: unknown): boolean { |
| 115 | + if (Array.isArray(left) && Array.isArray(right)) { |
| 116 | + return left.length === right.length && left.every((value, index) => value === right[index]); |
| 117 | + } |
| 118 | + return left === right; |
| 119 | +} |
| 120 | + |
| 121 | +export function extractServerUiPrefs(configObject: unknown): ServerUiPrefs { |
| 122 | + const prefs = asRecord(asRecord(asRecord(configObject)?.ui)?.prefs); |
| 123 | + if (!prefs) { |
| 124 | + return {}; |
| 125 | + } |
| 126 | + const result: ServerUiPrefs = {}; |
| 127 | + for (const key of SYNCED_PREF_KEYS) { |
| 128 | + const value = SYNCED_PREFS[key].extract(prefs[key]); |
| 129 | + if (value !== undefined) { |
| 130 | + (result as Record<string, unknown>)[key] = value; |
| 131 | + } |
| 132 | + } |
| 133 | + return result; |
| 134 | +} |
| 135 | + |
| 136 | +export function resolveServerUiPrefStateFromSnapshot<K extends SyncedPrefKey>( |
| 137 | + configObject: unknown, |
| 138 | + key: K, |
| 139 | + shadowPrefs: ServerUiPrefs | null, |
| 140 | + settings: UiSettings, |
| 141 | + canSync?: boolean | null, |
| 142 | +): ServerUiPrefState<SyncedPrefValue<K>> { |
| 143 | + const specification = SYNCED_PREFS[key]; |
| 144 | + const localValue = specification.local(settings) as SyncedPrefValue<K> | undefined; |
| 145 | + const resetPatch = specification.reset?.(settings); |
| 146 | + const productDefault = ( |
| 147 | + resetPatch ? specification.local({ ...settings, ...resetPatch }) : undefined |
| 148 | + ) as SyncedPrefValue<K> | undefined; |
| 149 | + const localState = ( |
| 150 | + resetValue: SyncedPrefValue<K> | undefined, |
| 151 | + ): ServerUiPrefState<SyncedPrefValue<K>> => { |
| 152 | + const overridden = !prefValuesEqual(localValue, resetValue); |
| 153 | + return { |
| 154 | + overridden, |
| 155 | + provenance: overridden ? "device-local" : "default", |
| 156 | + resetValue, |
| 157 | + value: localValue, |
| 158 | + }; |
| 159 | + }; |
| 160 | + const prefs = asRecord(asRecord(asRecord(configObject)?.ui)?.prefs); |
| 161 | + const serverValue = |
| 162 | + prefs && Object.hasOwn(prefs, key) |
| 163 | + ? (specification.extract(prefs[key]) as SyncedPrefValue<K> | undefined) |
| 164 | + : undefined; |
| 165 | + const canApplyServerValue = |
| 166 | + serverValue !== undefined && |
| 167 | + (!specification.canApply || |
| 168 | + (specification.canApply as (value: unknown, settings: UiSettings) => boolean)( |
| 169 | + serverValue, |
| 170 | + settings, |
| 171 | + )); |
| 172 | + const applicableServerValue = canApplyServerValue ? serverValue : productDefault; |
| 173 | + if (shadowPrefs && key in shadowPrefs) { |
| 174 | + if (canSync === false) { |
| 175 | + return { |
| 176 | + ...localState(applicableServerValue), |
| 177 | + // Keep queued intent for a later authorized reconnect without claiming |
| 178 | + // that this connected read-only browser is pending a server sync. |
| 179 | + provenance: "device-local", |
| 180 | + }; |
| 181 | + } |
| 182 | + const shadowValue = shadowPrefs[key]; |
| 183 | + if (shadowValue === null) { |
| 184 | + return { ...localState(productDefault), provenance: "pending" }; |
| 185 | + } |
| 186 | + return { |
| 187 | + overridden: true, |
| 188 | + provenance: "pending", |
| 189 | + resetValue: productDefault, |
| 190 | + value: shadowValue as SyncedPrefValue<K>, |
| 191 | + }; |
| 192 | + } |
| 193 | + if (!prefs || !Object.hasOwn(prefs, key) || serverValue === undefined) { |
| 194 | + return localState(productDefault); |
| 195 | + } |
| 196 | + if (!canApplyServerValue) { |
| 197 | + if (canSync === false) { |
| 198 | + return localState(productDefault); |
| 199 | + } |
| 200 | + // Preserve authored server provenance even when this browser cannot render |
| 201 | + // the value, so Restore default still removes the server override. |
| 202 | + return { |
| 203 | + overridden: true, |
| 204 | + provenance: "synced", |
| 205 | + resetValue: productDefault, |
| 206 | + value: localValue, |
| 207 | + }; |
| 208 | + } |
| 209 | + if (prefValuesEqual(localValue, serverValue)) { |
| 210 | + return { |
| 211 | + overridden: true, |
| 212 | + provenance: "synced", |
| 213 | + resetValue: productDefault, |
| 214 | + value: serverValue, |
| 215 | + }; |
| 216 | + } |
| 217 | + return localState(serverValue); |
| 218 | +} |
| 219 | + |
| 220 | +/** Local-settings patch that brings the browser mirror in line with the server. */ |
| 221 | +export function serverPrefsLocalPatch( |
| 222 | + prefs: ServerUiPrefs, |
| 223 | + settings: UiSettings, |
| 224 | +): Partial<UiSettings> | null { |
| 225 | + const patch: Partial<UiSettings> = {}; |
| 226 | + for (const key of SYNCED_PREF_KEYS) { |
| 227 | + const specification = SYNCED_PREFS[key]; |
| 228 | + const serverValue = prefs[key]; |
| 229 | + if (serverValue === undefined) { |
| 230 | + continue; |
| 231 | + } |
| 232 | + if (serverValue === null) { |
| 233 | + const resetPatch = specification.clearable ? specification.reset?.(settings) : undefined; |
| 234 | + if (resetPatch) { |
| 235 | + for (const [resetKey, resetValue] of Object.entries(resetPatch)) { |
| 236 | + if ( |
| 237 | + !prefValuesEqual((settings as unknown as Record<string, unknown>)[resetKey], resetValue) |
| 238 | + ) { |
| 239 | + (patch as Record<string, unknown>)[resetKey] = resetValue; |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + continue; |
| 244 | + } |
| 245 | + if (prefValuesEqual(serverValue, specification.local(settings))) { |
| 246 | + continue; |
| 247 | + } |
| 248 | + if ( |
| 249 | + specification.canApply && |
| 250 | + !(specification.canApply as (value: unknown, settings: UiSettings) => boolean)( |
| 251 | + serverValue, |
| 252 | + settings, |
| 253 | + ) |
| 254 | + ) { |
| 255 | + continue; |
| 256 | + } |
| 257 | + (patch as Record<string, unknown>)[key] = serverValue; |
| 258 | + } |
| 259 | + return Object.keys(patch).length > 0 ? patch : null; |
| 260 | +} |
0 commit comments