diff --git a/apps/web/src/clientSettings.ts b/apps/web/src/clientSettings.ts deleted file mode 100644 index b44da242ea..0000000000 --- a/apps/web/src/clientSettings.ts +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Client-only settings - persisted in localStorage. - * - * These settings affect only the frontend UX and never need to be read by - * the server. Server-authoritative settings live in `ServerSettings` - * (defined in `@t3tools/contracts/server`). - */ -import { Option, Schema } from "effect"; - -export const CLIENT_SETTINGS_STORAGE_KEY = "t3code:client-settings:v1"; - -export const TimestampFormat = Schema.Literals(["locale", "12-hour", "24-hour"]); -export type TimestampFormat = typeof TimestampFormat.Type; -export const DEFAULT_TIMESTAMP_FORMAT: TimestampFormat = "locale"; - -export const SidebarProjectSortOrder = Schema.Literals(["updated_at", "created_at", "manual"]); -export type SidebarProjectSortOrder = typeof SidebarProjectSortOrder.Type; -export const DEFAULT_SIDEBAR_PROJECT_SORT_ORDER: SidebarProjectSortOrder = "updated_at"; - -export const SidebarThreadSortOrder = Schema.Literals(["updated_at", "created_at"]); -export type SidebarThreadSortOrder = typeof SidebarThreadSortOrder.Type; -export const DEFAULT_SIDEBAR_THREAD_SORT_ORDER: SidebarThreadSortOrder = "updated_at"; - -const withDefaults = - < - S extends Schema.Top & Schema.WithoutConstructorDefault, - D extends S["~type.make.in"] & S["Encoded"], - >( - fallback: () => D, - ) => - (schema: S) => - schema.pipe( - Schema.withConstructorDefault(() => Option.some(fallback())), - Schema.withDecodingDefault(() => fallback()), - ); - -export const ClientSettingsSchema = Schema.Struct({ - confirmThreadDelete: Schema.Boolean.pipe(withDefaults(() => true)), - diffWordWrap: Schema.Boolean.pipe(withDefaults(() => false)), - sidebarProjectSortOrder: SidebarProjectSortOrder.pipe( - withDefaults(() => DEFAULT_SIDEBAR_PROJECT_SORT_ORDER), - ), - sidebarThreadSortOrder: SidebarThreadSortOrder.pipe( - withDefaults(() => DEFAULT_SIDEBAR_THREAD_SORT_ORDER), - ), - timestampFormat: TimestampFormat.pipe(withDefaults(() => DEFAULT_TIMESTAMP_FORMAT)), -}); -export type ClientSettings = typeof ClientSettingsSchema.Type; - -export const DEFAULT_CLIENT_SETTINGS: ClientSettings = ClientSettingsSchema.makeUnsafe({}); diff --git a/apps/web/src/components/ChatView.browser.tsx b/apps/web/src/components/ChatView.browser.tsx index 1ace2d88a7..475c2599e3 100644 --- a/apps/web/src/components/ChatView.browser.tsx +++ b/apps/web/src/components/ChatView.browser.tsx @@ -31,7 +31,7 @@ import { isMacPlatform } from "../lib/utils"; import { getRouter } from "../router"; import { useStore } from "../store"; import { estimateTimelineMessageHeight } from "./timelineHeight"; -import { DEFAULT_CLIENT_SETTINGS } from "~/clientSettings"; +import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings"; const THREAD_ID = "thread-browser-test" as ThreadId; const UUID_ROUTE_RE = /^\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/; diff --git a/apps/web/src/components/PlanSidebar.tsx b/apps/web/src/components/PlanSidebar.tsx index a565c09c2e..01341dc803 100644 --- a/apps/web/src/components/PlanSidebar.tsx +++ b/apps/web/src/components/PlanSidebar.tsx @@ -1,5 +1,5 @@ import { memo, useState, useCallback } from "react"; -import { type TimestampFormat } from "../clientSettings"; +import { type TimestampFormat } from "@t3tools/contracts/settings"; import { Badge } from "./ui/badge"; import { Button } from "./ui/button"; import { ScrollArea } from "./ui/scroll-area"; diff --git a/apps/web/src/components/Sidebar.logic.ts b/apps/web/src/components/Sidebar.logic.ts index b77d34b02a..6ca29d27e9 100644 --- a/apps/web/src/components/Sidebar.logic.ts +++ b/apps/web/src/components/Sidebar.logic.ts @@ -1,4 +1,4 @@ -import type { SidebarProjectSortOrder, SidebarThreadSortOrder } from "../clientSettings"; +import type { SidebarProjectSortOrder, SidebarThreadSortOrder } from "@t3tools/contracts/settings"; import type { Thread } from "../types"; import { cn } from "../lib/utils"; import { diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index c8c352ae0e..6c531b1e7a 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -38,7 +38,10 @@ import { } from "@t3tools/contracts"; import { useMutation, useQueries, useQuery, useQueryClient } from "@tanstack/react-query"; import { useLocation, useNavigate, useParams } from "@tanstack/react-router"; -import { type SidebarProjectSortOrder, type SidebarThreadSortOrder } from "../clientSettings"; +import { + type SidebarProjectSortOrder, + type SidebarThreadSortOrder, +} from "@t3tools/contracts/settings"; import { isElectron } from "../env"; import { APP_STAGE_LABEL, APP_VERSION } from "../branding"; import { isLinuxPlatform, isMacPlatform, newCommandId, newProjectId } from "../lib/utils"; diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index 5295d42af1..f3174030ef 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -48,7 +48,7 @@ import { type ParsedTerminalContextEntry, } from "~/lib/terminalContext"; import { cn } from "~/lib/utils"; -import { type TimestampFormat } from "../../clientSettings"; +import { type TimestampFormat } from "@t3tools/contracts/settings"; import { formatTimestamp } from "../../timestampFormat"; import { buildInlineTerminalContextText, diff --git a/apps/web/src/components/chat/TraitsPicker.browser.tsx b/apps/web/src/components/chat/TraitsPicker.browser.tsx index a9e0f097f9..191ef1606d 100644 --- a/apps/web/src/components/chat/TraitsPicker.browser.tsx +++ b/apps/web/src/components/chat/TraitsPicker.browser.tsx @@ -22,7 +22,7 @@ import { useComposerThreadDraft, useEffectiveComposerModelState, } from "../../composerDraftStore"; -import { DEFAULT_CLIENT_SETTINGS } from "~/clientSettings"; +import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings"; // ── Claude TraitsPicker tests ───────────────────────────────────────── diff --git a/apps/web/src/hooks/useSettings.ts b/apps/web/src/hooks/useSettings.ts index 9b743ede3e..982f1c4d15 100644 --- a/apps/web/src/hooks/useSettings.ts +++ b/apps/web/src/hooks/useSettings.ts @@ -19,16 +19,19 @@ import { ThreadEnvMode, } from "@t3tools/contracts"; import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts"; +import { + type ClientSettings, + ClientSettingsSchema, + CLIENT_SETTINGS_STORAGE_KEY, + DEFAULT_CLIENT_SETTINGS, + SidebarProjectSortOrder, + SidebarThreadSortOrder, + TimestampFormat, +} from "@t3tools/contracts/settings"; import { serverConfigQueryOptions, serverQueryKeys } from "~/lib/serverReactQuery"; import { ensureNativeApi } from "~/nativeApi"; import { useLocalStorage } from "./useLocalStorage"; import { normalizeCustomModelSlugs } from "~/modelSelection"; -import { - ClientSettingsSchema, - type ClientSettings, - DEFAULT_CLIENT_SETTINGS, - CLIENT_SETTINGS_STORAGE_KEY, -} from "~/clientSettings"; import { Predicate, Schema, Struct } from "effect"; import { DeepMutable } from "effect/Types"; import { deepMerge } from "@t3tools/shared/Struct"; @@ -199,9 +202,38 @@ export function buildLegacyServerSettingsMigrationPatch(legacySettings: Record, +): Partial> { + const patch: Partial> = {}; + + if (Predicate.isBoolean(legacySettings.confirmThreadDelete)) { + patch.confirmThreadDelete = legacySettings.confirmThreadDelete; + } + + if (Predicate.isBoolean(legacySettings.diffWordWrap)) { + patch.diffWordWrap = legacySettings.diffWordWrap; + } + + if (Schema.is(SidebarProjectSortOrder)(legacySettings.sidebarProjectSortOrder)) { + patch.sidebarProjectSortOrder = legacySettings.sidebarProjectSortOrder; + } + + if (Schema.is(SidebarThreadSortOrder)(legacySettings.sidebarThreadSortOrder)) { + patch.sidebarThreadSortOrder = legacySettings.sidebarThreadSortOrder; + } + + if (Schema.is(TimestampFormat)(legacySettings.timestampFormat)) { + patch.timestampFormat = legacySettings.timestampFormat; + } + + return patch; +} + /** - * Call once on app startup. Migrates server-relevant settings from the - * old localStorage key to the server. Idempotent. + * Call once on app startup. Migrates settings from the old unified + * localStorage key to the server (server-relevant keys) and to the new + * client-settings localStorage key (client-only keys). Idempotent. */ export function migrateLocalSettingsToServer(): void { if (typeof window === "undefined") return; @@ -217,19 +249,27 @@ export function migrateLocalSettingsToServer(): void { const old = JSON.parse(raw); if (!Predicate.isObject(old)) return; - const api = ensureNativeApi(); - + // Migrate server-relevant keys via RPC const serverPatch = buildLegacyServerSettingsMigrationPatch(old); - - if (Object.keys(serverPatch).length === 0) { - localStorage.setItem(MIGRATION_FLAG_KEY, "true"); - return; + if (Object.keys(serverPatch).length > 0) { + const api = ensureNativeApi(); + void api.server.updateSettings(serverPatch); + } + + // Migrate client-only keys to the new localStorage key + const clientPatch = buildLegacyClientSettingsMigrationPatch(old); + if (Object.keys(clientPatch).length > 0) { + const existing = localStorage.getItem(CLIENT_SETTINGS_STORAGE_KEY); + const current = existing ? (JSON.parse(existing) as Record) : {}; + localStorage.setItem( + CLIENT_SETTINGS_STORAGE_KEY, + JSON.stringify({ ...current, ...clientPatch }), + ); } - void api.server.updateSettings(serverPatch); localStorage.setItem(MIGRATION_FLAG_KEY, "true"); } catch (error) { - console.error("[MIGRATION] Error migrating local settings to server:", error); + console.error("[MIGRATION] Error migrating local settings:", error); } finally { // Mark as migrated no matter what to avoid retrying localStorage.setItem(MIGRATION_FLAG_KEY, "true"); diff --git a/apps/web/src/timestampFormat.ts b/apps/web/src/timestampFormat.ts index 1687f58c73..71fc105c2e 100644 --- a/apps/web/src/timestampFormat.ts +++ b/apps/web/src/timestampFormat.ts @@ -1,4 +1,4 @@ -import { type TimestampFormat } from "./clientSettings"; +import { type TimestampFormat } from "@t3tools/contracts/settings"; export function getTimestampFormatOptions( timestampFormat: TimestampFormat, diff --git a/packages/contracts/package.json b/packages/contracts/package.json index 35f8b15498..29d0c1398f 100644 --- a/packages/contracts/package.json +++ b/packages/contracts/package.json @@ -10,6 +10,11 @@ "module": "./dist/index.mjs", "types": "./src/index.ts", "exports": { + "./settings": { + "types": "./src/settings.ts", + "import": "./src/settings.ts", + "require": "./src/settings.ts" + }, ".": { "types": "./src/index.ts", "import": "./src/index.ts", diff --git a/packages/contracts/src/server.ts b/packages/contracts/src/server.ts index 4078b41981..edd50ec4d0 100644 --- a/packages/contracts/src/server.ts +++ b/packages/contracts/src/server.ts @@ -2,100 +2,24 @@ import { Schema } from "effect"; import { IsoDateTime, TrimmedNonEmptyString } from "./baseSchemas"; import { KeybindingRule, ResolvedKeybindingsConfig } from "./keybindings"; import { EditorId } from "./editor"; +import { ProviderKind } from "./orchestration"; import { - ClaudeModelOptions, - CodexModelOptions, - DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER, -} from "./model"; -import { ModelSelection, ProviderKind } from "./orchestration"; + DEFAULT_SERVER_SETTINGS, + CodexSettings, + ClaudeSettings, + ServerSettings, + ServerSettingsPatch, + ThreadEnvMode, +} from "./settings"; -// ── Server Settings (server-authoritative) ─────────────────────────── - -export const ThreadEnvMode = Schema.Literals(["local", "worktree"]); -export type ThreadEnvMode = typeof ThreadEnvMode.Type; - -export const CodexSettings = Schema.Struct({ - binaryPath: Schema.String.pipe(Schema.withDecodingDefault(() => "")), - homePath: Schema.String.pipe(Schema.withDecodingDefault(() => "")), - customModels: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(() => [])), -}); -export type CodexSettings = typeof CodexSettings.Type; - -export const ClaudeSettings = Schema.Struct({ - binaryPath: Schema.String.pipe(Schema.withDecodingDefault(() => "")), - customModels: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(() => [])), -}); -export type ClaudeSettings = typeof ClaudeSettings.Type; - -export const ServerSettings = Schema.Struct({ - enableAssistantStreaming: Schema.Boolean.pipe(Schema.withDecodingDefault(() => false)), - defaultThreadEnvMode: ThreadEnvMode.pipe( - Schema.withDecodingDefault(() => "local" as const satisfies ThreadEnvMode), - ), - textGenerationModelSelection: ModelSelection.pipe( - Schema.withDecodingDefault(() => ({ - provider: "codex" as const, - model: DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER.codex, - })), - ), - - // Provider specific settings - providers: Schema.Struct({ - codex: CodexSettings.pipe(Schema.withDecodingDefault(() => ({}))), - claudeAgent: ClaudeSettings.pipe(Schema.withDecodingDefault(() => ({}))), - }).pipe(Schema.withDecodingDefault(() => ({}))), -}); -export type ServerSettings = typeof ServerSettings.Type; - -export const DEFAULT_SERVER_SETTINGS: ServerSettings = Schema.decodeUnknownSync(ServerSettings)({}); - -const CodexModelOptionsPatch = Schema.Struct({ - reasoningEffort: Schema.optionalKey(CodexModelOptions.fields.reasoningEffort), - fastMode: Schema.optionalKey(CodexModelOptions.fields.fastMode), -}); - -const ClaudeModelOptionsPatch = Schema.Struct({ - thinking: Schema.optionalKey(ClaudeModelOptions.fields.thinking), - effort: Schema.optionalKey(ClaudeModelOptions.fields.effort), - fastMode: Schema.optionalKey(ClaudeModelOptions.fields.fastMode), -}); - -const ModelSelectionPatch = Schema.Union([ - Schema.Struct({ - provider: Schema.optionalKey(Schema.Literal("codex")), - model: Schema.optionalKey(TrimmedNonEmptyString), - options: Schema.optionalKey(CodexModelOptionsPatch), - }), - Schema.Struct({ - provider: Schema.optionalKey(Schema.Literal("claudeAgent")), - model: Schema.optionalKey(TrimmedNonEmptyString), - options: Schema.optionalKey(ClaudeModelOptionsPatch), - }), -]); - -const CodexSettingsPatch = Schema.Struct({ - binaryPath: Schema.optionalKey(Schema.String), - homePath: Schema.optionalKey(Schema.String), - customModels: Schema.optionalKey(Schema.Array(Schema.String)), -}); - -const ClaudeSettingsPatch = Schema.Struct({ - binaryPath: Schema.optionalKey(Schema.String), - customModels: Schema.optionalKey(Schema.Array(Schema.String)), -}); - -export const ServerSettingsPatch = Schema.Struct({ - enableAssistantStreaming: Schema.optionalKey(Schema.Boolean), - defaultThreadEnvMode: Schema.optionalKey(ThreadEnvMode), - textGenerationModelSelection: Schema.optionalKey(ModelSelectionPatch), - providers: Schema.optionalKey( - Schema.Struct({ - codex: Schema.optionalKey(CodexSettingsPatch), - claudeAgent: Schema.optionalKey(ClaudeSettingsPatch), - }), - ), -}); -export type ServerSettingsPatch = typeof ServerSettingsPatch.Type; +export { + DEFAULT_SERVER_SETTINGS, + ServerSettings, + ServerSettingsPatch, + ThreadEnvMode, + CodexSettings, + ClaudeSettings, +}; const KeybindingsMalformedConfigIssue = Schema.Struct({ kind: Schema.Literal("keybindings.malformed-config"), diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts new file mode 100644 index 0000000000..7ad952012d --- /dev/null +++ b/packages/contracts/src/settings.ts @@ -0,0 +1,129 @@ +import * as Schema from "effect/Schema"; +import { TrimmedNonEmptyString } from "./baseSchemas"; +import { + ClaudeModelOptions, + CodexModelOptions, + DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER, +} from "./model"; +import { ModelSelection } from "./orchestration"; + +// ── Client Settings (local-only) ─────────────────────────────── + +export const CLIENT_SETTINGS_STORAGE_KEY = "t3code:client-settings:v1"; + +export const TimestampFormat = Schema.Literals(["locale", "12-hour", "24-hour"]); +export type TimestampFormat = typeof TimestampFormat.Type; +export const DEFAULT_TIMESTAMP_FORMAT: TimestampFormat = "locale"; + +export const SidebarProjectSortOrder = Schema.Literals(["updated_at", "created_at", "manual"]); +export type SidebarProjectSortOrder = typeof SidebarProjectSortOrder.Type; +export const DEFAULT_SIDEBAR_PROJECT_SORT_ORDER: SidebarProjectSortOrder = "updated_at"; + +export const SidebarThreadSortOrder = Schema.Literals(["updated_at", "created_at"]); +export type SidebarThreadSortOrder = typeof SidebarThreadSortOrder.Type; +export const DEFAULT_SIDEBAR_THREAD_SORT_ORDER: SidebarThreadSortOrder = "updated_at"; + +export const ClientSettingsSchema = Schema.Struct({ + confirmThreadDelete: Schema.Boolean.pipe(Schema.withDecodingDefault(() => true)), + diffWordWrap: Schema.Boolean.pipe(Schema.withDecodingDefault(() => false)), + sidebarProjectSortOrder: SidebarProjectSortOrder.pipe( + Schema.withDecodingDefault(() => DEFAULT_SIDEBAR_PROJECT_SORT_ORDER), + ), + sidebarThreadSortOrder: SidebarThreadSortOrder.pipe( + Schema.withDecodingDefault(() => DEFAULT_SIDEBAR_THREAD_SORT_ORDER), + ), + timestampFormat: TimestampFormat.pipe(Schema.withDecodingDefault(() => DEFAULT_TIMESTAMP_FORMAT)), +}); +export type ClientSettings = typeof ClientSettingsSchema.Type; + +export const DEFAULT_CLIENT_SETTINGS: ClientSettings = Schema.decodeSync(ClientSettingsSchema)({}); + +// ── Server Settings (server-authoritative) ──────────────────── + +export const ThreadEnvMode = Schema.Literals(["local", "worktree"]); +export type ThreadEnvMode = typeof ThreadEnvMode.Type; + +export const CodexSettings = Schema.Struct({ + binaryPath: Schema.String.pipe(Schema.withDecodingDefault(() => "")), + homePath: Schema.String.pipe(Schema.withDecodingDefault(() => "")), + customModels: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(() => [])), +}); +export type CodexSettings = typeof CodexSettings.Type; + +export const ClaudeSettings = Schema.Struct({ + binaryPath: Schema.String.pipe(Schema.withDecodingDefault(() => "")), + customModels: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(() => [])), +}); +export type ClaudeSettings = typeof ClaudeSettings.Type; + +export const ServerSettings = Schema.Struct({ + enableAssistantStreaming: Schema.Boolean.pipe(Schema.withDecodingDefault(() => false)), + defaultThreadEnvMode: ThreadEnvMode.pipe( + Schema.withDecodingDefault(() => "local" as const satisfies ThreadEnvMode), + ), + textGenerationModelSelection: ModelSelection.pipe( + Schema.withDecodingDefault(() => ({ + provider: "codex" as const, + model: DEFAULT_GIT_TEXT_GENERATION_MODEL_BY_PROVIDER.codex, + })), + ), + + // Provider specific settings + providers: Schema.Struct({ + codex: CodexSettings.pipe(Schema.withDecodingDefault(() => ({}))), + claudeAgent: ClaudeSettings.pipe(Schema.withDecodingDefault(() => ({}))), + }).pipe(Schema.withDecodingDefault(() => ({}))), +}); +export type ServerSettings = typeof ServerSettings.Type; + +export const DEFAULT_SERVER_SETTINGS: ServerSettings = Schema.decodeSync(ServerSettings)({}); + +// ── Server Settings Patch (replace with a Schema.deepPartial if available) ────────────────────────────────────────── + +const CodexModelOptionsPatch = Schema.Struct({ + reasoningEffort: Schema.optionalKey(CodexModelOptions.fields.reasoningEffort), + fastMode: Schema.optionalKey(CodexModelOptions.fields.fastMode), +}); + +const ClaudeModelOptionsPatch = Schema.Struct({ + thinking: Schema.optionalKey(ClaudeModelOptions.fields.thinking), + effort: Schema.optionalKey(ClaudeModelOptions.fields.effort), + fastMode: Schema.optionalKey(ClaudeModelOptions.fields.fastMode), +}); + +const ModelSelectionPatch = Schema.Union([ + Schema.Struct({ + provider: Schema.optionalKey(Schema.Literal("codex")), + model: Schema.optionalKey(TrimmedNonEmptyString), + options: Schema.optionalKey(CodexModelOptionsPatch), + }), + Schema.Struct({ + provider: Schema.optionalKey(Schema.Literal("claudeAgent")), + model: Schema.optionalKey(TrimmedNonEmptyString), + options: Schema.optionalKey(ClaudeModelOptionsPatch), + }), +]); + +const CodexSettingsPatch = Schema.Struct({ + binaryPath: Schema.optionalKey(Schema.String), + homePath: Schema.optionalKey(Schema.String), + customModels: Schema.optionalKey(Schema.Array(Schema.String)), +}); + +const ClaudeSettingsPatch = Schema.Struct({ + binaryPath: Schema.optionalKey(Schema.String), + customModels: Schema.optionalKey(Schema.Array(Schema.String)), +}); + +export const ServerSettingsPatch = Schema.Struct({ + enableAssistantStreaming: Schema.optionalKey(Schema.Boolean), + defaultThreadEnvMode: Schema.optionalKey(ThreadEnvMode), + textGenerationModelSelection: Schema.optionalKey(ModelSelectionPatch), + providers: Schema.optionalKey( + Schema.Struct({ + codex: Schema.optionalKey(CodexSettingsPatch), + claudeAgent: Schema.optionalKey(ClaudeSettingsPatch), + }), + ), +}); +export type ServerSettingsPatch = typeof ServerSettingsPatch.Type;